Resolve upload logic for frontend.

Frontend logic for routing was a bit broken when generating links for
directories, appending an extra / to the start. Resolved this to
properly link - interestingly, the file listing could still be served,
but the uploading was broken?

Also enforced regex for provider of directory listings and tidied up
some other string handling.
This commit is contained in:
Gabriel Simmer 2020-04-03 02:24:18 +01:00
parent 641e015ca6
commit c183003953
No known key found for this signature in database
GPG key ID: 33BA4D83B160A0A9
4 changed files with 7 additions and 9 deletions

View file

@ -13,12 +13,12 @@ function getFileListing(provider, path = "") {
let files = data["Files"] let files = data["Files"]
html` html`
<form action="#" method="post"> <form action="#" method="post">
<input type="file" id="file" data-dir="${provider}${path}"><label for="file">Upload</label> <input type="file" id="file" data-dir="${provider}/${path}"><label for="file">Upload</label>
</form> </form>
<progress id="progress" value="0" max="100" hidden=""></progress> <progress id="progress" value="0" max="100" hidden=""></progress>
<div class="grid-sm"> <div class="grid-sm">
${files.map(file => ${files.map(file =>
`<a href="${!file.IsDirectory ? `/api/files/${provider}${path}/${file.Name}` : `#${provider}/${path !== "" ? path.replace("/","") + "/" : ""}${file.Name}`}"> `<a href="${!file.IsDirectory ? `/api/files/${provider}/${path}/${file.Name}` : `#${provider}${path === "" ? "" : "/" + path}/${file.Name}`}">
${file.Name}${file.IsDirectory ? '/' : ''} ${file.Name}${file.IsDirectory ? '/' : ''}
</a> </a>
` `
@ -64,7 +64,7 @@ function router(event = null) {
let path = hash.split("/") let path = hash.split("/")
let provider = path.shift() let provider = path.shift()
path = path.join("/") path = path.join("/")
getFileListing(provider, "/" + path) getFileListing(provider, path)
} }
// File upload functions. Uses XMLHttpRequest so we can display file upload progress. // File upload functions. Uses XMLHttpRequest so we can display file upload progress.

View file

@ -256,9 +256,6 @@ func (bp *BackblazeProvider) SaveFile(file multipart.File, handler *multipart.Fi
} }
func (bp *BackblazeProvider) DetermineType(path string) string { func (bp *BackblazeProvider) DetermineType(path string) string {
// TODO: Implement directory support for B2.
return "file" return "file"
} }
func (bp *BackblazeProvider) GetProviderConfig() FileProvider {
return ProviderConfig[bp.Name]
}

View file

@ -56,7 +56,8 @@ func (dp *DiskProvider) ViewFile(path string, w io.Writer) {
} }
func (dp *DiskProvider) SaveFile(file multipart.File, handler *multipart.FileHeader, path string) bool { func (dp *DiskProvider) SaveFile(file multipart.File, handler *multipart.FileHeader, path string) bool {
f, err := os.OpenFile(dp.Location + path + "/" + handler.Filename, os.O_WRONLY|os.O_CREATE, 0666) filename := strings.Join([]string{dp.Location,path,handler.Filename}, "/")
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
return false return false

View file

@ -57,7 +57,7 @@ func Init() *mux.Router {
HandleProvider(), HandleProvider(),
)).Methods("GET", "POST") )).Methods("GET", "POST")
r.Handle(`/api/files/{provider}/{file:[a-zA-Z0-9=\-\/\s.,&_+]+}`, Handle( r.Handle(`/api/files/{provider:[a-zA-Z0-9]+}/{file:[a-zA-Z0-9=\-\/\s.,&_+]+}`, Handle(
HandleProvider(), HandleProvider(),
)).Methods("GET", "POST") )).Methods("GET", "POST")