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

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

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
27
28
29
30
31
32
33
34
35
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
 */
36
class DisseminationCopyHandler(audioDestPath: String, imageDestPath: String, videoDestPath: String, audioSnippetDuration: Int) {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

  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
   *
58
59
60
   * @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
61
62
   * @return
   */
63
64
  def createAudioCopy(data: ByteArrayOutputStream, destId: String, sourceFileType: MediaFileType): Try[Path] = Try {
    val tempFilePath = Files.createTempFile("media-", "." + Conversions.getFileTypeExtension(sourceFileType).get)
65
    val destFile = Paths.get(audioDestPath, destId + ".mp4")
66
    val snippetFile = Paths.get(audioDestPath, destId + "-intro." + Conversions.getFileTypeExtension(sourceFileType).get)
67
    writeData(data, tempFilePath)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
68
69
    MediaTransformations.audioToMp4(tempFilePath.toString, destFile.toString).get
    MediaTransformations.createAudioSnippet(tempFilePath.toString, snippetFile.toString, audioSnippetDuration)
70
71
    Files.delete(tempFilePath)
    destFile
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
72
73
74
75
76
  }

  /**
   * Creates dissemination copy of image file
   *
77
78
79
   * @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
80
81
   * @return
   */
82
83
  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
84
    val destFile = Paths.get(imageDestPath, destId + ".jp2")
85
    writeData(data, tempFilePath)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
86
    MediaTransformations.imageToJp2(tempFilePath.toString, destFile.toString).get
87
88
    Files.delete(tempFilePath)
    destFile
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
89
90
91
92
93
  }

  /**
   * Create dissemination copy of video file
   *
94
95
96
   * @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
97
98
   * @return
   */
99
100
  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
101
102
103
104
105
106
107
108
109
110
111
112
    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()
113
      Paths.get(audioDestPath, destId + "-intro.mp3").toFile.delete()
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    }

  /**
   * 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
   *
130
131
   * @param destId   Filename of dissemination copy without extension
   * @param fileType File type of media
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
132
133
   * @return
   */
134
  def deleteVideoCopy(destId: String, fileType: MediaFileType): Try[Boolean] =
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
135
    Try {
136
      Paths.get(videoDestPath, s"$destId.${Conversions.getFileTypeExtension(fileType).get}").toFile.delete()
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
137
138
139
    }
}