use header component to select multiple files to download

This commit is contained in:
Weidi Deng 2020-12-17 11:04:52 +08:00
parent c36860ce65
commit 57e11a5162
10 changed files with 107 additions and 64 deletions

View File

@ -58,7 +58,8 @@ export async function put (url, content = '') {
} }
export function download (format, ...files) { export function download (format, ...files) {
let url = `${baseURL}/api/raw` const isSharing = store.getters['isSharing']
let url = isSharing ? `${baseURL}/api/public/dl/${store.state.shared.hash}` : `${baseURL}/api/raw`
if (files.length === 1) { if (files.length === 1) {
url += removePrefix(files[0]) + '?' url += removePrefix(files[0]) + '?'
@ -78,7 +79,9 @@ export function download (format, ...files) {
url += `algo=${format}&` url += `algo=${format}&`
} }
if (!isSharing) {
url += `auth=${store.state.jwt}` url += `auth=${store.state.jwt}`
}
window.open(url) window.open(url)
} }

View File

@ -36,6 +36,8 @@ export async function fetchJSON (url, opts) {
export function removePrefix (url) { export function removePrefix (url) {
if (url.startsWith('/files')) { if (url.startsWith('/files')) {
url = url.slice(6) url = url.slice(6)
} else if (store.getters['isSharing']) {
url = url.slice(7 + store.state.shared.hash.length)
} }
if (url === '') url = '/' if (url === '') url = '/'

View File

@ -28,7 +28,7 @@
</div> </div>
<!-- This buttons are shown on a dropdown on mobile phones --> <!-- This buttons are shown on a dropdown on mobile phones -->
<div id="dropdown" :class="{ active: showMore }"> <div class="dropdown" :class="{ active: showMore }">
<div v-if="!isListing || !isMobile"> <div v-if="!isListing || !isMobile">
<share-button v-show="showShareButton"></share-button> <share-button v-show="showShareButton"></share-button>
<rename-button v-show="showRenameButton"></rename-button> <rename-button v-show="showRenameButton"></rename-button>
@ -49,6 +49,17 @@
</button> </button>
</div> </div>
</template> </template>
<template v-if="isSharing">
<!-- This buttons are shown on a dropdown on mobile phones -->
<div class="dropdown" :class="{ active: showMore }">
<download-button v-if="sharedSelectedCount > 0"></download-button>
<button @click="toggleSharedMultipleSelection" :aria-label="$t('buttons.selectMultiple')" :title="$t('buttons.selectMultiple')" class="action" >
<i class="material-icons">check_circle</i>
<span>{{ $t('buttons.select') }}</span>
</button>
</div>
</template>
<div v-show="showOverlay" @click="resetPrompts" class="overlay"></div> <div v-show="showOverlay" @click="resetPrompts" class="overlay"></div>
</div> </div>
@ -110,14 +121,17 @@ export default {
'isEditor', 'isEditor',
'isPreview', 'isPreview',
'isListing', 'isListing',
'isLogged' 'isLogged',
'isSharing',
'sharedSelectedCount'
]), ]),
...mapState([ ...mapState([
'req', 'req',
'user', 'user',
'loading', 'loading',
'reload', 'reload',
'multiple' 'multiple',
'shared'
]), ]),
logoURL: () => logoURL, logoURL: () => logoURL,
isExecEnabled: () => enableExec, isExecEnabled: () => enableExec,
@ -156,7 +170,7 @@ export default {
: this.user.perm.create) : this.user.perm.create)
}, },
showMore () { showMore () {
return this.isFiles && this.$store.state.show === 'more' return (this.isFiles || this.isSharing) && this.$store.state.show === 'more'
}, },
showOverlay () { showOverlay () {
return this.showMore return this.showMore
@ -176,6 +190,10 @@ export default {
this.$store.commit('multiple', !this.multiple) this.$store.commit('multiple', !this.multiple)
this.resetPrompts() this.resetPrompts()
}, },
toggleSharedMultipleSelection () {
this.$store.commit('sharedMultiple', !this.shared.multiple)
this.resetPrompts()
},
resetPrompts () { resetPrompts () {
this.$store.commit('closeHovers') this.$store.commit('closeHovers')
} }

View File

@ -3,6 +3,7 @@
<i class="material-icons">file_download</i> <i class="material-icons">file_download</i>
<span>{{ $t('buttons.download') }}</span> <span>{{ $t('buttons.download') }}</span>
<span v-if="selectedCount > 0" class="counter">{{ selectedCount }}</span> <span v-if="selectedCount > 0" class="counter">{{ selectedCount }}</span>
<span v-else-if="isSharing && sharedSelectedCount > 0" class="counter">{{ sharedSelectedCount }}</span>
</button> </button>
</template> </template>
@ -13,12 +14,12 @@ import { files as api } from '@/api'
export default { export default {
name: 'download-button', name: 'download-button',
computed: { computed: {
...mapState(['req', 'selected']), ...mapState(['req', 'selected', 'shared']),
...mapGetters(['isListing', 'selectedCount']) ...mapGetters(['isListing', 'selectedCount', 'isSharing', 'sharedSelectedCount'])
}, },
methods: { methods: {
download: function () { download: function () {
if (!this.isListing) { if (!this.isListing && !this.isSharing) {
api.download(null, this.$route.path) api.download(null, this.$route.path)
return return
} }
@ -28,6 +29,11 @@ export default {
return return
} }
if (this.sharedSelectedCount === 1 && !this.shared.req.items[this.shared.selected[0]].isDir) {
api.download(null, this.shared.req.items[this.shared.selected[0]].url)
return
}
this.$store.commit('showHover', 'download') this.$store.commit('showHover', 'download')
} }
} }

View File

@ -25,12 +25,20 @@ import { files as api } from '@/api'
export default { export default {
name: 'download', name: 'download',
computed: { computed: {
...mapState(['selected', 'req']), ...mapState(['selected', 'req', 'shared']),
...mapGetters(['selectedCount']) ...mapGetters(['selectedCount', 'isSharing' ,'sharedSelectedCount'])
}, },
methods: { methods: {
download: function (format) { download: function (format) {
if (this.selectedCount === 0) { if (this.isSharing) {
let files = []
for (let i of this.shared.selected) {
files.push(this.shared.req.items[i].url)
}
api.download(format, ...files)
} else if (this.selectedCount === 0) {
api.download(format, this.$route.path) api.download(format, this.$route.path)
} else { } else {
let files = [] let files = []

View File

@ -28,7 +28,7 @@
height: 100%; height: 100%;
background-color: rgba(0, 0, 0, 0.1); background-color: rgba(0, 0, 0, 0.1);
} }
#dropdown { .dropdown {
position: fixed; position: fixed;
top: 1em; top: 1em;
right: 1em; right: 1em;
@ -40,23 +40,23 @@
transform-origin: top right; transform-origin: top right;
z-index: 99999; z-index: 99999;
} }
#dropdown > div { .dropdown > div {
display: block; display: block;
} }
#dropdown.active { .dropdown.active {
transform: scale(1); transform: scale(1);
} }
#dropdown .action { .dropdown .action {
display: flex; display: flex;
align-items: center; align-items: center;
border-radius: 0; border-radius: 0;
width: 100%; width: 100%;
} }
#dropdown .action span:not(.counter) { .dropdown .action span:not(.counter) {
display: inline-block; display: inline-block;
padding: .4em; padding: .4em;
} }
#dropdown .counter { .dropdown .counter {
left: 2.25em; left: 2.25em;
} }
#file-selection { #file-selection {

View File

@ -4,6 +4,7 @@ const getters = {
isListing: (state, getters) => getters.isFiles && state.req.isDir, isListing: (state, getters) => getters.isFiles && state.req.isDir,
isEditor: (state, getters) => getters.isFiles && (state.req.type === 'text' || state.req.type === 'textImmutable'), isEditor: (state, getters) => getters.isFiles && (state.req.type === 'text' || state.req.type === 'textImmutable'),
isPreview: state => state.previewMode, isPreview: state => state.previewMode,
isSharing: state => state.shared.loaded && state.route.name === 'Share',
selectedCount: state => state.selected.length, selectedCount: state => state.selected.length,
progress : state => { progress : state => {
if (state.upload.progress.length == 0) { if (state.upload.progress.length == 0) {

View File

@ -26,6 +26,9 @@ const state = {
showConfirm: null, showConfirm: null,
previewMode: false, previewMode: false,
shared: { shared: {
req: {},
hash: '',
loaded: false,
selected: [], selected: [],
multiple: false multiple: false
} }

View File

@ -27,8 +27,12 @@ const mutations = {
state.show = 'success' state.show = 'success'
state.showMessage = value state.showMessage = value
}, },
setLoading: (state, value) => { state.loading = value }, setLoading: (state, value) => {
setReload: (state, value) => { state.reload = value }, state.loading = value
},
setReload: (state, value) => {
state.reload = value
},
setUser: (state, value) => { setUser: (state, value) => {
if (value === null) { if (value === null) {
state.user = null state.user = null
@ -96,7 +100,10 @@ const mutations = {
resetSharedSelected: (state) => { resetSharedSelected: (state) => {
state.shared.selected = [] state.shared.selected = []
}, },
sharedMultiple: (state, value) => (state.shared.multiple = value) sharedMultiple: (state, value) => (state.shared.multiple = value),
updateSharedRequest: (state, value) => (state.shared.req = value),
setSharedHash: (state, value) => (state.shared.hash = value),
toggleSharedLoaded: (state, value) => (state.shared.loaded = value)
} }
export default mutations export default mutations

View File

@ -1,7 +1,7 @@
<template> <template>
<div v-if="loaded"> <div v-if="shared.loaded">
<div id="breadcrumbs"> <div id="breadcrumbs">
<router-link :to="'/share/' + hash" :aria-label="$t('files.home')" :title="$t('files.home')"> <router-link :to="'/share/' + shared.hash" :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>
@ -13,13 +13,13 @@
<div class="share"> <div class="share">
<div class="share__box share__box__info"> <div class="share__box share__box__info">
<div class="share__box__header"> <div class="share__box__header">
{{ file.isDir ? sharedSelectedCount > 0 ? $t('download.downloadSelected') : $t('download.downloadFolder') : $t('download.downloadFile') }} {{ shared.req.isDir ? $t('download.downloadFolder') : $t('download.downloadFile') }}
</div> </div>
<div class="share__box__element share__box__center share__box__icon"> <div class="share__box__element share__box__center share__box__icon">
<i class="material-icons">{{ icon }}</i> <i class="material-icons">{{ icon }}</i>
</div> </div>
<div class="share__box__element"> <div class="share__box__element">
<strong>{{ $t('prompts.displayName') }}</strong> {{ file.name }} <strong>{{ $t('prompts.displayName') }}</strong> {{ shared.req.name }}
</div> </div>
<div class="share__box__element"> <div class="share__box__element">
<strong>{{ $t('prompts.lastModified') }}:</strong> {{ humanTime }} <strong>{{ $t('prompts.lastModified') }}:</strong> {{ humanTime }}
@ -33,19 +33,13 @@
<div class="share__box__element share__box__center"> <div class="share__box__element share__box__center">
<qrcode-vue :value="fullLink" size="200" level="M"></qrcode-vue> <qrcode-vue :value="fullLink" size="200" level="M"></qrcode-vue>
</div> </div>
<div v-if="file.isDir" class="share__box__element share__box__center">
<label>
<input type="checkbox" :checked="shared.multiple" @click="toggleMultipleSelection">
{{ $t('buttons.selectMultiple') }}
</label>
</div> </div>
</div> <div v-if="shared.req.isDir" class="share__box share__box__items">
<div v-if="file.isDir" class="share__box share__box__items"> <div class="share__box__header" v-if="shared.req.isDir">
<div class="share__box__header" v-if="file.isDir">
{{ $t('files.files') }} {{ $t('files.files') }}
</div> </div>
<div id="listing" class="list"> <div id="listing" class="list">
<shared-item v-for="(item) in file.items.slice(0, this.showLimit)" <shared-item v-for="(item) in shared.req.items.slice(0, this.showLimit)"
:key="base64(item.name)" :key="base64(item.name)"
v-bind:index="item.index" v-bind:index="item.index"
v-bind:name="item.name" v-bind:name="item.name"
@ -55,9 +49,16 @@
v-bind:type="item.type" v-bind:type="item.type"
v-bind:size="item.size"> v-bind:size="item.size">
</shared-item> </shared-item>
<div v-if="file.items.length > showLimit" class="item"> <div v-if="shared.req.items.length > showLimit" class="item">
<div> <div>
<p class="name"> + {{ file.items.length - showLimit }} </p> <p class="name"> + {{ shared.req.items.length - showLimit }} </p>
</div>
</div>
<div :class="{ active: $store.state.shared.multiple }" id="multiple-selection">
<p>{{ $t('files.multipleSelectionEnabled') }}</p>
<div @click="$store.commit('sharedMultiple', false)" tabindex="0" role="button" :title="$t('files.clear')" :aria-label="$t('files.clear')" class="action">
<i class="material-icons">clear</i>
</div> </div>
</div> </div>
</div> </div>
@ -82,15 +83,16 @@ export default {
QrcodeVue QrcodeVue
}, },
data: () => ({ data: () => ({
loaded: false,
notFound: false, notFound: false,
file: null, filePath: '',
showLimit: 500 showLimit: 500
}), }),
watch: { watch: {
'$route': 'fetchData' '$route': 'fetchData'
}, },
created: async function () { created: async function () {
const hash = this.$route.params.pathMatch.split('/')[0]
this.setSharedHash(hash)
await this.fetchData() await this.fetchData()
}, },
mounted () { mounted () {
@ -103,17 +105,14 @@ export default {
...mapState(['shared']), ...mapState(['shared']),
...mapGetters(['sharedSelectedCount']), ...mapGetters(['sharedSelectedCount']),
icon: function () { icon: function () {
if (this.file.isDir) return 'folder' if (this.shared.req.isDir) return 'folder'
if (this.file.type === 'image') return 'insert_photo' if (this.shared.req.type === 'image') return 'insert_photo'
if (this.file.type === 'audio') return 'volume_up' if (this.shared.req.type === 'audio') return 'volume_up'
if (this.file.type === 'video') return 'movie' if (this.shared.req.type === 'video') return 'movie'
return 'insert_drive_file' return 'insert_drive_file'
}, },
hash: function () {
return this.$route.params.pathMatch.split('/')[0]
},
path: function () { path: function () {
let absoluteParts = this.file.path.split('/') let absoluteParts = this.filePath.split('/')
let urlParts = this.$route.params.pathMatch.split('/') let urlParts = this.$route.params.pathMatch.split('/')
absoluteParts.shift() absoluteParts.shift()
@ -133,26 +132,20 @@ export default {
return absoluteParts.slice(absoluteParts.length - len).join('/') return absoluteParts.slice(absoluteParts.length - len).join('/')
}, },
link: function () { link: function () {
if (this.sharedSelectedCount === 0) return `${baseURL}/api/public/dl/${this.hash}/${this.path}` return `${baseURL}/api/public/dl/${this.shared.hash}/${this.path}`
if (this.sharedSelectedCount === 1) return `${baseURL}/api/public/dl/${this.hash}/${this.path}/${encodeURIComponent(this.file.items[this.shared.selected[0]].name)}`
let files = []
for (let s of this.shared.selected) {
files.push(encodeURIComponent(this.file.items[s].name))
}
return `${baseURL}/api/public/dl/${this.hash}/${this.path}/?files=${encodeURIComponent(files.join(','))}`
}, },
fullLink: function () { fullLink: function () {
return window.location.origin + this.link return window.location.origin + this.link
}, },
humanSize: function () { humanSize: function () {
if (this.file.isDir) { if (this.shared.req.isDir) {
return this.file.items.length return this.shared.req.items.length
} }
return filesize(this.file.size) return filesize(this.shared.req.size)
}, },
humanTime: function () { humanTime: function () {
return moment(this.file.modified).fromNow() return moment(this.shared.req.modified).fromNow()
}, },
breadcrumbs () { breadcrumbs () {
let parts = this.path.split('/') let parts = this.path.split('/')
@ -169,7 +162,7 @@ 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: '/share/' + this.hash + '/' + parts[i] + '/' }) breadcrumbs.push({ name: decodeURIComponent(parts[i]), url: '/share/' + this.shared.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] + '/' })
} }
@ -189,23 +182,25 @@ export default {
} }
}, },
methods: { methods: {
...mapMutations([ 'resetSharedSelected' ]), ...mapMutations([ 'resetSharedSelected', 'setSharedHash', 'updateSharedRequest', 'toggleSharedLoaded' ]),
base64: function (name) { base64: function (name) {
return window.btoa(unescape(encodeURIComponent(name))) return window.btoa(unescape(encodeURIComponent(name)))
}, },
fetchData: async function () { fetchData: async function () {
this.loaded = false
this.notFound = false this.notFound = false
this.$store.commit('toggleSharedLoaded', false)
this.$store.commit('resetSharedSelected') this.$store.commit('resetSharedSelected')
this.$store.commit('sharedMultiple', false) this.$store.commit('sharedMultiple', false)
try { try {
this.file = await api.getHash(encodeURIComponent(this.$route.params.pathMatch)) let file = await api.getHash(encodeURIComponent(this.$route.params.pathMatch))
if (this.file.isDir) this.file.items = this.file.items.map((item, index) => { this.filePath = file.path
if (file.isDir) file.items = file.items.map((item, index) => {
item.index = index item.index = index
item.url = `/share/${this.hash}/${this.path}/${encodeURIComponent(item.name)}` item.url = `/share/${this.shared.hash}/${this.path}/${encodeURIComponent(item.name)}`
return item return item
}) })
this.loaded = true this.updateSharedRequest(file)
this.$store.commit('toggleSharedLoaded', true)
} catch (e) { } catch (e) {
this.notFound = true this.notFound = true
} }