|
| 1 | +package io.udash.utils |
| 2 | + |
| 3 | +import com.avsystem.commons.misc.AbstractCase |
| 4 | + |
| 5 | +import java.io.IOException |
| 6 | +import org.scalajs.dom._ |
| 7 | +import org.scalajs.dom.raw.Blob |
| 8 | + |
| 9 | +import scala.scalajs.js |
| 10 | +import scala.concurrent.{Future, Promise} |
| 11 | +import scala.scalajs.js.annotation.JSGlobal |
| 12 | +import scala.scalajs.js.typedarray.ArrayBuffer |
| 13 | +import scala.util.Try |
| 14 | + |
| 15 | +@js.native |
| 16 | +@JSGlobal |
| 17 | +private[utils] final class FileReaderSync() extends js.Object { |
| 18 | + def readAsArrayBuffer(blob: Blob): ArrayBuffer = js.native |
| 19 | +} |
| 20 | + |
| 21 | +final case class CloseableUrl(value: String) extends AbstractCase with AutoCloseable { |
| 22 | + override def close(): Unit = { |
| 23 | + URL.revokeObjectURL(value) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +object FileService { |
| 28 | + |
| 29 | + final val OctetStreamType = "application/octet-stream" |
| 30 | + |
| 31 | + /** |
| 32 | + * Converts specified bytes arrays to string that contains URL |
| 33 | + * that representing the array given in the parameter with specified mime-type. |
| 34 | + * |
| 35 | + * Keep in mind that returned URL should be closed. |
| 36 | + */ |
| 37 | + def createURL(bytesArrays: Seq[Array[Byte]], mimeType: String): CloseableUrl = { |
| 38 | + import js.typedarray._ |
| 39 | + |
| 40 | + val jsBytesArrays = js.Array[js.Any](bytesArrays.map(_.toTypedArray) :_ *) |
| 41 | + val blob = new Blob(jsBytesArrays, BlobPropertyBag(mimeType)) |
| 42 | + CloseableUrl(URL.createObjectURL(blob)) |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Converts specified bytes arrays to string that contains URL |
| 47 | + * that representing the array given in the parameter with `application/octet-stream` mime-type. |
| 48 | + * |
| 49 | + * Keep in mind that returned URL should be closed. |
| 50 | + */ |
| 51 | + def createURL(bytesArrays: Seq[Array[Byte]]): CloseableUrl = |
| 52 | + createURL(bytesArrays, OctetStreamType) |
| 53 | + |
| 54 | + /** |
| 55 | + * Converts specified bytes array to string that contains URL |
| 56 | + * that representing the array given in the parameter with specified mime-type. |
| 57 | + * |
| 58 | + * Keep in mind that returned URL should be closed. |
| 59 | + */ |
| 60 | + def createURL(byteArray: Array[Byte], mimeType: String): CloseableUrl = |
| 61 | + createURL(Seq(byteArray), mimeType) |
| 62 | + |
| 63 | + /** |
| 64 | + * Converts specified bytes array to string that contains URL |
| 65 | + * that representing the array given in the parameter with `application/octet-stream` mime-type. |
| 66 | + * |
| 67 | + * Keep in mind that returned URL should be closed. |
| 68 | + */ |
| 69 | + def createURL(byteArray: Array[Byte]): CloseableUrl = |
| 70 | + createURL(Seq(byteArray), OctetStreamType) |
| 71 | + |
| 72 | + /** |
| 73 | + * Asynchronously convert specified part of file to bytes array. |
| 74 | + */ |
| 75 | + def asBytesArray(file: File, start: Double, end: Double): Future[Array[Byte]] = { |
| 76 | + import js.typedarray._ |
| 77 | + |
| 78 | + val fileReader = new FileReader() |
| 79 | + val promise = Promise[Array[Byte]]() |
| 80 | + |
| 81 | + fileReader.onerror = (e: Event) => |
| 82 | + promise.failure(new IOException(e.toString)) |
| 83 | + |
| 84 | + fileReader.onabort = (e: Event) => |
| 85 | + promise.failure(new IOException(e.toString)) |
| 86 | + |
| 87 | + fileReader.onload = (_: UIEvent) => |
| 88 | + promise.complete(Try( |
| 89 | + new Int8Array(fileReader.result.asInstanceOf[ArrayBuffer]).toArray |
| 90 | + )) |
| 91 | + |
| 92 | + val slice = file.slice(start, end) |
| 93 | + fileReader.readAsArrayBuffer(slice) |
| 94 | + |
| 95 | + promise.future |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Asynchronously convert specified file to bytes array. |
| 100 | + */ |
| 101 | + def asBytesArray(file: File): Future[Array[Byte]] = |
| 102 | + asBytesArray(file, 0, file.size) |
| 103 | + |
| 104 | + /** |
| 105 | + * Synchronously convert specified part of file to bytes array. |
| 106 | + * |
| 107 | + * Because it is using synchronous I/O this API can be used only inside worker. |
| 108 | + * |
| 109 | + * This method is using FileReaderSync that is part of Working Draft File API. |
| 110 | + * Anyway it is supported for majority of modern browsers |
| 111 | + */ |
| 112 | + def asSyncBytesArray(file: File, start: Double, end: Double): Array[Byte] = { |
| 113 | + import js.typedarray._ |
| 114 | + |
| 115 | + val fileReaderSync = new FileReaderSync() |
| 116 | + val slice = file.slice(start, end) |
| 117 | + |
| 118 | + val int8Array = new Int8Array(fileReaderSync.readAsArrayBuffer(slice)) |
| 119 | + |
| 120 | + int8Array.toArray |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Synchronously convert file to bytes array. |
| 125 | + * |
| 126 | + * Because it is using synchronous I/O this API can be used only inside worker. |
| 127 | + * |
| 128 | + * This method is using FileReaderSync that is part of Working Draft File API. |
| 129 | + * Anyway it is supported for majority of modern browsers |
| 130 | + */ |
| 131 | + def asSyncBytesArray(file: File): Array[Byte] = |
| 132 | + asSyncBytesArray(file, 0, file.size) |
| 133 | +} |
0 commit comments