Unverified Commit 2f434ee7 authored by Sebastian Schüpbach's avatar Sebastian Schüpbach
Browse files

get mimetype from metadata record

parent e2d7a847
Pipeline #14171 passed with stages
in 15 minutes and 29 seconds
......@@ -39,18 +39,17 @@ class FedoraClientWrapper(fc: FedoraClient) {
import FedoraClientWrapper.{copyDataAndCloseResource, getMediaFileType}
/**
* Fetches a binary resource from Fedora and returns, if successful, a [[FileWithMetadata]] instance
* Fetches a binary resource from Fedora and returns, if successful, a [[ByteArrayOutputStream]] instance
*
* @param url The URL to the resource
* @return
*/
def fetchBinaryResource(url: String): Try[FileWithMetadata] = {
def fetchBinaryResource(url: String): Try[ByteArrayOutputStream] = {
for {
u <- Try(new URI(url))
resource <- Try(fc.fetchBinaryResource(u))
mediaFileType <- getMediaFileType(resource.getMimetype)
outputStream <- copyDataAndCloseResource(resource)
} yield models.FileWithMetadata(outputStream, mediaFileType)
} yield outputStream
}
}
......
......@@ -44,14 +44,14 @@ class RecordProcessor(fileHandler: DisseminationCopyHandler, fedoraClientWrapper
}
def process(record: ConsumerRecord[String, String]): ProcessOutcome = {
val test = (for {
(for {
kafkaMsg <- BinaryResourceMetadata.build(record.value(), externalBaseUrl)
fileWithMetadata <- fedoraClientWrapper.fetchBinaryResource(kafkaMsg.filePath)
} yield createProcessResult(kafkaMsg.id, kafkaMsg.eventType, fileWithMetadata.fileType, fileWithMetadata.data))
test
data <- fedoraClientWrapper.fetchBinaryResource(kafkaMsg.filePath)
} yield createProcessResult(kafkaMsg.id, kafkaMsg.eventType, kafkaMsg.fileType, data))
.recover {
case e: NoLocalBinary => ProcessIgnore(record.key(), e.getMessage)
case e: NoDigitalObject => ProcessIgnore(record.key(), e.getMessage)
case e: UnmanageableMediaFileType => ProcessIgnore(record.key(), e.getMessage)
case e: Exception => ProcessFailure(record.key(), UnknownFileType, "", e)
}
}.get
......
......@@ -35,7 +35,7 @@ import scala.util.{Success, Try}
*/
case class BinaryResourceMetadata(id: String,
filePath: String,
fileType: String,
fileType: MediaFileType,
eventType: Event) {
}
......@@ -73,7 +73,7 @@ object BinaryResourceMetadata {
BinaryResourceMetadata(
obj("@id").str.substring(s"$baseUrl/digital/".length),
obj("locator").str,
obj("hasMimeType").str,
Conversions.getMediaFileType(obj("hasMimeType").str).get,
eventType)
case None => throw new NoEventType
......
/*
* Media Converter
* Extracts media files from Fedora repository
* Copyright (C) 2020 Memoriav
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package ch.memobase.models
import java.io.ByteArrayOutputStream
/**
* Represents binary data with respective file type meant to be handled by media transformation tools
*
* @param data binary data
* @param fileType respective file type
*/
case class FileWithMetadata(data: ByteArrayOutputStream, fileType: MediaFileType)
......@@ -39,7 +39,7 @@ class BinaryResourceMetadataTest extends AnyFunSuite {
}
test("the value of the id field of a KafkaMessage should match the id of the parsed object") {
val km = BinaryResourceMetadata.build(loadMessageWithBinaryResource("Create", "image/jpeg", "https://memobase.ch/digital/BAZ-MEI_77466-1/binary"), externalBaseUrl)
val km = BinaryResourceMetadata.build(loadMessageWithBinaryResource("Create", "image/png", "https://memobase.ch/digital/BAZ-MEI_77466-1/binary"), externalBaseUrl)
assert(km.isSuccess)
}
......
......@@ -85,7 +85,7 @@ class RecordProcessorTest extends AnyFunSuite with MockFactory {
val mockDCH = f.mockDisseminationCopyHandler
val mockFCW = f.mockFedoraClientWrapper
val (incomingMessage, baos) = createIncomingMessage(Create, JpegFile)
(mockFCW.fetchBinaryResource _).expects(f.pathToResource).returning(Success(FileWithMetadata(baos, JpegFile)))
(mockFCW.fetchBinaryResource _).expects(f.pathToResource).returning(Success(baos))
(mockDCH.createImageCopy _).expects(baos, f.resourceId, JpegFile)
val rP = new RecordProcessor(mockDCH, mockFCW, f.externalBaseUrl)
val cR = new ConsumerRecord[String, String]("_void", 1, 0, "1", incomingMessage)
......@@ -97,7 +97,7 @@ class RecordProcessorTest extends AnyFunSuite with MockFactory {
val mockDCH = f.mockDisseminationCopyHandler
val mockFCW = f.mockFedoraClientWrapper
val (incomingMessage, baos) = createIncomingMessage(Delete, Mp3File)
(mockFCW.fetchBinaryResource _).expects(f.pathToResource).returning(Success(FileWithMetadata(baos, Mp3File)))
(mockFCW.fetchBinaryResource _).expects(f.pathToResource).returning(Success(baos))
(mockDCH.deleteAudioCopy _).expects(f.resourceId)
val rP = new RecordProcessor(mockDCH, mockFCW, f.externalBaseUrl)
val cR = new ConsumerRecord[String, String]("_void", 1, 0, "1", incomingMessage)
......
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