-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.clj
194 lines (143 loc) · 5.07 KB
/
io.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
(ns tech.io
"Wrapper for tech io subsystem designed to be drop in replacement for some use cases
of clojure.java.io."
(:require [clojure.java.io :as io]
[tech.io.url :as url]
[tech.io.protocols :as io-prot]
[tech.io.edn :as edn]
[tech.io.base]
[tech.io.temp-file :as temp-file]
[think.resource.core :as resource]
[tech.io.providers :as providers])
(:import [javax.imageio ImageIO]))
(defn enable-s3!
[]
(require 'tech.io.s3))
;;Purists or poeple using components will want to use the io-protocols directly with providers
;;passed in. This API is meant to mimic clojure.java.io but in a more extensible way.
(def ^:dynamic *provider-fn* #(or (providers/default-provider)
(io-prot/url-parts->provider %)))
(defmacro with-provider-fn
[provider-fn & body]
`(with-bindings {#'*provider-fn* ~provider-fn }
~@body))
(defmacro with-provider
[provider & body]
`(with-provider-fn (constantly ~provider) ~@body))
(defn- args->map
[args]
(apply hash-map args))
;;Straight forwards
(def reader io/reader)
(def writer io/writer)
(def make-parents io/make-parents)
(defn file
"Wrapper around "
[path-or-url]
(let [filepath (if (url/url? path-or-url)
(-> (url/url->parts path-or-url)
url/parts->file-path)
path-or-url)]
(io/file filepath)))
(defmacro ^:private lookup-provider
[url & body]
`(let [~'url-parts (url/url->parts ~url)
~'provider (*provider-fn* ~'url-parts)]
~@body))
(defn input-stream
"thing->input-stream conversion. Falls back to clojure.java.io if url is not a string url"
[url & options]
(if (url/url? url)
(lookup-provider url
(io-prot/input-stream provider url-parts (args->map options)))
(apply io/input-stream url options)))
(defn output-stream!
"thing->output-stream conversion. Falls back to clojure.java.io if url is not a string url"
[url & options]
(if (url/url? url)
(lookup-provider url
(io-prot/output-stream! provider url-parts (args->map options)))
(apply io/output-stream url options)))
(defn copy
[src dest & args]
(with-open [in-s (apply input-stream src args)
out-s (apply output-stream! dest args)]
(io/copy in-s out-s)))
(defn ls
"Return a directory listing. May be recursive if desired; only works with file
or s3 providers."
[url & {:keys [recursive?] :as options}]
(lookup-provider url
(io-prot/ls provider url-parts
(assoc (args->map options)
:recursive? recursive?))))
(defn delete!
"Delete a resource. Works currently with file or s3."
[url & options]
(lookup-provider url
(io-prot/delete! provider url-parts (args->map options))))
(defn exists?
"Boolean existence check. Works with everything as fallback is to open
an input stream and then close it."
[url & options]
(lookup-provider url
(io-prot/exists? provider url-parts (args->map options))))
(defn metadata
"If supported, returns at least :modify-date and :byte-length.
Exception otherwise."
[url & options]
(lookup-provider url
(io-prot/metadata provider url-parts options)))
(defn get-object
"Get object always returns something convertible to an input-stream.
It may return a file for instance."
[url & options]
(lookup-provider url
(io-prot/get-object provider url-parts (args->map options))))
(defn put-object!
"Put object. Object must be a byte-array, a file, or an input-stream.
Strings will be interpreted as per the rules of clojure.java.io/input-stream.
The most optimizations will apply to either files or byte arrays."
[url value & options]
(lookup-provider url
(io-prot/put-object! provider url-parts value (args->map options))))
(defn put-edn!
"Put edn data to a url"
[url data & options]
(lookup-provider url
(edn/put-edn! provider url-parts data (args->map options))))
(defn get-edn
"Get edn data from a url"
[url & options]
(lookup-provider url
(edn/get-edn provider url-parts (args->map options))))
(defn put-nippy!
"Put nippy data to a url"
[url data & options]
(lookup-provider url
(edn/put-nippy! provider url-parts data (args->map options))))
(defn get-nippy
"Get nippy data from a url"
[url & options]
(lookup-provider url
(edn/get-nippy provider url-parts (args->map options))))
(defn put-image!
"Will throw if an image with transparency is used to write a jpeg"
[image path-or-url & {:as options}]
(let [path-ext (url/extension path-or-url)]
(with-open [out-s (output-stream! path-or-url)]
(ImageIO/write image path-ext out-s))))
(defn get-image
[path-or-url]
(resource/with-resource-context
(let [temp (temp-file/watch-file-for-delete
(temp-file/random-file-url))]
(copy path-or-url temp)
(ImageIO/read (file temp)))))
(def ^:dynamic *enable-poor-api-naming?* true)
(defn enable-poor-api-naming!
[]
(def output-stream output-stream!)
(def put-object put-object!))
(when *enable-poor-api-naming?*
(enable-poor-api-naming!))