Skip to content

Commit

Permalink
Merge pull request #1086 from UdashFramework/file-uploader-auth
Browse files Browse the repository at this point in the history
Added authorization header support for FileUploader.
  • Loading branch information
ddworak authored May 8, 2023
2 parents 13e652f + 35b206e commit 5cfbcf3
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions core/.js/src/main/scala/io/udash/utils/FileUploader.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package io.udash.utils

import com.avsystem.commons.misc.AbstractCase
import com.avsystem.commons.misc.{AbstractCase, CaseMethods}
import io.udash._
import io.udash.properties.{Blank, HasModelPropertyCreator, PropertyCreator}
import org.scalajs.dom._

import scala.scalajs.js

class FileUploader(url: Url) {

import FileUploader._

/** Uploads files selected in provided `input`. */
Expand All @@ -17,16 +18,15 @@ class FileUploader(url: Url) {
(0 until input.files.length).map(input.files.item)
)

/** Uploads provided `file` in a field named `fieldName`. */
/** Uploads provided `file` in a field named `fieldName` with optional additional request headers. */
def uploadFile(
fieldName: String, file: File, extraData: Map[js.Any, js.Any] = Map.empty
): ReadableModelProperty[FileUploadModel] = {
upload(fieldName, Seq(file), extraData)
}
fieldName: String, file: File, extraData: Map[js.Any, js.Any] = Map.empty, additionalRequestHeaders: Map[RequestName, RequestValue] = Map.empty
): ReadableModelProperty[FileUploadModel] =
upload(fieldName, Seq(file), extraData = extraData, additionalRequestHeaders = additionalRequestHeaders)

/** Uploads provided `files` in a field named `fieldName`. */
/** Uploads provided `files` in a field named `fieldName` with optional additional request headers. */
def upload(
fieldName: String, files: Seq[File], extraData: Map[js.Any, js.Any] = Map.empty
fieldName: String, files: Seq[File], extraData: Map[js.Any, js.Any] = Map.empty, additionalRequestHeaders: Map[RequestName, RequestValue] = Map.empty
): ReadableModelProperty[FileUploadModel] = {
val p = ModelProperty[FileUploadModel](
new FileUploadModel(Seq.empty, FileUploadState.InProgress, 0, 0, None)
Expand All @@ -47,26 +47,28 @@ class FileUploader(url: Url) {
}
)
xhr.addEventListener("load", (_: Event) => {
p.subProp(_.response).set(Some(new HttpResponse(xhr)))
p.subProp(_.state).set(xhr.status / 100 match {
case 2 => FileUploadState.Completed
case _ => FileUploadState.Failed
})
}
)
p.subProp(_.response).set(Some(new HttpResponse(xhr)))
p.subProp(_.state).set(xhr.status / 100 match {
case 2 => FileUploadState.Completed
case _ => FileUploadState.Failed
})
})
xhr.addEventListener("error", (_: Event) =>
p.subProp(_.state).set(FileUploadState.Failed)
)
xhr.addEventListener("abort", (_: Event) =>
p.subProp(_.state).set(FileUploadState.Cancelled)
)
xhr.open(method = "POST", url = url.value)
additionalRequestHeaders.foreach { case (name, value) => xhr.setRequestHeader(name.name, value.value) }
xhr.send(data)

p
}
}

final case class RequestName(name: String) extends AnyVal with CaseMethods
final case class RequestValue(value: String) extends AnyVal with CaseMethods

object FileUploader {
sealed trait FileUploadState extends AbstractCase
object FileUploadState {
Expand Down

0 comments on commit 5cfbcf3

Please sign in to comment.