FedoraClientWrapper.scala 3.14 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
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}

  /**
42
   * Fetches a binary resource from Fedora and returns, if successful, a [[ByteArrayOutputStream]] instance
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
43
44
45
46
   *
   * @param url The URL to the resource
   * @return
   */
47
  def fetchBinaryResource(url: String): Try[ByteArrayOutputStream] = {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
48
49
50
51
    for {
      u <- Try(new URI(url))
      resource <- Try(fc.fetchBinaryResource(u))
      outputStream <- copyDataAndCloseResource(resource)
52
    } yield outputStream
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  }
}

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)
  }

77
78
79
80
81
82
  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
83
84

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