DisseminationCopyHandler.scala 4.67 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
/*
 * 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}

24
25
import ch.memobase.models.{Conversions, MediaFileType}

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
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
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 dissemination copy of audio file
   *
57
58
59
   * @param data           binary data as [[java.io.ByteArrayOutputStream]] instance
   * @param destId         Filename of dissemination copy without extension
   * @param sourceFileType File type of input data
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
60
61
   * @return
   */
62
63
  def createAudioCopy(data: ByteArrayOutputStream, destId: String, sourceFileType: MediaFileType): Try[Path] = Try {
    val tempFilePath = Files.createTempFile("media-", "." + Conversions.getFileTypeExtension(sourceFileType).get)
64
65
66
67
68
    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
69
70
71
72
73
  }

  /**
   * Creates dissemination copy of image file
   *
74
75
76
   * @param data           binary data as [[java.io.ByteArrayOutputStream]] instance
   * @param destId         Filename of dissemination copy without extension
   * @param sourceFileType File type of input data
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
77
78
   * @return
   */
79
80
  def createImageCopy(data: ByteArrayOutputStream, destId: String, sourceFileType: MediaFileType): Try[Path] = Try {
    val tempFilePath = Files.createTempFile("media-", "." + Conversions.getFileTypeExtension(sourceFileType).get)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
81
    val destFile = Paths.get(imageDestPath, destId + ".jp2")
82
    writeData(data, tempFilePath)
83
84
85
    Transformations.imageToJp2(tempFilePath.toString, destFile.toString).get
    Files.delete(tempFilePath)
    destFile
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
86
87
88
89
90
  }

  /**
   * Create dissemination copy of video file
   *
91
92
93
   * @param data           binary data as [[java.io.ByteArrayOutputStream]] instance
   * @param destId         Filename of dissemination copy without extension
   * @param sourceFileType File type of input data
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
94
95
   * @return
   */
96
97
  def createVideoCopy(data: ByteArrayOutputStream, destId: String, sourceFileType: MediaFileType): Try[Path] = {
    val destFile = Paths.get(videoDestPath, s"$destId.${Conversions.getFileTypeExtension(sourceFileType).get}")
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
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
    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
   *
126
127
   * @param destId   Filename of dissemination copy without extension
   * @param fileType File type of media
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
128
129
   * @return
   */
130
  def deleteVideoCopy(destId: String, fileType: MediaFileType): Try[Boolean] =
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
131
    Try {
132
      Paths.get(videoDestPath, s"$destId.${Conversions.getFileTypeExtension(fileType).get}").toFile.delete()
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
133
134
135
    }
}