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.
// 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
import scala.collection.mutable.ArrayBuffer
import scala.util.Try
/**
* Essential information on a binary file residing in Fedora
*
* @param id Identifier of the binary file
* @param filePath File path (URL) to resource
* @param mimeType MIME type
* @param resource Type of instantiation
*/
case class BinaryResourceMetadata(id: String,
filePath: String,
mimeType: MimeType,
resource: MemobaseResource) {
}
/** Essential information on a binary file residing in Fedora
*
* @param id
* Identifier of the binary file
* @param filePath
* File path (URL) to resource
* @param mimeType
* MIME type
* @param resource
* Type of instantiation
*/
case class BinaryResourceMetadata(
id: String,
filePath: String,
mimeType: MimeType,
resource: MemobaseResource
) {}
object BinaryResourceMetadata extends RecordUtils {
/**
* Builds a `BinaryResourceMetadata` object from a JSON-LD object pulled from Kafka topic
*
* @param msg Pulled Kafka message
* @param externalBaseUrl Base URL of resource used outside of Fedora
* @param distributorHost Host and port of media distributor service
* @return
*/
def build(msg: String, externalBaseUrl: String, distributorHost: String): List[Try[BinaryResourceMetadata]] = {
/** Builds a `BinaryResourceMetadata` object from a JSON-LD object pulled from
* Kafka topic
*
* @param msg
* Pulled Kafka message
* @param externalBaseUrl
* Base URL of resource used outside of Fedora
* @param distributorHost
* Host and port of media distributor service
* @return
*/
def build(
msg: String,
externalBaseUrl: String,
distributorHost: String
): List[Try[BinaryResourceMetadata]] = {
val jsonldGraph = getJsonldGraph(msg)
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 {
case DigitalObject =>
s"${distributorHost.stripSuffix("/")}/media/$fileName"
......@@ -67,21 +81,32 @@ object BinaryResourceMetadata extends RecordUtils {
}
//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
.withFilter {
v => isDigitalObject(v.obj) || isPreviewImage(v.obj)
.withFilter { v =>
isDigitalObject(v.obj) || isPreviewImage(v.obj)
}
.map {
v => {
.map { v =>
{
val resourceId = v.obj.getOrElse("@id", Value("<unknown id>")).str
Try(
v.obj match {
case v if isLocalRecord(v) && isProcessableMimeType(v) =>
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 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)
s"$recordSetId-$basename"
}
......@@ -89,15 +114,20 @@ object BinaryResourceMetadata extends RecordUtils {
binaryObjectId,
buildDistributorUrl(locator, distributorHost, instantiation),
Conversions.getMediaFileType(v("hasMimeType").str).get,
instantiation)
instantiation
)
case v if isLocalRecord(v) =>
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 =>
val resource = MemobaseResource(v("type").str)
throw new NoLocalBinary(resourceId, resource)
}
)
}
}.toList
}
.toList
}
Markdown is supported
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