Skip to content

Commit ccad45c

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 convert any `Array[Byte]` from frontend to URL as simple call `FileService.bytesAsURL()`, create an anchor to download it as `FileService.anchor()` and asynchronously convert any uploaded file to `Array[Byte]` as `FileService.convert()`. 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 d67eb82 commit ccad45c

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
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 anchor(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 convert(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+
}

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 fronted ",
28+
" and asynchronously load file to bytes array. "),
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: 12 additions & 1 deletion
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,24 @@ object FileInputDemo extends AutoDemo with CssView {
1213
import org.scalajs.dom.File
1314
import scalatags.JsDom.all._
1415

16+
import scala.concurrent.ExecutionContext.Implicits.global
17+
1518
val acceptMultipleFiles = Property(true)
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+
FileService.convert(file.get) foreach { bytes =>
27+
content.set(bytes)
28+
}
29+
val name = file.get.name
30+
li(showIfElse(content.transform(_.isEmpty))(
31+
span(name).render,
32+
FileService.anchor(name, content.get)(name).render
33+
)).render
2334
}))
2435
)
2536
}.withSourceCode

0 commit comments

Comments
 (0)