-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a Data Provider to manage images #34
Open
Antonio171003
wants to merge
5
commits into
wiringbits:main
Choose a base branch
from
Antonio171003:add-data-provider-to-manage-images
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
00dbce6
Added a Data Provider to manage images
Antonio171003 d645f56
Merge branch 'main' into add-data-provider-to-manage-images
AlexITC df59cda
Add exceptions to the convertHexToImage function to check that the ar…
Antonio171003 8fd1aaf
Merge branch 'main' into add-data-provider-to-manage-images
AlexITC ca0ce83
Added an object parseHexString to convert hex strings to a byte array…
Antonio171003 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
spra-web/src/main/scala/net/wiringbits/spra/ui/web/facades/DataProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,19 @@ | ||
package net.wiringbits.spra.ui.web.facades | ||
|
||
import scala.scalajs.js | ||
import scala.scalajs.js.annotation.JSImport | ||
|
||
@js.native | ||
trait DataProvider extends js.Object | ||
|
||
@js.native | ||
@JSImport("ra-data-simple-rest", JSImport.Default) | ||
// https://www.npmjs.com/package/ra-data-simple-rest | ||
def simpleRestProvider(url: String): DataProvider = js.native | ||
|
||
@js.native | ||
@JSImport("react-admin", "withLifecycleCallbacks") | ||
// https://marmelab.com/react-admin/withLifecycleCallbacks.html | ||
object WithLifecycleCallbacks extends js.Object { | ||
def apply(dataProvider: DataProvider, callbacks: js.Array[js.Object]): DataProvider = js.native | ||
} |
30 changes: 26 additions & 4 deletions
30
spra-web/src/main/scala/net/wiringbits/spra/ui/web/facades/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,32 @@ | ||
package net.wiringbits.spra.ui.web | ||
|
||
import scala.scalajs.js | ||
import scala.scalajs.js.annotation.JSImport | ||
import net.wiringbits.spra.ui.web.utils.Images.* | ||
import org.scalajs.dom.File | ||
|
||
package object facades { | ||
@js.native | ||
@JSImport("ra-data-simple-rest", JSImport.Default) | ||
def simpleRestProvider(url: String): DataProvider = js.native | ||
def createDataProvider(url: String): DataProvider = { | ||
val baseDataProvider = simpleRestProvider(url) | ||
WithLifecycleCallbacks( | ||
baseDataProvider, | ||
js.Array( | ||
js.Dynamic.literal( | ||
resource = "images", | ||
afterRead = (record: js.Dynamic, dataProvider: js.Any) => { | ||
val hexImage = record.data.asInstanceOf[String] | ||
val urlImage = convertHexToImage(hexImage) | ||
record.updateDynamic("data")(urlImage) | ||
record | ||
}, | ||
beforeSave = (data: js.Dynamic, dataProvider: js.Any) => { | ||
val rawFile = data.data.rawFile.asInstanceOf[File] | ||
convertImageToByteArray(rawFile).`then` { value => | ||
data.updateDynamic("data")(value.asInstanceOf[js.Any]) | ||
data | ||
} | ||
} | ||
) | ||
) | ||
) | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
spra-web/src/main/scala/net/wiringbits/spra/ui/web/utils/Images.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package net.wiringbits.spra.ui.web.utils | ||
|
||
import org.scalajs.dom | ||
import org.scalajs.dom.{Blob, File} | ||
import scala.util.{Failure, Success, Try} | ||
import scala.scalajs.js.Promise | ||
import scala.scalajs.js.typedarray.{ArrayBuffer, Int8Array, Uint8Array} | ||
import scala.scalajs.js | ||
|
||
object Images { | ||
def convertImageToByteArray(image: dom.File): js.Promise[String] = { | ||
new js.Promise[String]((resolve, reject) => { | ||
val reader = new dom.FileReader() | ||
reader.onload = { (e: dom.Event) => | ||
val arrayBuffer = reader.result.asInstanceOf[ArrayBuffer] | ||
val byteArray = new Int8Array(arrayBuffer).toArray | ||
resolve(byteArray.mkString("[", ", ", "]")) | ||
} | ||
reader.onerror = { (e: dom.Event) => | ||
reject(new js.Error("Failed to read file")) | ||
} | ||
reader.readAsArrayBuffer(image) | ||
}) | ||
} | ||
|
||
def convertHexToImage(imageHex: String): String = { | ||
// Check if the argument is a hexadecimal string" | ||
if (!imageHex.startsWith("\\x") || (imageHex.length % 2) == 1) { | ||
throw new IllegalArgumentException(s"Error: Expected a hexadecimal string but found: $imageHex") | ||
} | ||
// Remove the "\x" prefix from the hex string, as it's not part of the actual image data | ||
val hex = imageHex.tail.tail | ||
val imageBinary: Array[Byte] = | ||
Try(hex.grouped(2).map { hex => Integer.parseInt(hex, 16).toByte }.toArray) match { | ||
case Success(value) => value | ||
case Failure(_) => Array.empty | ||
Antonio171003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
val byteArray = Uint8Array(js.Array(imageBinary.map(_.toShort): _*)) | ||
dom.URL.createObjectURL(dom.Blob(js.Array(byteArray.buffer))) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
imageHex.startsWith("0x")
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All hexadecimal strings from the response have the prefix \x.