Merge pull request #12 from dubyte/fix_directory_with_spaces

Fix directory and files with spaces
This commit is contained in:
Sinuhe Tellez Rivera 2021-06-05 23:38:40 -04:00 committed by GitHub
commit 5bb3f1bb2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 10 deletions

View File

@ -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/), 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). 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
- return to filepath as the best way to handle paths for different platforms.
## [0.0.10] - 2021-05-06
### Added ### Added
- Unittests - Unittests
### Changed ### Changed
- structure of the project changed but public endpoint remains backward compatible. - structure of the project changed but public endpoint remains backward compatible.
## [0.0.9] - 2021-05-30 ## [0.0.9] - 2021-05-06
### Changed ### Changed
- using actions in github for building binaries only when tagging. - using actions in github for building binaries only when tagging.
## [0.0.8] - 2021-05-30 ## [0.0.8] - 2021-05-06
### Changed ### Changed
- using actions in github for building binaries only when tagging. - using actions in github for building binaries only when tagging.
## [0.0.7] - 2021-05-30 ## [0.0.7] - 2021-05-06
### Changed ### Changed
- using actions in github for testing - using actions in github for testing
## [0.0.6] - 2021-05-30 ## [0.0.6] - 2021-05-06
### Changed ### Changed
- using actions in github for testing - using actions in github for testing
## [0.0.5] - 2020-05-30 ## [0.0.5] - 2020-05-06
### removed ### removed
- usage of filepath package - usage of filepath package

View File

@ -10,8 +10,8 @@ import (
"log" "log"
"mime" "mime"
"net/http" "net/http"
"net/url"
"os" "os"
"path"
"path/filepath" "path/filepath"
"time" "time"
@ -37,7 +37,7 @@ type OPDS struct {
var TimeNowFunc = timeNow var TimeNowFunc = timeNow
func (s OPDS) Handler(w http.ResponseWriter, req *http.Request) error { 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) log.Printf("fPath:'%s'", fPath)
@ -84,7 +84,7 @@ func (s OPDS) makeFeed(dirpath string, req *http.Request) atom.Feed {
fis, _ := ioutil.ReadDir(dirpath) fis, _ := ioutil.ReadDir(dirpath)
for _, fi := range fis { for _, fi := range fis {
pathType := getPathType(path.Join(dirpath, fi.Name())) pathType := getPathType(filepath.Join(dirpath, fi.Name()))
feedBuilder = feedBuilder. feedBuilder = feedBuilder.
AddEntry(opds.EntryBuilder. AddEntry(opds.EntryBuilder.
ID(req.URL.Path + fi.Name()). 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 { 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 ( const (

View File

@ -27,6 +27,7 @@ func TestHandler(t *testing.T) {
"feed (dir of folders )": {input: "/", want: feed, WantedContentType: "application/xml"}, "feed (dir of folders )": {input: "/", want: feed, WantedContentType: "application/xml"},
"acquisitionFeed(dir of files)": {input: "/mybook", want: acquisitionFeed, 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"}, "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 { for name, tc := range tests {
@ -80,6 +81,13 @@ var feed = `<?xml version="1.0" encoding="UTF-8"?>
<published>2020-05-25T00:00:00+00:00</published> <published>2020-05-25T00:00:00+00:00</published>
<updated>2020-05-25T00:00:00+00:00</updated> <updated>2020-05-25T00:00:00+00:00</updated>
</entry> </entry>
<entry>
<title>new folder</title>
<id>/new folder</id>
<link rel="subsection" href="/new%20folder" type="application/atom+xml;profile=opds-catalog;kind=acquisition" title="new folder"></link>
<published>2020-05-25T00:00:00+00:00</published>
<updated>2020-05-25T00:00:00+00:00</updated>
</entry>
</feed>` </feed>`
var acquisitionFeed = `<?xml version="1.0" encoding="UTF-8"?> var acquisitionFeed = `<?xml version="1.0" encoding="UTF-8"?>
@ -91,6 +99,20 @@ var acquisitionFeed = `<?xml version="1.0" encoding="UTF-8"?>
<author> <author>
<name></name> <name></name>
</author> </author>
<entry>
<title>mybook copy.epub</title>
<id>/mybookmybook copy.epub</id>
<link rel="http://opds-spec.org/acquisition" href="/mybook/mybook%20copy.epub" type="application/epub+zip" title="mybook copy.epub"></link>
<published>2020-05-25T00:00:00+00:00</published>
<updated>2020-05-25T00:00:00+00:00</updated>
</entry>
<entry>
<title>mybook copy.txt</title>
<id>/mybookmybook copy.txt</id>
<link rel="http://opds-spec.org/acquisition" href="/mybook/mybook%20copy.txt" type="text/plain; charset=utf-8" title="mybook copy.txt"></link>
<published>2020-05-25T00:00:00+00:00</published>
<updated>2020-05-25T00:00:00+00:00</updated>
</entry>
<entry> <entry>
<title>mybook.epub</title> <title>mybook.epub</title>
<id>/mybookmybook.epub</id> <id>/mybookmybook.epub</id>

Binary file not shown.

View File

@ -0,0 +1 @@
Fixture

View File

@ -0,0 +1 @@
Fixture