DisseminationCopyHandler.scala 4.56 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
/*
 * 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, FileOutputStream, IOException}
import java.nio.file.{Files, Path, Paths}

import scala.util.{Failure, Success, Try}

/**
 * Manages dissemination copies of media files
 *
 * @param audioDestPath Path to folder containing audio dissemination copies
 * @param imageDestPath Path to folder containing image dissemination copies
 * @param videoDestPath Path to folder containing video dissemination copies
 */
class DisseminationCopyHandler(audioDestPath: String, imageDestPath: String, videoDestPath: String) {

  private def writeData(data: ByteArrayOutputStream, destFile: Path): Try[Path] = {
    Try(new FileOutputStream(destFile.toFile)) match {
      case Success(fos) =>
        try {
          data.writeTo(fos)
          Success(destFile)
        } catch {
          case e: IOException => Failure(e)
        } finally {
          fos.close()
          data.close()
        }
      case Failure(ex) => Failure(ex)
    }
  }

  /**
   * Creates destination filename
   *
   * @param id            id (signature) of record to which the media resource belongs to
   * @param collectionId  id of collection to which the media resource belongs to
   * @param institutionId id of institution to which the media resource belongs to
   * @return generated filename
   */
  private def createDestFilename(id: String, collectionId: String, institutionId: String): String = {
    // TODO: Create real filename producing function
    s"$institutionId-$collectionId-$id"
  }


  /**
   * Creates dissemination copy of audio file
   *
   * @param data   binary data as [[java.io.ByteArrayOutputStream]] instance
   * @param destId Filename of dissemination copy without extension
   * @return
   */
72
73
74
75
76
77
78
  def createAudioCopy(data: ByteArrayOutputStream, destId: String): Try[Path] = Try {
    val tempFilePath = Files.createTempFile("media-", null)
    val destFile = Paths.get(audioDestPath, destId + ".mp4")
    writeData(data, tempFilePath)
    Transformations.audioToMp4(tempFilePath.toString, destFile.toString).get
    Files.delete(tempFilePath)
    destFile
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
79
80
81
82
83
84
85
86
87
  }

  /**
   * Creates dissemination copy of image file
   *
   * @param data   binary data as [[java.io.ByteArrayOutputStream]] instance
   * @param destId Filename of dissemination copy without extension
   * @return
   */
88
89
  def createImageCopy(data: ByteArrayOutputStream, destId: String): Try[Path] = Try {
    val tempFilePath = Files.createTempFile("media-", null)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
90
    val destFile = Paths.get(imageDestPath, destId + ".jp2")
91
92
93
    Transformations.imageToJp2(tempFilePath.toString, destFile.toString).get
    Files.delete(tempFilePath)
    destFile
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  }

  /**
   * Create dissemination copy of video file
   *
   * @param data   binary data as [[java.io.ByteArrayOutputStream]] instance
   * @param destId Filename of dissemination copy without extension
   * @return
   */
  def createVideoCopy(data: ByteArrayOutputStream, destId: String): Try[Path] = {
    val destFile = Paths.get(videoDestPath, destId + ".mp4")
    writeData(data, destFile)
  }

  /**
   * Deletes dissemination copy of audio file
   *
   * @param destId Filename of dissemination copy without extension
   * @return
   */
  def deleteAudioCopy(destId: String): Try[Boolean] =
    Try {
      Paths.get(audioDestPath, destId + ".mp4").toFile.delete()
    }

  /**
   * Deletes dissemination copy of image file
   *
   * @param destId Filename of dissemination copy without extension
   * @return
   */
  def deleteImageCopy(destId: String): Try[Boolean] =
    Try {
      Paths.get(imageDestPath, destId + ".jp2").toFile.delete()
    }

  /**
   * Deletes dissemination copy of video file
   *
   * @param destId Filename of dissemination copy without extension
   * @return
   */
  def deleteVideoCopy(destId: String): Try[Boolean] =
    Try {
      Paths.get(videoDestPath, destId + ".mp4").toFile.delete()
    }
}