Skip to content

Commit 5e2b8ed

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 5e2b8ed

File tree

3 files changed

+181
-2
lines changed

3 files changed

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

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, attr("download") := name)(name)
38+
val revoke = a(onclick := { () =>
39+
content.set(Array.empty[Byte])
40+
URL.revokeObjectURL(url)
41+
})("revoke")
42+
43+
Seq(download, revoke).render
44+
}
45+
)).render
2346
}))
2447
)
2548
}.withSourceCode

0 commit comments

Comments
 (0)