Skip to content

Commit 0111442

Browse files
committed
FileService: a way to work with file at 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 convert any `Seq[Array[Byte]]` from frontend to URL as simple call `FileService.createURL`, asynchronously convert any uploaded `File` to `Future[Array[Byte]]` as `FileService.asBytesArray`, or to `Array[Byte]` via `FileService.asSyncBytesArray` inside worker. Unfortunately scalatags doesn't support `download` attribute and I need to make it by hand. I've opened a PR[^1] to introduce it, but it might be a while until it is included to release. `FileService.asSyncBytesArray` is using `FileReaderSync` that is also missed inside scala-js-dom. I've opened a PR[^2] but it might be a while. Also, this draft API but it is supported by majority of modern browsers[^3]. [^1]: com-lihaoyi/scalatags#212 [^2]: scala-js/scala-js-dom#424 [^3]: https://caniuse.com/?search=FileReaderSync
1 parent 6aacd3f commit 0111442

File tree

3 files changed

+159
-2
lines changed

3 files changed

+159
-2
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package io.udash.utils
2+
3+
import com.avsystem.commons.misc.AbstractCase
4+
5+
import java.io.IOException
6+
import org.scalajs.dom._
7+
import org.scalajs.dom.raw.Blob
8+
9+
import scala.scalajs.js
10+
import scala.concurrent.{Future, Promise}
11+
import scala.scalajs.js.annotation.JSGlobal
12+
import scala.scalajs.js.typedarray.ArrayBuffer
13+
import scala.util.Try
14+
15+
@js.native
16+
@JSGlobal
17+
private[utils] final class FileReaderSync() extends js.Object {
18+
def readAsArrayBuffer(blob: Blob): ArrayBuffer = js.native
19+
}
20+
21+
final case class CloseableUrl(value: String) extends AbstractCase with AutoCloseable {
22+
override def close(): Unit = {
23+
URL.revokeObjectURL(value)
24+
}
25+
}
26+
27+
object FileService {
28+
29+
final val OctetStreamType = "application/octet-stream"
30+
31+
/**
32+
* Converts specified bytes arrays to string that contains URL
33+
* that representing the array given in the parameter with specified mime-type.
34+
*
35+
* Keep in mind that returned URL should be closed.
36+
*/
37+
def createURL(bytesArrays: Seq[Array[Byte]], mimeType: String): CloseableUrl = {
38+
import js.typedarray._
39+
40+
val jsBytesArrays = js.Array[js.Any](bytesArrays.map(_.toTypedArray) :_ *)
41+
val blob = new Blob(jsBytesArrays, BlobPropertyBag(mimeType))
42+
CloseableUrl(URL.createObjectURL(blob))
43+
}
44+
45+
/**
46+
* Converts specified bytes arrays to string that contains URL
47+
* that representing the array given in the parameter with `application/octet-stream` mime-type.
48+
*
49+
* Keep in mind that returned URL should be closed.
50+
*/
51+
def createURL(bytesArrays: Seq[Array[Byte]]): CloseableUrl =
52+
createURL(bytesArrays, OctetStreamType)
53+
54+
/**
55+
* Converts specified bytes array to string that contains URL
56+
* that representing the array given in the parameter with specified mime-type.
57+
*
58+
* Keep in mind that returned URL should be closed.
59+
*/
60+
def createURL(byteArray: Array[Byte], mimeType: String): CloseableUrl =
61+
createURL(Seq(byteArray), mimeType)
62+
63+
/**
64+
* Converts specified bytes array to string that contains URL
65+
* that representing the array given in the parameter with `application/octet-stream` mime-type.
66+
*
67+
* Keep in mind that returned URL should be closed.
68+
*/
69+
def createURL(byteArray: Array[Byte]): CloseableUrl =
70+
createURL(Seq(byteArray), OctetStreamType)
71+
72+
/**
73+
* Asynchronously convert specified part of file to bytes array.
74+
*/
75+
def asBytesArray(file: File, start: Double, end: Double): Future[Array[Byte]] = {
76+
import js.typedarray._
77+
78+
val fileReader = new FileReader()
79+
val promise = Promise[Array[Byte]]()
80+
81+
fileReader.onerror = (e: Event) =>
82+
promise.failure(new IOException(e.toString))
83+
84+
fileReader.onabort = (e: Event) =>
85+
promise.failure(new IOException(e.toString))
86+
87+
fileReader.onload = (_: UIEvent) =>
88+
promise.complete(Try(
89+
new Int8Array(fileReader.result.asInstanceOf[ArrayBuffer]).toArray
90+
))
91+
92+
val slice = file.slice(start, end)
93+
fileReader.readAsArrayBuffer(slice)
94+
95+
promise.future
96+
}
97+
98+
/**
99+
* Asynchronously convert specified file to bytes array.
100+
*/
101+
def asBytesArray(file: File): Future[Array[Byte]] =
102+
asBytesArray(file, 0, file.size)
103+
104+
/**
105+
* Synchronously convert specified part of file to bytes array.
106+
*
107+
* Because it is using synchronous I/O this API can be used only inside worker.
108+
*
109+
* This method is using FileReaderSync that is part of Working Draft File API.
110+
* Anyway it is supported for majority of modern browsers
111+
*/
112+
def asSyncBytesArray(file: File, start: Double, end: Double): Array[Byte] = {
113+
import js.typedarray._
114+
115+
val fileReaderSync = new FileReaderSync()
116+
val slice = file.slice(start, end)
117+
118+
val int8Array = new Int8Array(fileReaderSync.readAsArrayBuffer(slice))
119+
120+
int8Array.toArray
121+
}
122+
123+
/**
124+
* Synchronously convert file to bytes array.
125+
*
126+
* Because it is using synchronous I/O this API can be used only inside worker.
127+
*
128+
* This method is using FileReaderSync that is part of Working Draft File API.
129+
* Anyway it is supported for majority of modern browsers
130+
*/
131+
def asSyncBytesArray(file: File): Array[Byte] =
132+
asSyncBytesArray(file, 0, file.size)
133+
}

guide/guide/.js/src/main/scala/io/udash/web/guide/views/frontend/FrontendFilesView.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class FrontendFilesView extends View {
2424
),
2525
p("You can find a working demo application in the ", a(href := References.UdashFilesDemoRepo, target := "_blank")("Udash Demos"), " repositiory."),
2626
h3("Frontend forms"),
27+
p(i("FileService"), " is an object that allows to convert ", i("Array[Byte]")," to URL, save it as file from frontend ",
28+
" and asynchronously convert ", i("File"), " to ", i("Future[Array[Byte]]"), "."),
2729
p(i("FileInput"), " is the file HTML input wrapper providing a property containing selected files. "),
2830
fileInputSnippet,
2931
p("Take a look at the following live demo:"),

guide/guide/.js/src/main/scala/io/udash/web/guide/views/frontend/demos/FileInputDemo.scala

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.udash.web.guide.views.frontend.demos
22

33
import io.udash.css.CssView
4+
import io.udash.utils.FileService
45
import io.udash.web.guide.demos.AutoDemo
56
import io.udash.web.guide.styles.partials.GuideStyles
67
import scalatags.JsDom.all._
@@ -12,14 +13,35 @@ object FileInputDemo extends AutoDemo with CssView {
1213
import org.scalajs.dom.File
1314
import scalatags.JsDom.all._
1415

15-
val acceptMultipleFiles = Property(true)
16+
import scala.concurrent.ExecutionContext.Implicits.global
17+
18+
val acceptMultipleFiles = true.toProperty
1619
val selectedFiles = SeqProperty.blank[File]
1720

1821
div(
1922
FileInput(selectedFiles, acceptMultipleFiles)("files"),
2023
h4("Selected files"),
2124
ul(repeat(selectedFiles)(file => {
22-
li(file.get.name).render
25+
val content = Property(Array.empty[Byte])
26+
27+
FileService.asBytesArray(file.get) foreach { bytes =>
28+
content.set(bytes)
29+
}
30+
31+
val name = file.get.name
32+
li(showIfElse(content.transform(_.isEmpty))(
33+
span(name).render,
34+
{
35+
val url = FileService.createURL(content.get)
36+
val download = a(href := url.value, attr("download") := name)(name)
37+
val revoke = a(href := "#", onclick := { () =>
38+
content.set(Array.empty[Byte])
39+
url.close()
40+
})("revoke")
41+
42+
Seq(download, span(" or "), revoke).render
43+
}
44+
)).render
2345
}))
2446
)
2547
}.withSourceCode

0 commit comments

Comments
 (0)