Skip to content

Commit 0d90a27

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 `Array[Byte]` as `FileService.asBytesArray`, or to `InputStream` via `FileService.asInputStream` 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.asInputStream` 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 0d90a27

File tree

3 files changed

+185
-2
lines changed

3 files changed

+185
-2
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package io.udash.utils
2+
3+
import java.io.{IOException, InputStream}
4+
5+
import org.scalajs.dom._
6+
import org.scalajs.dom.raw.Blob
7+
8+
import scala.scalajs.js
9+
import scala.concurrent.{Future, Promise}
10+
import scala.scalajs.js.annotation.JSGlobal
11+
import scala.scalajs.js.typedarray.ArrayBuffer
12+
import scala.util.Try
13+
14+
@js.native
15+
@JSGlobal
16+
sealed class FileReaderSync() extends js.Object {
17+
def readAsArrayBuffer(blob: Blob): ArrayBuffer = js.native
18+
}
19+
20+
final class FileBufferedInputStream(file: File, bufferSize: Int) extends InputStream {
21+
val fileReaderSync = new FileReaderSync()
22+
23+
var filePos: Int = 0
24+
var pos: Int = 0
25+
26+
var buffer: Array[Byte] = Array.empty
27+
28+
override def available(): Int = {
29+
val res = file.size.toInt - filePos
30+
if (res < 0) 0 else res
31+
}
32+
33+
override def read(): Int = {
34+
if (pos >= buffer.length) {
35+
import js.typedarray._
36+
37+
if (filePos >= file.size)
38+
return -1
39+
40+
val len = math.min(filePos + bufferSize, file.size.toInt)
41+
val slice = file.slice(filePos, len)
42+
buffer = new Int8Array(fileReaderSync.readAsArrayBuffer(slice)).toArray
43+
filePos += buffer.length
44+
pos = 0
45+
}
46+
val r = buffer(pos).toInt & 0xff
47+
pos += 1
48+
r
49+
}
50+
51+
override def close(): Unit = filePos = file.size.toInt
52+
53+
override def markSupported(): Boolean = true
54+
55+
var markPos: Option[Int] = None
56+
57+
override def mark(readlimit: Int): Unit =
58+
markPos = Some(pos + filePos - buffer.length)
59+
60+
override def reset(): Unit = markPos match {
61+
case Some(p) =>
62+
filePos = p
63+
pos = 0
64+
buffer = Array.empty
65+
markPos = None
66+
67+
case _ =>
68+
// do nothing
69+
}
70+
}
71+
72+
final case class CloseableUrl(value: String) extends AutoCloseable {
73+
override def close(): Unit = {
74+
URL.revokeObjectURL(value)
75+
}
76+
}
77+
78+
object FileService {
79+
80+
final val OctetStreamType = "application/octet-stream"
81+
82+
/**
83+
* Converts specified bytes arrays to string that contains URL
84+
* that representing the array given in the parameter with specified mime-type.
85+
*
86+
* Keep in mind that returned URL should be closed.
87+
*/
88+
def createURL(bytesArrays: Seq[Array[Byte]], mimeType: String): CloseableUrl = {
89+
import js.typedarray._
90+
91+
val jsBytesArrays = js.Array[js.Any](bytesArrays.map(_.toTypedArray) :_ *)
92+
val blob = new Blob(jsBytesArrays, BlobPropertyBag(mimeType))
93+
CloseableUrl(URL.createObjectURL(blob))
94+
}
95+
96+
/**
97+
* Converts specified bytes arrays to string that contains URL
98+
* that representing the array given in the parameter with `application/octet-stream` mime-type.
99+
*
100+
* Keep in mind that returned URL should be closed.
101+
*/
102+
def createURL(bytesArrays: Seq[Array[Byte]]): CloseableUrl =
103+
createURL(bytesArrays, OctetStreamType)
104+
105+
/**
106+
* Converts specified bytes array to string that contains URL
107+
* that representing the array given in the parameter with specified mime-type.
108+
*
109+
* Keep in mind that returned URL should be closed.
110+
*/
111+
def createURL(byteArray: Array[Byte], mimeType: String): CloseableUrl =
112+
createURL(Seq(byteArray), mimeType)
113+
114+
/**
115+
* Converts specified bytes array to string that contains URL
116+
* that representing the array given in the parameter with `application/octet-stream` mime-type.
117+
*
118+
* Keep in mind that returned URL should be closed.
119+
*/
120+
def createURL(byteArray: Array[Byte]): CloseableUrl =
121+
createURL(Seq(byteArray), OctetStreamType)
122+
123+
/**
124+
* Asynchronously convert specified file to bytes array.
125+
*/
126+
def asBytesArray(file: File): Future[Array[Byte]] = {
127+
import js.typedarray._
128+
129+
val fileReader = new FileReader()
130+
val promise = Promise[Array[Byte]]()
131+
132+
fileReader.onerror = (e: Event) =>
133+
promise.failure(new IOException(e.toString))
134+
135+
fileReader.onabort = (e: Event) =>
136+
promise.failure(new IOException(e.toString))
137+
138+
fileReader.onload = (_: UIEvent) =>
139+
promise.complete(Try(
140+
new Int8Array(fileReader.result.asInstanceOf[ArrayBuffer]).toArray
141+
))
142+
143+
fileReader.readAsArrayBuffer(file)
144+
145+
promise.future
146+
}
147+
148+
/**
149+
* Convert specified file to InputStream with blocking I/O
150+
*
151+
* Because it is using synchronous I/O that could potentially this API can be used only inside worker.
152+
*
153+
* This method is using FileReaderSync that is part of Working Draft File API.
154+
* Anyway it is supported for majority of modern browsers
155+
*/
156+
def asInputStream(file: File, bufferSize: Int = 1024): InputStream =
157+
new FileBufferedInputStream(file, bufferSize)
158+
}

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("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: 25 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._
@@ -9,8 +10,11 @@ object FileInputDemo extends AutoDemo with CssView {
910

1011
private val (rendered, source) = {
1112
import io.udash._
12-
import org.scalajs.dom.File
13+
import org.scalajs.dom.{File, URL}
1314
import scalatags.JsDom.all._
15+
import org.scalajs.dom.window
16+
17+
import scala.concurrent.ExecutionContext.Implicits.global
1418

1519
val acceptMultipleFiles = Property(true)
1620
val selectedFiles = SeqProperty.blank[File]
@@ -19,7 +23,26 @@ object FileInputDemo extends AutoDemo with CssView {
1923
FileInput(selectedFiles, acceptMultipleFiles)("files"),
2024
h4("Selected files"),
2125
ul(repeat(selectedFiles)(file => {
22-
li(file.get.name).render
26+
val content = Property(Array.empty[Byte])
27+
28+
FileService.asBytesArray(file.get) foreach { bytes =>
29+
content.set(bytes)
30+
}
31+
32+
val name = file.get.name
33+
li(showIfElse(content.transform(_.isEmpty))(
34+
span(name).render,
35+
{
36+
val url = FileService.createURL(content.get)
37+
val download = a(href := url.value, attr("download") := name)(name)
38+
val revoke = a(href := "#", onclick := { () =>
39+
content.set(Array.empty[Byte])
40+
url.close()
41+
})("revoke")
42+
43+
Seq(download, span(" or "), revoke).render
44+
}
45+
)).render
2346
}))
2447
)
2548
}.withSourceCode

0 commit comments

Comments
 (0)