FedoraClientWrapper.scala 3.23 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
22
23
24
25
26
27
 * 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

import java.io.ByteArrayOutputStream
import java.net.URI

import ch.memobase.models._
import org.memobase.fedora.{BinaryResource, FedoraClient, FedoraClientImpl}

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
28
import scala.language.postfixOps
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import scala.util.{Failure, Success, Try}


/**
 * Acts as a thin wrapper around a [[org.memobase.fedora.FedoraClient]] instance
 *
 * @param fc Wrapped [[org.memobase.fedora.FedoraClient]] instance
 */
class FedoraClientWrapper(fc: FedoraClient) {

  import FedoraClientWrapper.{copyDataAndCloseResource, getMediaFileType}

  /**
   * Fetches a binary resource from Fedora and returns, if successful, a [[FileWithMetadata]] instance
   *
   * @param url The URL to the resource
   * @return
   */
  def fetchBinaryResource(url: String): Try[FileWithMetadata] = {
    for {
      u <- Try(new URI(url))
      resource <- Try(fc.fetchBinaryResource(u))
      mediaFileType <- getMediaFileType(resource.getMimetype)
      outputStream <- copyDataAndCloseResource(resource)
    } yield models.FileWithMetadata(outputStream, mediaFileType)
  }
}

object FedoraClientWrapper {
  /**
   * Creates a [[FedoraClientWrapper]] instance
   *
   * @param interalBaseUrl  Base URL used internally by Fedora
   * @param externalBaseUrl Base URL used outside of Fedora
   * @param fedoraUsername  Username used to query Fedora
   * @param fedoraPassword  Password used to query Fedora
   * @return
   */
  def apply(interalBaseUrl: String,
            externalBaseUrl: String,
            fedoraUsername: String,
            fedoraPassword: String): FedoraClientWrapper = {
    val builder = FedoraClientImpl.builder()
    builder.urls(interalBaseUrl, externalBaseUrl)
    builder.credentials(fedoraUsername, fedoraPassword)
    val fc = builder.build()
    new FedoraClientWrapper(fc)
  }

78
79
80
81
82
83
  private def getMediaFileType(mimeType: String): Try[MediaFileType] =
    Conversions.getMediaFileType(mimeType) match {
      case Some(ft) => Success(ft)
      case None =>
        Failure(new UnmanageableMediaFileType(s"Can't handle files of type $mimeType"))
    }
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
84
85

  private def copyDataAndCloseResource(binaryResource: BinaryResource): Try[ByteArrayOutputStream] = {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
86
    val dataByteLength = 2097152
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
87
88
    val outputStream: Try[ByteArrayOutputStream] = Try {
      val oS = new ByteArrayOutputStream()
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
89
      val data = new Array[Byte](dataByteLength)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
90
91
92
93
94
95
96
97
98
99
      Iterator.continually(binaryResource.getData.read(data))
        .takeWhile(-1 !=)
        .foreach(oS.write)
      oS
    }
    binaryResource.close()
    outputStream
  }
}