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 = []
for (let item of items) {
const from = removePrefix(item.from)
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'))
}
return Promise.all(promises)
}
export function move (items, overwrite = false) {
return moveCopy(items, false, overwrite)
export function move (items, overwrite = false, rename = false) {
return moveCopy(items, false, overwrite, rename)
}
export function copy (items, overwrite = false) {
return moveCopy(items, true, overwrite)
export function copy (items, overwrite = false, rename = false) {
return moveCopy(items, true, overwrite, rename)
}
export async function checksum (url, algo) {

View File

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

View File

@ -138,28 +138,34 @@ export default {
let path = this.$route.path + base
let baseItems = (await api.fetch(path)).items
let action = (overwrite) => {
api.move(items, overwrite).then(() => {
let action = (overwrite, rename) => {
api.move(items, overwrite, rename).then(() => {
this.$store.commit('setReload', true)
}).catch(this.$showError)
}
let conflict = upload.checkConflict(items, baseItems)
let overwrite = false
let rename = false
if (conflict) {
this.$store.commit('showHover', {
prompt: 'replace',
confirm: (event) => {
prompt: 'replace-rename',
confirm: (event, option) => {
overwrite = option == 'overwrite'
rename = option == 'rename'
event.preventDefault()
this.$store.commit('closeHovers')
action(true)
action(overwrite, rename)
}
})
return
}
action(false)
action(overwrite, rename)
},
click: function (event) {
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')
await api.copy(items, overwrite).then(() => {
await api.copy(items, overwrite, rename).then(() => {
buttons.success('copy')
this.$router.push({ path: this.dest })
}).catch((e) => {
@ -69,20 +69,26 @@ export default {
let dstItems = (await api.fetch(this.dest)).items
let conflict = upload.checkConflict(items, dstItems)
let overwrite = false
let rename = false
if (conflict) {
this.$store.commit('showHover', {
prompt: 'replace',
confirm: (event) => {
prompt: 'replace-rename',
confirm: (event, option) => {
overwrite = option == 'overwrite'
rename = option == 'rename'
event.preventDefault()
this.$store.commit('closeHovers')
action(true)
action(overwrite, rename)
}
})
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')
await api.move(items, overwrite).then(() => {
await api.move(items, overwrite, rename).then(() => {
buttons.success('move')
this.$router.push({ path: this.dest })
}).catch((e) => {
@ -67,20 +67,26 @@ export default {
let dstItems = (await api.fetch(this.dest)).items
let conflict = upload.checkConflict(items, dstItems)
let overwrite = false
let rename = false
if (conflict) {
this.$store.commit('showHover', {
prompt: 'replace',
confirm: (event) => {
prompt: 'replace-rename',
confirm: (event, option) => {
overwrite = option == 'overwrite'
rename = option == 'rename'
event.preventDefault()
this.$store.commit('closeHovers')
action(true)
action(overwrite, rename)
}
})
return
}
action(false)
action(overwrite, rename)
}
}
}

View File

@ -16,6 +16,7 @@ import Copy from './Copy'
import NewFile from './NewFile'
import NewDir from './NewDir'
import Replace from './Replace'
import ReplaceRename from './ReplaceRename'
import Share from './Share'
import Upload from './Upload'
import { mapState } from 'vuex'
@ -35,6 +36,7 @@ export default {
NewDir,
Help,
Replace,
ReplaceRename,
Upload
},
data: function () {
@ -87,6 +89,7 @@ export default {
'newDir',
'download',
'replace',
'replace-rename',
'share',
'upload'
].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);
}
.button--red:hover {
background: var(--dark-red);
.button--blue {
background: var(--blue);
}
.button--flat {

View File

@ -148,12 +148,31 @@ var resourcePatchHandler = withUser(func(w http.ResponseWriter, r *http.Request,
return http.StatusForbidden, nil
}
if r.URL.Query().Get("override") != "true" {
if _, err := d.user.Fs.Stat(dst); err == nil {
override := r.URL.Query().Get("override") == "true"
rename := r.URL.Query().Get("rename") == "true"
if !override && !rename {
if _, err = d.user.Fs.Stat(dst); err == 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 {
switch action {
// TODO: use enum