Verified Commit 7f60054f authored by Sebastian Schüpbach's avatar Sebastian Schüpbach
Browse files

fix file name trunk extraction

parent 9bb32724
Pipeline #44484 passed with stages
in 7 minutes and 1 second
version = "3.0.5"
runner.dialect = scala213
\ No newline at end of file
// DO NOT EDIT! This file is auto-generated. // DO NOT EDIT! This file is auto-generated.
// This file enables sbt-bloop to create bloop config files. // This file enables sbt-bloop to create bloop config files.
addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.4.3-23-550c6c0a") addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.4.12")
...@@ -25,37 +25,51 @@ import ujson.Value ...@@ -25,37 +25,51 @@ import ujson.Value
import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer
import scala.util.Try import scala.util.Try
/** Essential information on a binary file residing in Fedora
/** *
* Essential information on a binary file residing in Fedora * @param id
* * Identifier of the binary file
* @param id Identifier of the binary file * @param filePath
* @param filePath File path (URL) to resource * File path (URL) to resource
* @param mimeType MIME type * @param mimeType
* @param resource Type of instantiation * MIME type
*/ * @param resource
case class BinaryResourceMetadata(id: String, * Type of instantiation
filePath: String, */
mimeType: MimeType, case class BinaryResourceMetadata(
resource: MemobaseResource) { id: String,
} filePath: String,
mimeType: MimeType,
resource: MemobaseResource
) {}
object BinaryResourceMetadata extends RecordUtils { object BinaryResourceMetadata extends RecordUtils {
/** /** Builds a `BinaryResourceMetadata` object from a JSON-LD object pulled from
* Builds a `BinaryResourceMetadata` object from a JSON-LD object pulled from Kafka topic * Kafka topic
* *
* @param msg Pulled Kafka message * @param msg
* @param externalBaseUrl Base URL of resource used outside of Fedora * Pulled Kafka message
* @param distributorHost Host and port of media distributor service * @param externalBaseUrl
* @return * Base URL of resource used outside of Fedora
*/ * @param distributorHost
def build(msg: String, externalBaseUrl: String, distributorHost: String): List[Try[BinaryResourceMetadata]] = { * Host and port of media distributor service
* @return
*/
def build(
msg: String,
externalBaseUrl: String,
distributorHost: String
): List[Try[BinaryResourceMetadata]] = {
val jsonldGraph = getJsonldGraph(msg) val jsonldGraph = getJsonldGraph(msg)
extractBinaryResourceMetadata(jsonldGraph, externalBaseUrl, distributorHost) extractBinaryResourceMetadata(jsonldGraph, externalBaseUrl, distributorHost)
} }
private def buildDistributorUrl(fileName: String, distributorHost: String, resourceType: MemobaseResource): String = { private def buildDistributorUrl(
fileName: String,
distributorHost: String,
resourceType: MemobaseResource
): String = {
resourceType match { resourceType match {
case DigitalObject => case DigitalObject =>
s"${distributorHost.stripSuffix("/")}/media/$fileName" s"${distributorHost.stripSuffix("/")}/media/$fileName"
...@@ -67,21 +81,32 @@ object BinaryResourceMetadata extends RecordUtils { ...@@ -67,21 +81,32 @@ object BinaryResourceMetadata extends RecordUtils {
} }
//noinspection ScalaStyle //noinspection ScalaStyle
private def extractBinaryResourceMetadata(jsonldGraph: ArrayBuffer[Value], baseUrl: String, distributorHost: String): List[Try[BinaryResourceMetadata]] = private def extractBinaryResourceMetadata(
jsonldGraph: ArrayBuffer[Value],
baseUrl: String,
distributorHost: String
): List[Try[BinaryResourceMetadata]] =
jsonldGraph.value jsonldGraph.value
.withFilter { .withFilter { v =>
v => isDigitalObject(v.obj) || isPreviewImage(v.obj) isDigitalObject(v.obj) || isPreviewImage(v.obj)
} }
.map { .map { v =>
v => { {
val resourceId = v.obj.getOrElse("@id", Value("<unknown id>")).str val resourceId = v.obj.getOrElse("@id", Value("<unknown id>")).str
Try( Try(
v.obj match { v.obj match {
case v if isLocalRecord(v) && isProcessableMimeType(v) => case v if isLocalRecord(v) && isProcessableMimeType(v) =>
val instantiation = MemobaseResource(v("type").str) val instantiation = MemobaseResource(v("type").str)
val binaryObjectId = v("@id").str.substring(s"$baseUrl/digital/".length - 1) val binaryObjectId =
v("@id").str.substring(s"$baseUrl/digital/".length - 1)
val locator = { val locator = {
val basename = v("locator").str.split("/").last.split("\\.")(0).replaceAll(" ", "_") val basename = {
val filename = v("locator").str.split("/").last
filename
.splitAt(filename.lastIndexOf("."))
._1
.replaceAll(" ", "_")
}
val recordSetId = binaryObjectId.substring(0, 7) val recordSetId = binaryObjectId.substring(0, 7)
s"$recordSetId-$basename" s"$recordSetId-$basename"
} }
...@@ -89,15 +114,20 @@ object BinaryResourceMetadata extends RecordUtils { ...@@ -89,15 +114,20 @@ object BinaryResourceMetadata extends RecordUtils {
binaryObjectId, binaryObjectId,
buildDistributorUrl(locator, distributorHost, instantiation), buildDistributorUrl(locator, distributorHost, instantiation),
Conversions.getMediaFileType(v("hasMimeType").str).get, Conversions.getMediaFileType(v("hasMimeType").str).get,
instantiation) instantiation
)
case v if isLocalRecord(v) => case v if isLocalRecord(v) =>
val resource = MemobaseResource(v("type").str) val resource = MemobaseResource(v("type").str)
throw new UnmanageableMediaFileType(s"Media file type for $resourceId unknown", resource) throw new UnmanageableMediaFileType(
s"Media file type for $resourceId unknown",
resource
)
case v => case v =>
val resource = MemobaseResource(v("type").str) val resource = MemobaseResource(v("type").str)
throw new NoLocalBinary(resourceId, resource) throw new NoLocalBinary(resourceId, resource)
} }
) )
} }
}.toList }
.toList
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment