feat: rename option on replace prompt

pull/1026/head
Ramires Viana 2020-07-16 19:30:17 +00:00
parent eed9da1471
commit 2636f876ab
9 changed files with 118 additions and 37 deletions

View File

@ -112,25 +112,25 @@ export async function post (url, content = '', overwrite = false, onupload) {
}) })
} }
function moveCopy (items, copy = false, overwrite = false) { function moveCopy (items, copy = false, overwrite = false, rename = false) {
let promises = [] let promises = []
for (let item of items) { for (let item of items) {
const from = removePrefix(item.from) const from = removePrefix(item.from)
const to = encodeURIComponent(removePrefix(item.to)) const to = encodeURIComponent(removePrefix(item.to))
const url = `${from}?action=${copy ? 'copy' : 'rename'}&destination=${to}&override=${overwrite}` const url = `${from}?action=${copy ? 'copy' : 'rename'}&destination=${to}&override=${overwrite}&rename=${rename}`
promises.push(resourceAction(url, 'PATCH')) promises.push(resourceAction(url, 'PATCH'))
} }
return Promise.all(promises) return Promise.all(promises)
} }
export function move (items, overwrite = false) { export function move (items, overwrite = false, rename = false) {
return moveCopy(items, false, overwrite) return moveCopy(items, false, overwrite, rename)
} }
export function copy (items, overwrite = false) { export function copy (items, overwrite = false, rename = false) {
return moveCopy(items, true, overwrite) return moveCopy(items, true, overwrite, rename)
} }
export async function checksum (url, algo) { export async function checksum (url, algo) {

View File

@ -268,15 +268,15 @@ export default {
return return
} }
let action = (overwrite) => { let action = (overwrite, rename) => {
api.copy(items, overwrite).then(() => { api.copy(items, overwrite, rename).then(() => {
this.$store.commit('setReload', true) this.$store.commit('setReload', true)
}).catch(this.$showError) }).catch(this.$showError)
} }
if (this.$store.state.clipboard.key === 'x') { if (this.$store.state.clipboard.key === 'x') {
action = (overwrite) => { action = (overwrite, rename) => {
api.move(items, overwrite).then(() => { api.move(items, overwrite, rename).then(() => {
this.$store.commit('setReload', true) this.$store.commit('setReload', true)
}).catch(this.$showError) }).catch(this.$showError)
} }
@ -284,20 +284,26 @@ export default {
let conflict = upload.checkConflict(items, this.req.items) let conflict = upload.checkConflict(items, this.req.items)
let overwrite = false
let rename = false
if (conflict) { if (conflict) {
this.$store.commit('showHover', { this.$store.commit('showHover', {
prompt: 'replace', prompt: 'replace-rename',
confirm: (event) => { confirm: (event, option) => {
overwrite = option == 'overwrite'
rename = option == 'rename'
event.preventDefault() event.preventDefault()
this.$store.commit('closeHovers') this.$store.commit('closeHovers')
action(true) action(overwrite, rename)
} }
}) })
return return
} }
action(false) action(overwrite, rename)
}, },
resizeEvent () { resizeEvent () {
// Update the columns size based on the window width. // Update the columns size based on the window width.

View File

@ -138,28 +138,34 @@ export default {
let path = this.$route.path + base let path = this.$route.path + base
let baseItems = (await api.fetch(path)).items let baseItems = (await api.fetch(path)).items
let action = (overwrite) => { let action = (overwrite, rename) => {
api.move(items, overwrite).then(() => { api.move(items, overwrite, rename).then(() => {
this.$store.commit('setReload', true) this.$store.commit('setReload', true)
}).catch(this.$showError) }).catch(this.$showError)
} }
let conflict = upload.checkConflict(items, baseItems) let conflict = upload.checkConflict(items, baseItems)
let overwrite = false
let rename = false
if (conflict) { if (conflict) {
this.$store.commit('showHover', { this.$store.commit('showHover', {
prompt: 'replace', prompt: 'replace-rename',
confirm: (event) => { confirm: (event, option) => {
overwrite = option == 'overwrite'
rename = option == 'rename'
event.preventDefault() event.preventDefault()
this.$store.commit('closeHovers') this.$store.commit('closeHovers')
action(true) action(overwrite, rename)
} }
}) })
return return
} }
action(false) action(overwrite, rename)
}, },
click: function (event) { click: function (event) {
if (this.selectedCount !== 0) event.preventDefault() if (this.selectedCount !== 0) event.preventDefault()

View File

@ -54,10 +54,10 @@ export default {
}) })
} }
let action = async (overwrite) => { let action = async (overwrite, rename) => {
buttons.loading('copy') buttons.loading('copy')
await api.copy(items, overwrite).then(() => { await api.copy(items, overwrite, rename).then(() => {
buttons.success('copy') buttons.success('copy')
this.$router.push({ path: this.dest }) this.$router.push({ path: this.dest })
}).catch((e) => { }).catch((e) => {
@ -69,20 +69,26 @@ export default {
let dstItems = (await api.fetch(this.dest)).items let dstItems = (await api.fetch(this.dest)).items
let conflict = upload.checkConflict(items, dstItems) let conflict = upload.checkConflict(items, dstItems)
let overwrite = false
let rename = false
if (conflict) { if (conflict) {
this.$store.commit('showHover', { this.$store.commit('showHover', {
prompt: 'replace', prompt: 'replace-rename',
confirm: (event) => { confirm: (event, option) => {
overwrite = option == 'overwrite'
rename = option == 'rename'
event.preventDefault() event.preventDefault()
this.$store.commit('closeHovers') this.$store.commit('closeHovers')
action(true) action(overwrite, rename)
} }
}) })
return return
} }
action(false) action(overwrite, rename)
} }
} }
} }

View File

@ -52,10 +52,10 @@ export default {
}) })
} }
let action = async (overwrite) => { let action = async (overwrite, rename) => {
buttons.loading('move') buttons.loading('move')
await api.move(items, overwrite).then(() => { await api.move(items, overwrite, rename).then(() => {
buttons.success('move') buttons.success('move')
this.$router.push({ path: this.dest }) this.$router.push({ path: this.dest })
}).catch((e) => { }).catch((e) => {
@ -67,20 +67,26 @@ export default {
let dstItems = (await api.fetch(this.dest)).items let dstItems = (await api.fetch(this.dest)).items
let conflict = upload.checkConflict(items, dstItems) let conflict = upload.checkConflict(items, dstItems)
let overwrite = false
let rename = false
if (conflict) { if (conflict) {
this.$store.commit('showHover', { this.$store.commit('showHover', {
prompt: 'replace', prompt: 'replace-rename',
confirm: (event) => { confirm: (event, option) => {
overwrite = option == 'overwrite'
rename = option == 'rename'
event.preventDefault() event.preventDefault()
this.$store.commit('closeHovers') this.$store.commit('closeHovers')
action(true) action(overwrite, rename)
} }
}) })
return return
} }
action(false) action(overwrite, rename)
} }
} }
} }

View File

@ -16,6 +16,7 @@ import Copy from './Copy'
import NewFile from './NewFile' import NewFile from './NewFile'
import NewDir from './NewDir' import NewDir from './NewDir'
import Replace from './Replace' import Replace from './Replace'
import ReplaceRename from './ReplaceRename'
import Share from './Share' import Share from './Share'
import Upload from './Upload' import Upload from './Upload'
import { mapState } from 'vuex' import { mapState } from 'vuex'
@ -35,6 +36,7 @@ export default {
NewDir, NewDir,
Help, Help,
Replace, Replace,
ReplaceRename,
Upload Upload
}, },
data: function () { data: function () {
@ -87,6 +89,7 @@ export default {
'newDir', 'newDir',
'download', 'download',
'replace', 'replace',
'replace-rename',
'share', 'share',
'upload' 'upload'
].indexOf(this.show) >= 0; ].indexOf(this.show) >= 0;

View File

@ -0,0 +1,35 @@
<template>
<div class="card floating">
<div class="card-title">
<h2>{{ $t('prompts.replace') }}</h2>
</div>
<div class="card-content">
<p>{{ $t('prompts.replaceMessage') }}</p>
</div>
<div class="card-action">
<button class="button button--flat button--grey"
@click="$store.commit('closeHovers')"
:aria-label="$t('buttons.cancel')"
:title="$t('buttons.cancel')">{{ $t('buttons.cancel') }}</button>
<button class="button button--flat button--blue"
@click="(event) => showConfirm(event, 'rename')"
:aria-label="$t('buttons.rename')"
:title="$t('buttons.rename')">{{ $t('buttons.rename') }}</button>
<button class="button button--flat button--red"
@click="(event) => showConfirm(event, 'overwrite')"
:aria-label="$t('buttons.replace')"
:title="$t('buttons.replace')">{{ $t('buttons.replace') }}</button>
</div>
</div>
</template>
<script>
import { mapState } from 'vuex'
export default {
name: 'replace-rename',
computed: mapState(['showConfirm'])
}
</script>

View File

@ -25,8 +25,8 @@
background: var(--red); background: var(--red);
} }
.button--red:hover { .button--blue {
background: var(--dark-red); background: var(--blue);
} }
.button--flat { .button--flat {

View File

@ -148,12 +148,31 @@ var resourcePatchHandler = withUser(func(w http.ResponseWriter, r *http.Request,
return http.StatusForbidden, nil return http.StatusForbidden, nil
} }
if r.URL.Query().Get("override") != "true" { override := r.URL.Query().Get("override") == "true"
if _, err := d.user.Fs.Stat(dst); err == nil { rename := r.URL.Query().Get("rename") == "true"
if !override && !rename {
if _, err = d.user.Fs.Stat(dst); err == nil {
return http.StatusConflict, nil return http.StatusConflict, nil
} }
} }
if rename {
counter := 1
dir, name := filepath.Split(dst)
ext := filepath.Ext(name)
base := strings.TrimSuffix(name, ext)
for {
if _, err = d.user.Fs.Stat(dst); err != nil {
break
}
new := fmt.Sprintf("%s(%d)%s", base, counter, ext)
dst = filepath.Join(dir, new)
counter++
}
}
err = d.RunHook(func() error { err = d.RunHook(func() error {
switch action { switch action {
// TODO: use enum // TODO: use enum