+
Skip to content

Fix ring.response/attachment to handle space correctly #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## [Unreleased]

### Changed
- Exclude `slf4j-api` from dependencies.

### Fixed
- Fix `toyokumo.commons.ring.response/attachment` to handle filenames containing spaces correctly.

## 0.3.151
### Breaking
- Remove deprecated codes for PostgreSQL. Use `toyokumo.commons.db.postgresql` and `toyokumo.commons.db.extension.postgresql`
Expand Down
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
commons-io/commons-io {:mvn/version "2.11.0"}
info.sunng/ring-jetty9-adapter {:mvn/version "0.17.6"}
com.stuartsierra/component {:mvn/version "1.1.0"}
hikari-cp/hikari-cp {:mvn/version "2.14.0"}
hikari-cp/hikari-cp {:mvn/version "2.14.0" :exclusions [org.slf4j/slf4j-api]}
com.github.seancorfield/next.jdbc {:mvn/version "1.2.772"}
metosin/jsonista {:mvn/version "0.3.5"}
com.taoensso/carmine {:mvn/version "3.1.0"}
Expand Down
8 changes: 7 additions & 1 deletion src/toyokumo/commons/ring/response.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns toyokumo.commons.ring.response
(:require
[clojure.string :as str]
[ring.util.http-response :as res]
[toyokumo.commons.url :as tc.url]))

Expand Down Expand Up @@ -43,6 +44,11 @@
(defn content-disposition [resp value]
(header resp "Content-Disposition" value))

(defn- encode-filename [filename]
(-> filename
(tc.url/url-encode)
(str/replace "+" " ")))

(defn attachment
"Use when you want to make a client save response as a file.
For example:
Expand All @@ -51,7 +57,7 @@
(attachment \"foobar.csv\")
(csv)))"
[resp filename]
(content-disposition resp (str "attachment; filename=\"" (tc.url/url-encode filename) "\"")))
(content-disposition resp (str "attachment; filename=\"" (encode-filename filename) "\"")))

;;; Specific Content-Type

Expand Down
14 changes: 13 additions & 1 deletion test/toyokumo/commons/ring/response_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,19 @@
:headers {"Content-Disposition" "attachment; filename=\"%E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A.csv\""}
:status 200}
(-> (ok "test")
(attachment "あいうえお.csv")))))
(attachment "あいうえお.csv"))))

(is (= {:body "test"
:headers {"Content-Disposition" "attachment; filename=\"hello world.csv\""}
:status 200}
(-> (ok "test")
(attachment "hello world.csv"))))

(is (= {:body "test"
:headers {"Content-Disposition" "attachment; filename=\"hello%2Bworld.csv\""}
:status 200}
(-> (ok "test")
(attachment "hello+world.csv")))))

(deftest csv-test
(is (= {:body "foo,bar"
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载