From 1c32fecd2d467404cac2681ef46f37abb2110bdf Mon Sep 17 00:00:00 2001 From: Sinuhe Tellez Date: Sat, 5 Jun 2021 23:36:53 -0400 Subject: [PATCH 1/2] use file path and url path scape --- CHANGELOG.md | 16 ++++++++----- internal/service/service.go | 8 +++---- internal/service/service_test.go | 22 ++++++++++++++++++ .../service/testdata/mybook/mybook copy.epub | Bin 0 -> 2295 bytes .../service/testdata/mybook/mybook copy.txt | 1 + .../service/testdata/new folder/mybook.txt | 1 + 6 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 internal/service/testdata/mybook/mybook copy.epub create mode 100644 internal/service/testdata/mybook/mybook copy.txt create mode 100644 internal/service/testdata/new folder/mybook.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index f2d115a..2c720de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,30 +4,34 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [0.0.10] - 2021-05-30 +## [0.0.11] - 2021-06-05 +### Changed +- Unittests + +## [0.0.10] - 2021-05-06 ### Added - Unittests ### Changed - structure of the project changed but public endpoint remains backward compatible. -## [0.0.9] - 2021-05-30 +## [0.0.9] - 2021-05-06 ### Changed - using actions in github for building binaries only when tagging. -## [0.0.8] - 2021-05-30 +## [0.0.8] - 2021-05-06 ### Changed - using actions in github for building binaries only when tagging. -## [0.0.7] - 2021-05-30 +## [0.0.7] - 2021-05-06 ### Changed - using actions in github for testing -## [0.0.6] - 2021-05-30 +## [0.0.6] - 2021-05-06 ### Changed - using actions in github for testing -## [0.0.5] - 2020-05-30 +## [0.0.5] - 2020-05-06 ### removed - usage of filepath package diff --git a/internal/service/service.go b/internal/service/service.go index 4139960..54c2c9b 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -10,8 +10,8 @@ import ( "log" "mime" "net/http" + "net/url" "os" - "path" "path/filepath" "time" @@ -37,7 +37,7 @@ type OPDS struct { var TimeNowFunc = timeNow func (s OPDS) Handler(w http.ResponseWriter, req *http.Request) error { - fPath := path.Join(s.DirRoot, req.URL.Path) + fPath := filepath.Join(s.DirRoot, req.URL.Path) log.Printf("fPath:'%s'", fPath) @@ -84,7 +84,7 @@ func (s OPDS) makeFeed(dirpath string, req *http.Request) atom.Feed { fis, _ := ioutil.ReadDir(dirpath) for _, fi := range fis { - pathType := getPathType(path.Join(dirpath, fi.Name())) + pathType := getPathType(filepath.Join(dirpath, fi.Name())) feedBuilder = feedBuilder. AddEntry(opds.EntryBuilder. ID(req.URL.Path + fi.Name()). @@ -124,7 +124,7 @@ func getType(name string, pathType int) string { } func getHref(req *http.Request, name string) string { - return path.Join(req.URL.RequestURI(), name) + return filepath.Join(req.URL.RequestURI(), url.PathEscape(name)) } const ( diff --git a/internal/service/service_test.go b/internal/service/service_test.go index e070cad..33d321b 100644 --- a/internal/service/service_test.go +++ b/internal/service/service_test.go @@ -27,6 +27,7 @@ func TestHandler(t *testing.T) { "feed (dir of folders )": {input: "/", want: feed, WantedContentType: "application/xml"}, "acquisitionFeed(dir of files)": {input: "/mybook", want: acquisitionFeed, WantedContentType: "application/xml"}, "servingAFile": {input: "/mybook/mybook.txt", want: "Fixture", WantedContentType: "text/plain; charset=utf-8"}, + "serving file with spaces": {input: "/mybook/mybook%20copy.txt", want: "Fixture", WantedContentType: "text/plain; charset=utf-8"}, } for name, tc := range tests { @@ -80,6 +81,13 @@ var feed = ` 2020-05-25T00:00:00+00:00 2020-05-25T00:00:00+00:00 + + new folder + /new folder + + 2020-05-25T00:00:00+00:00 + 2020-05-25T00:00:00+00:00 + ` var acquisitionFeed = ` @@ -91,6 +99,20 @@ var acquisitionFeed = ` + + mybook copy.epub + /mybookmybook copy.epub + + 2020-05-25T00:00:00+00:00 + 2020-05-25T00:00:00+00:00 + + + mybook copy.txt + /mybookmybook copy.txt + + 2020-05-25T00:00:00+00:00 + 2020-05-25T00:00:00+00:00 + mybook.epub /mybookmybook.epub diff --git a/internal/service/testdata/mybook/mybook copy.epub b/internal/service/testdata/mybook/mybook copy.epub new file mode 100644 index 0000000000000000000000000000000000000000..b675c762daeaebdebdb644abbee9855be66ed30f GIT binary patch literal 2295 zcmah~2{@GN9v{nOvMWoI22&hr7=upANsLUk%3Na$DKygzW?@FhXskJg$~J|OoRs~b zEIA#FjxBMrCi}^HLK22D(d~9WH%rHJyZ8N`|MUId?|Xj#=lw75?}xP!5tRplARrJ| zC&!NJQJ@X%K&u9P0+C5%JcCKYd(ddUBrgvJiAvGM(*isZK_nV>4GH-Vvdc$!$_GI3 z08<9c7Gs9R>CzcYUp#&7Ma1J7NH045r#Q5DBuWw^ID$c9{fcs}$bJz6H+OZW)>irW zjaw_J9=jtLaZ0`E{ZElG^8vcI>>J+8t~zOKYpUI-wP45NlyOgYo@-JpZtlD7CAINd zpVgtUZ7kP}?^nX#N=X!!7$h&&5nYlkJlGXORX2Ct+MQ)L#GjtUt{W|Pabc#zeaiSBiP*#er%fGYq@{X*SKXe zK;6IY)^?mEPGx+;xfj9J4h4Nl4g&eo03e@@r3QFw4ZwS?HVtUO7YBtx>8)iOkM^%u z1G-=g&aAE_DHh>qmyV!=K%R*37!mou`W^?v{c^g%S3aDHVXG$d&amUe4^qqJrsxj8YmuCwD9-&;9ybiOGNO*iKFmp=^iaXghIE-0?ET##FXwPhpk*PJUq+J&_1S6{Vonhq6Vr$1+B2 zMI9@9W1b|?8|c|L=9GG*jPOx*7hi~VO4kRju<;*hAz{_M@7eQkeBr#el+m=2#D1dS zxpjK^m2}c|EevzA0)9t7zc68R@Eq1g4DzqEHn~}#G87@uTJ1nEYCbYW2a21Yes?v@;qnwI+x=3ugKlC8yWuSA zg{1Pr+z(vsW(uynX-7*-nqZ=SZ^fGe{Xn7KTLU@TaiS76zqlw@+4PUsKccnHmdaS| z>n{5f-En5I;7SLvm?$oS#E1UYmr6Z0pBO>L{c7QR=(egfyJ;#X+WXPC0o;*Cxw?~> z*&XR)

z>agTLgboDDg`9%Z8T8FpYqN=G0>hm(o+WDaDWUVsug4T+ZnkgR`;hEc$ zMP^}LV8zdD2|3+*o>q^C+_fhM+@mq~9qNX$Ou|8DvnApka&U2N{ds2Jl&b6>;SB?) zxVo@epGowCyF!lERd0yzf}scpWaMwe3(znwT~jCW@x^fL`TP(nCv)~ zmCkV>C;oD}zq%R5(iP7Pd$3ndC$qn`y5#Kd!7jrTs!}cZybZ?$4ObeQ@VRIWYm11_ zZod0^0p^U{>oTON=zIqKN0SJJ7{&K}p~60Ay_|IX&e2|KKd`!s~NH?4A+cSz3j7`*TcjGA;RK(&L7TFQ?p~ zjLHLjR6}-Vpa}HAVFotPw+Pu~kRc3T!f!|;kl%y@H%|$8f}Gcov=-)@wRWx8`cI4`~!i+ NfTsz_7Am&7`!}ELia-DW literal 0 HcmV?d00001 diff --git a/internal/service/testdata/mybook/mybook copy.txt b/internal/service/testdata/mybook/mybook copy.txt new file mode 100644 index 0000000..fdfaaa1 --- /dev/null +++ b/internal/service/testdata/mybook/mybook copy.txt @@ -0,0 +1 @@ +Fixture \ No newline at end of file diff --git a/internal/service/testdata/new folder/mybook.txt b/internal/service/testdata/new folder/mybook.txt new file mode 100644 index 0000000..fdfaaa1 --- /dev/null +++ b/internal/service/testdata/new folder/mybook.txt @@ -0,0 +1 @@ +Fixture \ No newline at end of file From a1549f8bc32bb5ba10109461e156e00a0644b769 Mon Sep 17 00:00:00 2001 From: Sinuhe Tellez Date: Sat, 5 Jun 2021 23:37:34 -0400 Subject: [PATCH 2/2] use file path and url path scape --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c720de..59e7a0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.0.11] - 2021-06-05 ### Changed -- Unittests +- return to filepath as the best way to handle paths for different platforms. ## [0.0.10] - 2021-05-06 ### Added