-
Notifications
You must be signed in to change notification settings - Fork 4
/
core.clj
280 lines (233 loc) · 8.01 KB
/
core.clj
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
(ns tools.io.core
(:require
[clojure.java.io :as io]
[clojure.string :as str])
(:import
[java.io File Closeable Reader Writer]
[java.util.zip GZIPInputStream GZIPOutputStream ZipOutputStream ZipEntry]
[org.apache.commons.compress.archivers.zip ZipFile ZipArchiveEntry])
(:gen-class))
(defonce file-preds (atom {}))
(defn register-file-pred!
"Extend the dispatch function of the multimethods with your own
implementation.
Example:
(register-file-pred!
:my-filetype
(fn [filename] (clojure.string/starts-with? (str filename) \"myprotocol://\")))
(defmethod mk-input-stream :my-filetype
[filename & [options]]
;; ...
{:stream ...}
)
The code above is sufficient to get all reading function for free on your
custom protocol, given that mk-input-stream works correctly."
[file-type file-pred]
(swap! file-preds assoc file-type file-pred))
(defn unregister-file-pred!
[file-type]
(swap! file-preds dissoc file-type))
(defn get-file-type
"Dispatch function for files IO multimethods."
([filename]
(or (some (fn [[file-type file-pred]]
(when (file-pred filename)
file-type)) @file-preds)
(throw (Exception. (str "unsupported protocol for " filename ". perhaps you forgot to require the proper extension (eg.: tools.io.gs)")))))
([filename _ & _]
(get-file-type filename)))
(defn- mk-file-protocol-checker
[protocols]
{:pre [(set? protocols)]}
(fn [filename]
(when-not (instance? Reader filename)
(-> filename
str
(str/split #"://" 2)
first
str/lower-case
protocols))))
(defmulti mk-input-stream
"Returns an input stream with any implementation."
get-file-type)
(defmulti mk-output-stream
"Returns an output stream with any implementation."
get-file-type)
(defn gzipped?
"Tests if a filename ends with .gz or .gzip"
[filename]
(re-find #"(?i)\.gz(?:ip)?$" (str filename)))
(defn input-stream
"Returns an input-stream, with support of gzip compression."
([filename] (input-stream filename nil))
([filename options]
(let [is (mk-input-stream filename options)]
(if (gzipped? filename)
(update is :stream #(GZIPInputStream. %))
is))))
(defn output-stream
"Returns an output-stream, with support of gzip compression."
([filename] (output-stream filename nil))
([filename options]
(let [os (mk-output-stream filename options)]
(if (gzipped? filename)
(update os :stream #(GZIPOutputStream. %))
os))))
(defn file-reader
"Return a file reader"
([filename] (file-reader filename nil))
([filename options]
(let [file (input-stream filename options)]
(update file :stream #(io/reader % :encoding (:encoding options "UTF-8"))))))
(defn file-writer
"Return a file writer"
([filename] (file-writer filename nil))
([filename options]
(let [file (output-stream filename options)]
(update file :stream #(io/writer % :encoding (:encoding options "UTF-8"))))))
(defn close!
"Close a stream and optionally call its close-fn function if it's present."
[{:keys [^Closeable stream close-fn] :as file}]
(.close stream)
(when close-fn (close-fn file)))
(defmulti list-files
"Returns a seq of filenames with provided path as prefix."
get-file-type)
(defmulti list-dirs
"Return a seq of directories with provided path as prefix"
get-file-type)
(defmulti delete-file
"Deletes a file with any implementation."
get-file-type)
(defmulti exists?
"Returns `true` if filename exists"
get-file-type)
(defmulti zip-directory
"Create zip from target directory"
get-file-type)
(defmulti unzip-file
"Unzip the targeted file to the current
directory. If not a Zip file, yield `nil`"
get-file-type)
;;Shamefully copied from clojure.java.io/do-copy because we hardly can reuse the do-copy multi-fn
(defn copy
[^Reader input ^Writer output opts]
(let [^"[C" buffer (make-array Character/TYPE (:buffer-size opts 4096))]
(loop []
(let [size (.read input buffer)]
(when (pos? size)
(.write output buffer 0 size)
(recur))))))
;; Default Hooks
;; =============
;; On-Disk Files
;; -------------
(register-file-pred!
:base (some-fn
(mk-file-protocol-checker #{"file"})
(fn filename-with-no-protocol?
[filename]
(and
(not (instance? Reader filename))
(not (str/includes? (str filename) "://"))))))
(defmethod mk-input-stream :base
[filename & [options]]
{:stream (io/input-stream filename :encoding (:encoding options "UTF-8"))})
(defmethod mk-output-stream :base
[filename & [options]]
{:stream (io/output-stream filename :append (:append options false))})
(defmethod list-files :base
[path & [_options]]
(let [^File f (io/file path)]
(cond
(.isDirectory f)
(->> (file-seq f)
(filter #(.isFile ^File %))
(map #(.getPath ^File %)))
;; trailing slash but not a directory: nil
(str/ends-with? path File/separator)
nil
:else
(when-let [parent (.getParentFile f)]
(let [prefix (.getName f)]
(->> (.listFiles parent)
(filter (fn [^File file]
(and (.isFile file)
(str/starts-with? (.getName file) prefix))))
(map (fn [^File f] (.getPath f)))))))))
(defmethod list-dirs :base
[path & [_options]]
(let [^File f (io/file path)]
(when (.isDirectory f)
(->> (.listFiles f)
(filter #(.isDirectory ^File %))
(map #(.getPath ^File %))))))
(defmethod delete-file :base
[path & [options]]
(io/delete-file path (:silently options false)))
(defmethod exists? :base
[filename & [_options]]
(when filename
(try
(.exists (io/as-file filename))
(catch Exception _ false))))
;; Zip disk primitives
(defmethod zip-directory :base
[folder & {:keys [output-file
absolute?]
:or {absolute? true}}]
(when output-file
(try
(with-open [zip (ZipOutputStream. (io/output-stream output-file))]
(let [folder (if absolute? folder
(.getName (io/file folder)))
files (file-seq (io/file folder))]
(doseq [f files
:when (.isFile ^File f)]
(.putNextEntry zip (ZipEntry. (.getPath ^File f)))
(io/copy f zip)
(.closeEntry zip))))
true
(catch Exception _ false))))
(defmethod unzip-file :base
[filename & {:keys [output-folder overwrite?]
:or {overwrite? false}}]
(let [extension (str/lower-case (last (str/split filename #"\.")))]
(try
(when (and output-folder (= extension "zip"))
(with-open [z (ZipFile. (io/file filename))]
(doseq [^ZipArchiveEntry entry (enumeration-seq (.getEntries z))
:when (not (.isDirectory ^ZipArchiveEntry entry))
:let [zs (.getInputStream z entry)
out (io/file (str output-folder
java.io.File/separator
(.getName entry)))]]
(io/make-parents out)
(when (or (not (.exists out)) overwrite?)
(with-open [entry-o-s (io/output-stream out)]
(io/copy zs entry-o-s))))
true))
(catch Exception _ false))))
;; HTTP & HTTPS
;; ------------
;; Basic implementation.
(register-file-pred!
:http (mk-file-protocol-checker #{"http" "https"}))
(defmethod mk-input-stream :http
[filename & [options]]
{:stream (io/input-stream filename :encoding (:encoding options "UTF-8"))})
(defmethod exists? :http
[filename & [options]]
(when filename
(try
(with-open [stream (io/input-stream filename :encoding (:encoding options "UTF-8"))]
(pos? (.available stream)))
(catch Exception _ false))))
;; STDIN
;; -----
(register-file-pred!
:stdin (fn [filename]
(= *in* filename)))
(defmethod mk-input-stream :stdin
[filename & [_options]]
{:stream filename :close-fn (fn noop [_])})