Skip to content

Commit a49674f

Browse files
author
Roman Rudakov
authored
Merge pull request #46 from rrudakov/hotfix/fix-upload-api-invalid-filenames
Accept invalid filenames for upload endpoint
2 parents 4c94225 + 017d979 commit a49674f

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

src/education/http/endpoints/upload.clj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@
2727
(with-open [out (output-stream (file name))]
2828
(.write out (file->byte-array f))))
2929

30+
(defn- extract-file-extension
31+
[filename]
32+
(if (str/includes? filename ".")
33+
(str "." (last (str/split filename #"\.")))
34+
""))
35+
3036
(defn upload-file-handler
3137
[{:keys [filename _content-type tempfile]} config]
32-
(let [name (str/join "_" [(uuid) filename])
33-
path (path/join (config/storage-path config) img-prefix name)]
38+
(let [extension (extract-file-extension filename)
39+
name (str/join [(uuid) extension])
40+
path (path/join (config/storage-path config) img-prefix name)]
3441
(write-file tempfile path)
3542
(ok {:url (path/join (config/base-url config) img-prefix name)})))
3643

test-resources/2

189 KB
Binary file not shown.

test/education/http/endpoints/upload_test.clj

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
Must be real image name from `test-resources` folder."
1717
"1.png")
1818

19+
(def ^:private test-image-name-without-extension
20+
"Image name to be uploaded.
21+
22+
This image without extension to check `extract-file-extension` function."
23+
"2")
24+
1925
(deftest uuid-test
2026
(testing "Test `uuid` function returns unique string every time"
2127
(let [first-uuid (sut/uuid)
@@ -37,14 +43,30 @@
3743
(testing "Test POST /upload successfully"
3844
(with-redefs [sut/write-file (spy/spy)
3945
sut/uuid (spy/stub test-uuid)]
40-
(let [app (test-app/api-routes-with-auth)
46+
(let [file (io/file (io/resource test-img-name))
47+
app (test-app/api-routes-with-auth)
48+
response (app (-> (mock/request :post "/api/upload")
49+
(merge (mp/build {:file file}))))
50+
body (test-app/parse-body (:body response))
51+
[[f name]] (spy/calls sut/write-file)]
52+
(is (= 200 (:status response)))
53+
(is (= {:url (str (config/base-url td/test-config) "/img/" test-uuid ".png")} body))
54+
(is (= (str (config/storage-path td/test-config) "img/" test-uuid ".png") name))
55+
(is (= (slurp f) (slurp file))))))
56+
57+
(testing "test POST /upload successfully without file extension"
58+
(with-redefs [sut/write-file (spy/spy)
59+
sut/uuid (spy/stub test-uuid)]
60+
(let [file (io/file (io/resource test-image-name-without-extension))
61+
app (test-app/api-routes-with-auth)
4162
response (app (-> (mock/request :post "/api/upload")
42-
(merge (mp/build {:file (io/file (io/resource test-img-name))}))))
63+
(merge (mp/build {:file file}))))
4364
body (test-app/parse-body (:body response))
44-
[[_ name]] (spy/calls sut/write-file)]
65+
[[f name]] (spy/calls sut/write-file)]
4566
(is (= 200 (:status response)))
46-
(is (= {:url (str (config/base-url td/test-config) "/img/" test-uuid "_" test-img-name)} body))
47-
(is (= (str (config/storage-path td/test-config) "img/" test-uuid "_" test-img-name) name)))))
67+
(is (= {:url (str (config/base-url td/test-config) "/img/" test-uuid)} body))
68+
(is (= (str (config/storage-path td/test-config) "img/" test-uuid) name))
69+
(is (= (slurp f) (slurp file))))))
4870

4971
(testing "Test POST /upload with invalid request"
5072
(with-redefs [sut/write-file (spy/spy)]

0 commit comments

Comments
 (0)