|
| 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 | +sealed 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 asURL(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 asURL(bytesArrays: Seq[Array[Byte]]): String = |
| 99 | + asURL(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 asURL(byteArray: Array[Byte], mimeType: String): String = |
| 108 | + asURL(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 asURL(byteArray: Array[Byte]): String = |
| 117 | + asURL(Seq(byteArray), OctetStreamType) |
| 118 | + |
| 119 | + /** |
| 120 | + * Create an anchor element that on click downloads specified URL as a file with specified name. |
| 121 | + */ |
| 122 | + def asAnchor(filename: String, url: String): JsDom.TypedTag[Anchor] = { |
| 123 | + import JsDom.all._ |
| 124 | + |
| 125 | + val download = attr("download") |
| 126 | + a(href := url, download := filename) |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Create an anchor element that on click downloads byte array as a file with specified name. |
| 131 | + * |
| 132 | + * Keep in mind that anchor's href URL should be revoked via `org.scalajs.dom.revokeObjectURL(url)`. |
| 133 | + */ |
| 134 | + def asAnchor(filename: String, bytes: Array[Byte], mimeType: String = OctetStreamType): JsDom.TypedTag[Anchor] = |
| 135 | + asAnchor(filename, asURL(Seq(bytes), mimeType)) |
| 136 | + |
| 137 | + /** |
| 138 | + * Asynchronously convert specified file to bytes array. |
| 139 | + */ |
| 140 | + def asBytesArray(file: File): Future[Array[Byte]] = { |
| 141 | + import js.typedarray._ |
| 142 | + |
| 143 | + val fileReader = new FileReader() |
| 144 | + val promise = Promise[Array[Byte]]() |
| 145 | + |
| 146 | + fileReader.onerror = (e: Event) => |
| 147 | + promise.failure(new IOException(e.toString)) |
| 148 | + |
| 149 | + fileReader.onabort = (e: Event) => |
| 150 | + promise.failure(new IOException(e.toString)) |
| 151 | + |
| 152 | + fileReader.onload = (_: UIEvent) => |
| 153 | + promise.complete(Try( |
| 154 | + new Int8Array(fileReader.result.asInstanceOf[ArrayBuffer]).toArray |
| 155 | + )) |
| 156 | + |
| 157 | + fileReader.readAsArrayBuffer(file) |
| 158 | + |
| 159 | + promise.future |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Convert specified file to InputStream with blocking I/O |
| 164 | + * |
| 165 | + * Because it is using synchronous I/O that could potentially this API can be used only inside worker. |
| 166 | + * |
| 167 | + * This method is using FileReaderSync that is part of Working Draft File API. |
| 168 | + * Anyway it is supported for majority of modern browsers |
| 169 | + */ |
| 170 | + def asInputStream(file: File, bufferSize: Int = 1024): InputStream = |
| 171 | + new FileBufferedInputStream(file, bufferSize) |
| 172 | +} |
0 commit comments