-
Notifications
You must be signed in to change notification settings - Fork 37
FileService: a way to work with file at frontend #656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
core/.js/src/main/scala/io/udash/utils/FileService.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package io.udash.utils | ||
|
||
import com.avsystem.commons.misc.AbstractCase | ||
|
||
import java.io.IOException | ||
import org.scalajs.dom._ | ||
import org.scalajs.dom.raw.Blob | ||
|
||
import scala.scalajs.js | ||
import scala.concurrent.{Future, Promise} | ||
import scala.scalajs.js.annotation.JSGlobal | ||
import scala.scalajs.js.typedarray.ArrayBuffer | ||
import scala.util.Try | ||
|
||
@js.native | ||
@JSGlobal | ||
private[utils] final class FileReaderSync() extends js.Object { | ||
def readAsArrayBuffer(blob: Blob): ArrayBuffer = js.native | ||
} | ||
|
||
final case class CloseableUrl(value: String) extends AbstractCase with AutoCloseable { | ||
override def close(): Unit = { | ||
URL.revokeObjectURL(value) | ||
} | ||
} | ||
|
||
object FileService { | ||
|
||
final val OctetStreamType = "application/octet-stream" | ||
|
||
/** | ||
* Converts specified bytes arrays to string that contains URL | ||
* that representing the array given in the parameter with specified mime-type. | ||
* | ||
* Keep in mind that returned URL should be closed. | ||
*/ | ||
def createURL(bytesArrays: Seq[Array[Byte]], mimeType: String): CloseableUrl = { | ||
import js.typedarray._ | ||
|
||
val jsBytesArrays = js.Array[js.Any](bytesArrays.map(_.toTypedArray) :_ *) | ||
val blob = new Blob(jsBytesArrays, BlobPropertyBag(mimeType)) | ||
CloseableUrl(URL.createObjectURL(blob)) | ||
} | ||
|
||
/** | ||
* Converts specified bytes arrays to string that contains URL | ||
* that representing the array given in the parameter with `application/octet-stream` mime-type. | ||
* | ||
* Keep in mind that returned URL should be closed. | ||
*/ | ||
def createURL(bytesArrays: Seq[Array[Byte]]): CloseableUrl = | ||
createURL(bytesArrays, OctetStreamType) | ||
|
||
/** | ||
* Converts specified bytes array to string that contains URL | ||
* that representing the array given in the parameter with specified mime-type. | ||
* | ||
* Keep in mind that returned URL should be closed. | ||
*/ | ||
def createURL(byteArray: Array[Byte], mimeType: String): CloseableUrl = | ||
createURL(Seq(byteArray), mimeType) | ||
|
||
/** | ||
* Converts specified bytes array to string that contains URL | ||
* that representing the array given in the parameter with `application/octet-stream` mime-type. | ||
* | ||
* Keep in mind that returned URL should be closed. | ||
*/ | ||
def createURL(byteArray: Array[Byte]): CloseableUrl = | ||
createURL(Seq(byteArray), OctetStreamType) | ||
|
||
/** | ||
* Asynchronously convert specified part of file to bytes array. | ||
*/ | ||
def asBytesArray(file: File, start: Double, end: Double): Future[Array[Byte]] = { | ||
import js.typedarray._ | ||
|
||
val fileReader = new FileReader() | ||
val promise = Promise[Array[Byte]]() | ||
|
||
fileReader.onerror = (e: Event) => | ||
promise.failure(new IOException(e.toString)) | ||
|
||
fileReader.onabort = (e: Event) => | ||
promise.failure(new IOException(e.toString)) | ||
|
||
fileReader.onload = (_: UIEvent) => | ||
promise.complete(Try( | ||
new Int8Array(fileReader.result.asInstanceOf[ArrayBuffer]).toArray | ||
)) | ||
|
||
catap marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val slice = file.slice(start, end) | ||
fileReader.readAsArrayBuffer(slice) | ||
|
||
promise.future | ||
} | ||
|
||
/** | ||
* Asynchronously convert specified file to bytes array. | ||
*/ | ||
def asBytesArray(file: File): Future[Array[Byte]] = | ||
asBytesArray(file, 0, file.size) | ||
|
||
/** | ||
* Synchronously convert specified part of file to bytes array. | ||
* | ||
* Because it is using synchronous I/O this API can be used only inside worker. | ||
* | ||
* This method is using FileReaderSync that is part of Working Draft File API. | ||
* Anyway it is supported for majority of modern browsers | ||
*/ | ||
def asBytesArraySync(file: File, start: Double, end: Double): Array[Byte] = { | ||
import js.typedarray._ | ||
|
||
val fileReaderSync = new FileReaderSync() | ||
val slice = file.slice(start, end) | ||
|
||
val int8Array = new Int8Array(fileReaderSync.readAsArrayBuffer(slice)) | ||
|
||
int8Array.toArray | ||
} | ||
|
||
/** | ||
* Synchronously convert file to bytes array. | ||
* | ||
* Because it is using synchronous I/O this API can be used only inside worker. | ||
* | ||
* This method is using FileReaderSync that is part of Working Draft File API. | ||
* Anyway it is supported for majority of modern browsers | ||
*/ | ||
def asBytesArraySync(file: File): Array[Byte] = | ||
asBytesArraySync(file, 0, file.size) | ||
} | ||
catap marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.