Permalink
Browse files

Extracted loadDataURL and loadText from ImageUtility into FileUtility…

… for more generic utilization
  • Loading branch information...
darkfrog26 committed Jan 8, 2019
1 parent e41aa98 commit 4d496d03fd0b2e271a399e7f217335460f82b4e5
@@ -5,7 +5,7 @@ import io.youi.drawable.{Context, Drawable}
import io.youi.image.resize.ImageResizer
import io.youi.net.URL
import io.youi.spatial.{BoundingBox, Size}
import io.youi.util.{CanvasPool, ImageUtility}
import io.youi.util.{CanvasPool, FileUtility, ImageUtility}
import org.scalajs.dom._

import scala.concurrent.ExecutionContext.Implicits.global
@@ -70,10 +70,10 @@ object Image {
}

def apply(file: File): Future[Image] = file.`type` match {
case "image/svg+xml" => ImageUtility.loadText(file).flatMap { svgString =>
case "image/svg+xml" => FileUtility.loadText(file).flatMap { svgString =>
SVGImage(svgString)
}
case _ => ImageUtility.loadDataURL(file).flatMap(apply)
case _ => FileUtility.loadDataURL(file).flatMap(apply)
}

def isImage(file: File): Boolean = file.`type`.startsWith("image/")
@@ -0,0 +1,35 @@
package io.youi.util

import org.scalajs.dom.Event
import org.scalajs.dom.raw.{File, FileReader, URL}

import scala.concurrent.{Future, Promise}
import scribe.Execution.global

object FileUtility {
def loadDataURL(file: File, useFileReader: Boolean = false): Future[String] = if (useFileReader) {
val reader = new FileReader
val promise = Promise[String]
reader.addEventListener("load", (evt: Event) => {
promise.success(reader.result.asInstanceOf[String])
})
reader.readAsDataURL(file)
val future = promise.future
future.failed.foreach(scribe.error(_))
future
} else {
Future.successful(URL.createObjectURL(file))
}

def loadText(file: File): Future[String] = {
val reader = new FileReader
val promise = Promise[String]
reader.addEventListener("load", (_: Event) => {
promise.success(reader.result.asInstanceOf[String])
})
reader.readAsText(file)
val future = promise.future
future.failed.foreach(scribe.error(_))
future
}
}
@@ -68,7 +68,7 @@ object ImageUtility {
val promise = Promise[Unit]
val listener: js.Function1[Event, _] = (_: Event) => promise.success(())
tempImage.addEventListener("load", listener)
loadDataURL(file).foreach(tempImage.src = _)
FileUtility.loadDataURL(file).foreach(tempImage.src = _)
promise.future.flatMap { _ =>
tempImage.removeEventListener("load", listener)
val f = process(tempImage)
@@ -103,32 +103,6 @@ object ImageUtility {
}
}

def loadDataURL(file: File, useFileReader: Boolean = false): Future[String] = if (useFileReader) {
val reader = new FileReader
val promise = Promise[String]
reader.addEventListener("load", (evt: Event) => {
promise.success(reader.result.asInstanceOf[String])
})
reader.readAsDataURL(file)
val future = promise.future
future.failed.foreach(scribe.error(_))
future
} else {
Future.successful(URL.createObjectURL(file))
}

def loadText(file: File): Future[String] = {
val reader = new FileReader
val promise = Promise[String]
reader.addEventListener("load", (_: Event) => {
promise.success(reader.result.asInstanceOf[String])
})
reader.readAsText(file)
val future = promise.future
future.failed.foreach(scribe.error(_))
future
}

def preview(file: File): Future[Option[Image]] = if (Video.isVideo(file)) {
Video(file, autoPlay = false, loop = false, muted = true).flatMap { video =>
video.seek(video.duration / 2.0).map { _ =>

0 comments on commit 4d496d0

Please sign in to comment.