remove unnecessary method
refine url
This commit is contained in:
Weidi Deng 2020-12-12 14:53:11 +08:00
parent 7c56617072
commit e700f6e9b7
3 changed files with 39 additions and 52 deletions

View File

@ -1,7 +1,7 @@
<template> <template>
<div v-if="loaded"> <div v-if="loaded">
<div id="breadcrumbs"> <div id="breadcrumbs">
<router-link :to="'/share/' + rootHash" :aria-label="$t('files.home')" :title="$t('files.home')"> <router-link :to="'/share/' + hash + '/' + this.root" :aria-label="$t('files.home')" :title="$t('files.home')">
<i class="material-icons">home</i> <i class="material-icons">home</i>
</router-link> </router-link>
@ -48,8 +48,8 @@
<div class="item" v-for="(item) in file.items.slice(0, this.showLimit)" :key="base64(item.name)" <div class="item" v-for="(item) in file.items.slice(0, this.showLimit)" :key="base64(item.name)"
:aria-selected="selected.includes(item.name)" :aria-selected="selected.includes(item.name)"
@click="click(item.name)" @click="click(item.name)"
@dblclick="dblclick(item.name)" @dblclick="dblclick(item.path)"
@touchstart="touchstart(item.name)" @touchstart="touchstart(item.path)"
> >
<div> <div>
<i class="material-icons">{{ item.isDir ? 'folder' : (item.type==='image') ? 'insert_photo' : 'insert_drive_file' }}</i> <i class="material-icons">{{ item.isDir ? 'folder' : (item.type==='image') ? 'insert_photo' : 'insert_drive_file' }}</i>
@ -89,13 +89,15 @@ export default {
multiple: false, multiple: false,
touches: 0, touches: 0,
selected: [], selected: [],
firstSelected: -1 firstSelected: -1,
root: ''
}), }),
watch: { watch: {
'$route': 'fetchData' '$route': 'fetchData'
}, },
created: function () { created: async function () {
this.fetchData() await this.fetchData()
this.root = this.file.path.split('/')[1]
}, },
mounted () { mounted () {
window.addEventListener('keydown', this.keyEvent) window.addEventListener('keydown', this.keyEvent)
@ -108,20 +110,17 @@ export default {
return this.selected.length > 0 return this.selected.length > 0
}, },
hash: function () { hash: function () {
if (this.$route.params.pathMatch[this.$route.params.pathMatch.length - 1] !== '/') return this.$route.params.pathMatch + '/' return this.$route.params.pathMatch.split('/')[0]
return this.$route.params.pathMatch
},
rootHash: function () {
return this.$route.params.pathMatch.split('/')[0] + '/'
}, },
link: function () { link: function () {
if (!this.hasSelected) return `${baseURL}/api/public/dl/${this.hash}${encodeURI(this.file.name)}` let path = this.file.path.endsWith('/') ? this.file.path.slice(0, this.file.path.length - 1) : this.file.path
if (this.selected.length === 1) return `${baseURL}/api/public/raw/${this.hash}${encodeURI(this.selected[0])}` if (!this.hasSelected) return `${baseURL}/api/public/dl/${this.hash}${path}`
if (this.selected.length === 1) return `${baseURL}/api/public/dl/${this.hash}${path}/${encodeURI(this.selected[0])}`
let files = [] let files = []
for (let s of this.selected) { for (let s of this.selected) {
files.push('/' + encodeURI(s) + '/') files.push(encodeURI(s))
} }
return `${baseURL}/api/public/raw/${this.hash}?files=${encodeURIComponent(files.join(','))}` return `${baseURL}/api/public/dl/${this.hash}${path}/?files=${encodeURIComponent(files.join(','))}`
}, },
fullLink: function () { fullLink: function () {
return window.location.origin + this.link return window.location.origin + this.link
@ -137,7 +136,7 @@ export default {
return moment(this.file.modified).fromNow() return moment(this.file.modified).fromNow()
}, },
breadcrumbs () { breadcrumbs () {
let parts = this.$route.path.split('/') let parts = this.file.path.split('/')
if (parts[0] === '') { if (parts[0] === '') {
parts.shift() parts.shift()
@ -151,13 +150,12 @@ export default {
for (let i = 0; i < parts.length; i++) { for (let i = 0; i < parts.length; i++) {
if (i === 0) { if (i === 0) {
breadcrumbs.push({ name: decodeURIComponent(parts[i]), url: '/' + parts[i] + '/' }) breadcrumbs.push({ name: decodeURIComponent(parts[i]), url: '/share/' + this.hash + '/' + parts[i] + '/' })
} else { } else {
breadcrumbs.push({ name: decodeURIComponent(parts[i]), url: breadcrumbs[i - 1].url + parts[i] + '/' }) breadcrumbs.push({ name: decodeURIComponent(parts[i]), url: breadcrumbs[i - 1].url + parts[i] + '/' })
} }
} }
breadcrumbs.shift()
breadcrumbs.shift() breadcrumbs.shift()
if (breadcrumbs.length > 3) { if (breadcrumbs.length > 3) {
@ -183,12 +181,15 @@ export default {
this.selected = [] this.selected = []
this.firstSelected = -1 this.firstSelected = -1
try { try {
this.file = await api.getHash(this.hash) this.file = await api.getHash(this.$route.params.pathMatch)
this.loaded = true this.loaded = true
} catch (e) { } catch (e) {
this.notFound = true this.notFound = true
} }
}, },
fileItemsIndexOf: function (name) {
return this.file.items.indexOf(this.file.items.filter(item => item.name === name)[0])
},
addSelected: function(name) { addSelected: function(name) {
this.selected.push(name) this.selected.push(name)
}, },
@ -197,7 +198,7 @@ export default {
if (i === -1) return if (i === -1) return
this.selected.splice(i, 1) this.selected.splice(i, 1)
if (i === 0 && this.hasSelected) { if (i === 0 && this.hasSelected) {
this.firstSelected = this.file.items.indexOf(this.file.items.filter(item => item.name === this.selected[0])[0]) this.firstSelected = this.fileItemsIndexOf(this.selected[0])
} }
}, },
resetSelected: function () { resetSelected: function () {
@ -211,7 +212,7 @@ export default {
return return
} }
let index = this.file.items.indexOf(this.file.items.filter(item => item.name === name)[0]) let index = this.fileItemsIndexOf(name)
if (event.shiftKey && this.hasSelected) { if (event.shiftKey && this.hasSelected) {
let fi = 0 let fi = 0
let la = 0 let la = 0
@ -237,17 +238,17 @@ export default {
if (this.firstSelected === -1) this.firstSelected = index if (this.firstSelected === -1) this.firstSelected = index
this.addSelected(name) this.addSelected(name)
}, },
dblclick: function (name) { dblclick: function (path) {
this.$router.push({path: `/share/${this.hash}${name}`}) this.$router.push({path: `/share/${this.hash}${path}`})
}, },
touchstart (name) { touchstart (path) {
setTimeout(() => { setTimeout(() => {
this.touches = 0 this.touches = 0
}, 300) }, 300)
this.touches++ this.touches++
if (this.touches > 1) { if (this.touches > 1) {
this.dblclick(name) this.dblclick(path)
} }
}, },
keyEvent (event) { keyEvent (event) {

View File

@ -67,7 +67,6 @@ func NewHandler(imgSvc ImgService, fileCache FileCache, store *storage.Storage,
public := api.PathPrefix("/public").Subrouter() public := api.PathPrefix("/public").Subrouter()
public.PathPrefix("/dl").Handler(monkey(publicDlHandler, "/api/public/dl/")).Methods("GET") public.PathPrefix("/dl").Handler(monkey(publicDlHandler, "/api/public/dl/")).Methods("GET")
public.PathPrefix("/share").Handler(monkey(publicShareHandler, "/api/public/share/")).Methods("GET") public.PathPrefix("/share").Handler(monkey(publicShareHandler, "/api/public/share/")).Methods("GET")
public.PathPrefix("/raw").Handler(monkey(publicRawHandler, "/api/public/raw/")).Methods("GET")
return stripPrefix(server.BaseURL, r), nil return stripPrefix(server.BaseURL, r), nil
} }

View File

@ -7,13 +7,13 @@ import (
"github.com/filebrowser/filebrowser/v2/files" "github.com/filebrowser/filebrowser/v2/files"
) )
var withHashFile = func(fn handleFunc, trim bool) handleFunc { var withHashFile = func(fn handleFunc) handleFunc {
return func(w http.ResponseWriter, r *http.Request, d *data) (int, error) { return func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
id, path := ifPathWithName(r, trim) id, path := ifPathWithName(r)
link, err := d.store.Share.GetByHash(id) link, err := d.store.Share.GetByHash(id)
if err != nil { if err != nil {
return errToStatus(err), err return errToStatus(err), err
} }
user, err := d.store.Users.Get(d.server.Root, link.UserID) user, err := d.store.Users.Get(d.server.Root, link.UserID)
if err != nil { if err != nil {
@ -40,18 +40,14 @@ var withHashFile = func(fn handleFunc, trim bool) handleFunc {
// ref to https://github.com/filebrowser/filebrowser/pull/727 // ref to https://github.com/filebrowser/filebrowser/pull/727
// `/api/public/dl/MEEuZK-v/file-name.txt` for old browsers to save file with correct name // `/api/public/dl/MEEuZK-v/file-name.txt` for old browsers to save file with correct name
func ifPathWithName(r *http.Request, trim bool) (string, string) { func ifPathWithName(r *http.Request) (string, string) {
pathElements := strings.Split(r.URL.Path, "/") pathElements := strings.Split(r.URL.Path, "/")
// prevent maliciously constructed parameters like `/api/public/dl/XZzCDnK2_not_exists_hash_name` // prevent maliciously constructed parameters like `/api/public/dl/XZzCDnK2_not_exists_hash_name`
// len(pathElements) will be 1, and golang will panic `runtime error: index out of range` // len(pathElements) will be 1, and golang will panic `runtime error: index out of range`
if len(pathElements) < 2 { //nolint: mnd if len(pathElements) <= 2 { //nolint: mnd
return r.URL.Path, "" return pathElements[0], ""
} }
id := pathElements[0] return pathElements[0], strings.Join(pathElements[2:], "/")
if trim {
return id, strings.Join(pathElements[1:len(pathElements)-1], "/")
}
return id, strings.Join(pathElements[1:], "/")
} }
var publicShareHandler = withHashFile(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) { var publicShareHandler = withHashFile(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
@ -64,7 +60,7 @@ var publicShareHandler = withHashFile(func(w http.ResponseWriter, r *http.Reques
} }
return renderJSON(w, r, file) return renderJSON(w, r, file)
}, false) })
var publicDlHandler = withHashFile(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) { var publicDlHandler = withHashFile(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
file := d.raw.(*files.FileInfo) file := d.raw.(*files.FileInfo)
@ -73,13 +69,4 @@ var publicDlHandler = withHashFile(func(w http.ResponseWriter, r *http.Request,
} }
return rawDirHandler(w, r, d, file) return rawDirHandler(w, r, d, file)
}, true) })
var publicRawHandler = withHashFile(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
file := d.raw.(*files.FileInfo)
if !file.IsDir {
return rawFileHandler(w, r, file)
}
return rawDirHandler(w, r, d, file)
}, false)