Skip to content

Commit f3ac0f6

Browse files
committed
Introduced a simple way to work with files inside frontend
The usecase of this code is something like this: let image that user is making a protonmail or any another application where content inside frontend (=browser storage) some secret user's data that shouldn't be exposed to backend. Secret key for example or anything like that. This code allows to developer to save any `Array[Byte]` from frontend as simple call `FileService.save()` and load any uploaded file to `Array[Byte]` as `FileService.load()`. Unfortunately scalatags doesn't support `download` attribute and I need to make it by hand. I've opened a PR: com-lihaoyi/scalatags#212 to introduce it, but it might be a while until it is included to release.
1 parent 7e54db1 commit f3ac0f6

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.udash.utils
2+
3+
import java.io.IOException
4+
5+
import org.scalajs.dom._
6+
import scalatags.JsDom.all._
7+
8+
import scala.scalajs.js
9+
import js.JSConverters._
10+
import js.typedarray._
11+
12+
import scala.concurrent._
13+
import scala.util.Try
14+
15+
object FileService {
16+
def save(filename: String, bytes: Array[Byte], mimeType: String = "application/octet-stream"): Unit = {
17+
val jsBytes = Array[js.Any](bytes.toTypedArray).toJSArray
18+
val blob = new Blob(jsBytes, BlobPropertyBag(mimeType))
19+
val url = URL.createObjectURL(blob)
20+
val download = attr("download")
21+
22+
val anchor = a(href := url, download := filename, style := "display: none")(filename).render
23+
document.body.appendChild(anchor)
24+
anchor.click()
25+
document.body.removeChild(anchor)
26+
URL.revokeObjectURL(url)
27+
}
28+
29+
def load(file: File): Future[Array[Byte]] = {
30+
val fileReader = new FileReader()
31+
val promise = Promise[Array[Byte]]()
32+
33+
fileReader.onerror = (e: Event) =>
34+
promise.failure(new IOException(e.toString))
35+
36+
fileReader.onload = (_: UIEvent) =>
37+
promise.complete(Try(
38+
new Int8Array(fileReader.result.asInstanceOf[ArrayBuffer]).toArray
39+
))
40+
41+
fileReader.readAsArrayBuffer(file)
42+
43+
promise.future
44+
}
45+
}

0 commit comments

Comments
 (0)