FedoraClientWrapper.scala 3.14 KB
Newer Older
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
1
2
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
28
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
/*
 * 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}

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

76
77
78
79
80
81
  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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

  private def copyDataAndCloseResource(binaryResource: BinaryResource): Try[ByteArrayOutputStream] = {
    val outputStream: Try[ByteArrayOutputStream] = Try {
      val oS = new ByteArrayOutputStream()
      val data = new Array[Byte](2097152)
      Iterator.continually(binaryResource.getData.read(data))
        .takeWhile(-1 !=)
        .foreach(oS.write)
      oS
    }
    binaryResource.close()
    outputStream
  }
}