/
file_info.lua
187 lines (168 loc) · 4.96 KB
/
file_info.lua
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
-- Copyright © 2015-2021 the contributors (see Contributors.md).
--
-- This file is part of Knora.
--
-- Knora 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.
--
-- Knora 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 Knora. If not, see <http://www.gnu.org/licenses/>.
require "util"
-------------------------------------------------------------------------------
-- String constants to be returned
-------------------------------------------------------------------------------
TEXT = "text"
IMAGE = "image"
DOCUMENT = "document"
AUDIO = "audio"
-------------------------------------------------------------------------------
-- Mimetype constants
-------------------------------------------------------------------------------
local IMAGE_JP2 = "image/jp2"
local IMAGE_TIFF = "image/tiff"
local IMAGE_PNG = "image/png"
local IMAGE_JPG = "image/jpeg"
local APPLICATION_XML = "application/xml"
local TEXT_XML = "text/xml"
local TEXT_PLAIN = "text/plain"
local AUDIO_MP3 = "audio/mpeg"
local AUDIO_MP4 = "audio/mp4"
local AUDIO_WAV = "audio/wav"
local AUDIO_X_WAV = "audio/x-wav"
local AUDIO_VND_WAVE = "audio/vnd.wave"
local APPLICATION_PDF = "application/pdf"
local APPLICATION_DOC = "application/msword"
local APPLICATION_DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
local APPLICATION_XLS = "application/vnd.ms-excel"
local APPLICATION_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
local APPLICATION_PPT = "application/vnd.ms-powerpoint"
local APPLICATION_PPTX = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
local APPLICATION_ZIP = "application/zip"
local APPLICATION_TAR = "application/x-tar"
local APPLICATION_ISO = "application/x-iso9660-image"
local APPLICATION_GZIP = "application/gzip"
local image_mime_types = {
IMAGE_JP2,
IMAGE_TIFF,
IMAGE_PNG,
IMAGE_JPG
}
local audio_mime_types = {
AUDIO_MP3,
AUDIO_MP4,
AUDIO_WAV,
AUDIO_X_WAV,
AUDIO_VND_WAVE
}
local text_mime_types = {
TEXT_PLAIN,
APPLICATION_XML,
TEXT_XML
}
local document_mime_types = {
APPLICATION_PDF,
APPLICATION_TAR,
APPLICATION_ZIP,
APPLICATION_ISO,
APPLICATION_GZIP,
APPLICATION_DOC,
APPLICATION_DOCX,
APPLICATION_XLS,
APPLICATION_XLSX,
APPLICATION_PPT,
APPLICATION_PPTX
}
local audio_extensions = {
"mp3",
"mp4",
"wav"
}
local text_extensions = {
"xml",
"xsl",
"xsd",
"txt",
"csv"
}
local document_extensions = {
"pdf",
"zip",
"tar",
"iso",
"gz",
"doc",
"docx",
"xls",
"xlsx",
"ppt",
"pptx"
}
function make_image_file_info(extension)
return {
media_type = IMAGE,
extension = extension
}
end
function make_audio_file_info(extension)
if not table.contains(audio_extensions, extension) then
return nil
else
return {
media_type = AUDIO,
extension = extension
}
end
end
function make_text_file_info(extension)
if not table.contains(text_extensions, extension) then
return nil
else
return {
media_type = TEXT,
extension = extension
}
end
end
function make_document_file_info(extension)
if not table.contains(document_extensions, extension) then
return nil
else
return {
media_type = DOCUMENT,
extension = extension
}
end
end
-------------------------------------------------------------------------------
-- Determines the media type and file extension of a file.
-- Parameters:
-- "filename" (string): the name of the file.
-- "mimetype" (string): the mimetype of the file.
--
-- Returns:
-- a table containing "media_type" and "extension", or false if no supported media type was found.
-------------------------------------------------------------------------------
function get_file_info(filename, mimetype)
local extension = filename:match("^.+%.([^.]+)$")
if extension == nil then
return nil
elseif table.contains(image_mime_types, mimetype) then
return make_image_file_info(extension)
elseif table.contains(audio_mime_types, mimetype) then
return make_audio_file_info(extension)
elseif table.contains(text_mime_types, mimetype) then
return make_text_file_info(extension)
elseif table.contains(document_mime_types, mimetype) then
return make_document_file_info(extension)
else
-- no supported mediatype could be determined
return nil
end
end