-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmagic.ex
More file actions
255 lines (203 loc) · 6.09 KB
/
Copy pathmagic.ex
File metadata and controls
255 lines (203 loc) · 6.09 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# Code in this file adapted from the mimemagic gem, released under the MIT License.
# Copyright (c) 2011 Daniel Mendler. Available at https://github.com/mimemagicrb/mimemagic.
defmodule ExMarcel.Magic do
defstruct [:type, :mediatype, :subtype]
alias ExMarcel.TableWrapper
# Mime type by type string
def new(type) do
[mediatype, subtype] = String.split(type, "/")
%__MODULE__{
type: type,
mediatype: mediatype,
subtype: subtype
}
end
# Add custom mime type. Arguments:
# * <i>type</i>: Mime type
# * <i>options</i>: Options hash
#
# Option keys:
# * <i>:extensions</i>: String list or single string of file extensions
# * <i>:parents</i>: String list or single string of parent mime types
# * <i>:magic</i>: Mime magic specification
# * <i>:comment</i>: Comment string
def add(type, options \\ []) do
defaults = [extensions: nil, magic: nil, parents: nil]
options = Keyword.merge(defaults, options) |> Enum.into(%{})
extensions = options.extensions
new_extensions = TableWrapper.type_exts() |> Map.put(type, extensions)
ExMarcel.TableWrapper.put("type_exts", new_extensions)
case options.parents do
[] ->
nil
_ ->
new_parents = TableWrapper.type_parents() |> Map.put(type, options.parents)
TableWrapper.put("type_parents", new_parents)
end
table_extensions = TableWrapper.extensions()
new_table_extensions =
Enum.reduce(extensions, table_extensions, fn ext, acc ->
Map.put(acc, ext, type)
end)
TableWrapper.put("extensions", new_table_extensions)
if options[:magic] do
TableWrapper.put("magic", [[type, options.magic] | TableWrapper.magic()])
end
end
# Removes a mime type from the dictionary. You might want to do this if
# you're seeing impossible conflicts (for instance, application/x-gmc-link).
# * <i>type</i>: The mime type to remove. All associated extensions and magic are removed too.
def remove(_struct, _type) do
# TableWrapper.extensions() |> Map.delete(map, :b)
# EXTENSIONS.delete_if {|ext, t| t == type }
# MAGIC.delete_if {|t, m| t == type }
# TYPE_EXTS.delete(type)
# TYPE_PARENTS.delete(type)
end
# Returns true if type is a text format
def text?(struct) do
struct.mediatype.text || child_of?(struct, "text/plain")
end
# Mediatype shortcuts
def image?(struct) do
struct.mediatype == "image"
end
def audio?(struct) do
struct.mediatype == "audio"
end
def video?(struct) do
struct.mediatype == "video"
end
# Returns true if type is child of parent type
def child_of?(struct, parent) do
child?(struct.type, parent)
end
# Get string list of file extensions
def extensions(struct) do
TableWrapper.type_exts() |> Map.get(struct.type) || []
end
# Get mime comment
def comment(_struct) do
# deprecated
nil
end
# Lookup mime type by file extension
def by_extension(ext) do
ext = ext |> String.downcase()
mime =
case ext |> String.slice(0..0) do
"." ->
ext = ext |> String.slice(1..-1//-1)
TableWrapper.extensions() |> Map.get(ext)
_ ->
TableWrapper.extensions() |> Map.get(ext)
end
mime && __MODULE__.new(mime)
end
# Lookup mime type by filename
def by_path(path) do
by_extension(Path.extname(path))
end
# Lookup mime type by magic content analysis.
# This is a slow operation.
def by_magic(io) do
mime = magic_match(io, :find)
case mime do
[type, _] -> new(type)
_ -> nil
end
end
# Lookup all mime types by magic content analysis.
# This is a slower operation.
def all_by_magic(_io) do
# magic_match(io, :select).map { |mime| new(mime[0]) }
end
# Return type as string
def to_s(struct) do
struct.type
end
# Allow comparison with string
def eql?(struct, other) do
struct.type == to_s(other)
end
def hash(_struct) do
# type.hash
end
# alias == eql?
def child?(child, parent) do
require IEx
IEx.pry()
child == parent || TableWrapper.type_parents()
# child == parent || TYPE_PARENTS[child]&.any? {|p| child?(p, parent) }
end
def magic_match(io, method) do
case method do
:find ->
TableWrapper.magic()
|> Enum.find(nil, fn [type, matches] ->
# IO.puts("MAGIC MATCH FOR: #{type}")
magic_match_io(io, matches, "", type)
end)
_ ->
raise "no valid method for search magic bytes!"
end
end
def magic_match_io(io, matches, buffer, type) do
process_io = fn offset, value, children ->
if value do
value =
cond do
is_list(value) ->
# IO.inspect(value, binaries: :as_strings)
List.first(value)
is_nil(value) ->
""
true ->
value
end
cond do
offset |> is_integer() ->
# read first to skip buffer
IO.binread(io, offset)
buffer = IO.binread(io, byte_size(value))
if buffer == value do
[true, children]
else
[false, children]
end
# here we ask for a range
offset.__struct__ == Range ->
# IO.inspect(value)
# IO.binread(io, offset.first)
x = IO.binread(io, offset.last - offset.first + byte_size(value))
case x do
:eof ->
[false, children]
_ ->
result = String.contains?(x, value)
[result, children]
end
true ->
[false, children]
end
else
[false, children]
end
end
matches
|> Enum.any?(fn x ->
[match, children] =
case x do
[offset, value, children] ->
process_io.(offset, value, children)
[offset, value] ->
process_io.(offset, value, nil)
_ ->
[false, nil]
end
:file.position(io, :bof)
# IO.inspect(match)
match && (!children || magic_match_io(io, children, buffer, type))
end)
end
end