|  | 
|  | 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 | +} | 
0 commit comments