Skip to content

Commit 762c992

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 762c992

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.udash.utils
2+
3+
import java.io.IOException
4+
5+
import org.scalajs.dom._
6+
import org.scalajs.dom.html.Anchor
7+
import scalatags.JsDom
8+
9+
import scala.scalajs.js
10+
import scala.concurrent.{Future, Promise}
11+
import scala.util.Try
12+
13+
object FileService {
14+
15+
final val OctetStreamType = "application/octet-stream"
16+
17+
/**
18+
* Converts specified bytes array to string that contains URL
19+
* that representing the array given in the parameter with optionally specified mime-type.
20+
*
21+
* Keep in mind that returned URL should be revoked via `org.scalajs.dom.revokeObjectURL(url)`.
22+
*/
23+
def bytesAsURL(bytes: Array[Byte], mimeType: String = OctetStreamType): String = {
24+
import js.typedarray._
25+
26+
val jsBytes = js.Array[js.Any](bytes.toTypedArray)
27+
val blob = new Blob(jsBytes, BlobPropertyBag(mimeType))
28+
URL.createObjectURL(blob)
29+
}
30+
31+
/**
32+
* Create an anchor element that on click downloads byte array as a file with specified name.
33+
*
34+
* Keep in mind that anchor's href URL should be revoked via `org.scalajs.dom.revokeObjectURL(url)`.
35+
*/
36+
def save(filename: String, bytes: Array[Byte], mimeType: String = OctetStreamType): JsDom.TypedTag[Anchor] = {
37+
import JsDom.all._
38+
39+
val download = attr("download")
40+
a(href := bytesAsURL(bytes, mimeType), download := filename)
41+
}
42+
43+
/**
44+
* Asynchronously convert specified file to bytes array.
45+
*/
46+
def load(file: File): Future[Array[Byte]] = {
47+
import js.typedarray._
48+
49+
val fileReader = new FileReader()
50+
val promise = Promise[Array[Byte]]()
51+
52+
fileReader.onerror = (e: Event) =>
53+
promise.failure(new IOException(e.toString))
54+
55+
fileReader.onabort = (e: Event) =>
56+
promise.failure(new IOException(e.toString))
57+
58+
fileReader.onload = (_: UIEvent) =>
59+
promise.complete(Try(
60+
new Int8Array(fileReader.result.asInstanceOf[ArrayBuffer]).toArray
61+
))
62+
63+
fileReader.readAsArrayBuffer(file)
64+
65+
promise.future
66+
}
67+
}

0 commit comments

Comments
 (0)