BinaryResourceMetadata.scala 3.56 KB
Newer Older
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
1
/*
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
2
 * Media Converter
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 * 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

22
import ch.memobase.RecordUtils
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
23
import ujson.Value
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
24
25

import scala.collection.mutable.ArrayBuffer
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
26
import scala.util.Try
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
27
28
29
30
31


/**
 * Essential information on a binary file residing in Fedora
 *
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
32
33
34
35
36
 * @param id        Identifier of the binary file
 * @param filePath  File path (URL) to resource
 * @param mimeType  MIME type
 * @param resource  Type of instantiation
 * @param eventType Action to be performed on the copy of the file
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
37
38
39
 */
case class BinaryResourceMetadata(id: String,
                                  filePath: String,
40
                                  mimeType: MimeType,
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
41
                                  resource: MemobaseResource,
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
42
43
44
                                  eventType: Event) {
}

45
object BinaryResourceMetadata extends RecordUtils {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
46
47
48
49
50
51
52
53

  /**
   * 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
   * @return
   */
54
  def build(msg: String, externalBaseUrl: String): List[Try[BinaryResourceMetadata]] = {
55
    val jsonldGraph = getJsonldGraph(msg)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
56
    extractBinaryResourceMetadata(jsonldGraph, externalBaseUrl)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
57
58
  }

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
59
  //noinspection ScalaStyle
60
61
62
63
64
  private def extractBinaryResourceMetadata(jsonldGraph: ArrayBuffer[Value], baseUrl: String): List[Try[BinaryResourceMetadata]] =
    jsonldGraph.value
      .withFilter {
        v => isDigitalObject(v.obj) || isPreviewImage(v.obj)
      }
65
66
      .map { v => {
        val id = v.obj.getOrElse("@id", Value("<unknown id>")).str
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
67
        Try(
68
69
70
71
72
          v.obj match {
            case v if isLocalRecord(v, baseUrl) && isProcessableMimeType(v) =>
              getEventType(jsonldGraph) match {
                case Some(UnknownEvent(e)) => throw new UnknownEventType(s"Event type `$e` for $id not known")
                case Some(eventType) =>
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
73
                  val instantiation = MemobaseResource(v("type").str)
74
75
76
                  BinaryResourceMetadata(
                    v("@id").str.substring(s"$baseUrl/digital/".length - 1),
                    v("locator").str,
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
77
78
79
80
81
                    if (instantiation == Thumbnail) {
                      JpegFile
                    } else {
                      Conversions.getMediaFileType(v("hasMimeType").str).get
                    },
82
83
84
85
                    instantiation,
                    eventType)
                case None => throw new NoEventType(id)
              }
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
86
87
88
89
90
91
            case v if isLocalRecord(v, baseUrl) =>
              val resource = MemobaseResource(v("type").str)
              throw new UnmanageableMediaFileType(s"Media file type for $id unknown", resource)
            case v =>
              val resource = MemobaseResource(v("type").str)
              throw new NoLocalBinary(id, resource)
92
          }
93
        )
94
95

      }
96
      }.toList
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
97
}