Due to a scheduled upgrade to version 14.10, GitLab will be unavailabe on Monday 30.05., from 19:00 until 20:00.

MediaFileTypes.scala 2.46 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 * 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.models

/**
 * Represents a media file type manageable by media conversion tools
 */
sealed trait MediaFileType

/**
 * Represents any manageable audio file type
 */
sealed trait AudioFileType extends MediaFileType

/**
 * Represents any manageable image file type
 */
sealed trait ImageFileType extends MediaFileType

/**
 * Represents any manageable video file type
 */
sealed trait VideoFileType extends MediaFileType

/**
 * Represents an unknown file type
 */
case object UnknownFileType extends MediaFileType

/**
 * Represents an MP3 file
 */
case object Mp3File extends AudioFileType

52
53
54
55
56
/**
 * Represents an OGA file
 */
case object OgaFile extends AudioFileType

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
57
58
59
60
61
/**
 * Represents a JPG file
 */
case object JpegFile extends ImageFileType

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
62
63
64
65
66
/**
 * Represents a PNG file
 */
case object PngFile extends ImageFileType

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
67
68
69
70
/**
 * Represents a MPEG4 video file
 */
case object VideoMpeg4File extends VideoFileType
71
72
73
74
75
76
77
78

/**
 * Helps with conversions between different representations of file types
 */
object Conversions {
  private val fileTypeTuples: List[(MediaFileType, List[String], String)] = List(
    (Mp3File, List("audio/mpeg"), "mp3"),
    (OgaFile, List("audio/ogg"), "oga"),
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
79
80
    (JpegFile, List("image/jpeg"), "jpg"),
    (PngFile, List("image/png"), "png"),
81
82
83
84
85
86
87
88
89
90
91
92
93
    (VideoMpeg4File, List("video/mp4"), "mp4")
    // TODO: Other filetypes...
  )

  lazy val getMediaFileType: String => Option[MediaFileType] = (mimeType: String) =>
    fileTypeTuples.collectFirst {
      case (ft, mt, _) if mt.contains(mimeType) => ft
    }

  lazy val getFileTypeExtension: MediaFileType => Option[String] = (mediaFileType: MediaFileType) =>
    fileTypeTuples.collectFirst {
      case (ft, _, ex) if ft == mediaFileType => ex
    }
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
94
}