|
2 | 2 | * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors |
3 | 3 | * SPDX-License-Identifier: AGPL-3.0-or-later |
4 | 4 | */ |
5 | | -import { defineStore } from 'pinia' |
6 | | -import { subscribe } from '@nextcloud/event-bus' |
7 | 5 | import type { Node } from '@nextcloud/files' |
8 | 6 | import type { RenamingStore } from '../types' |
9 | 7 |
|
| 8 | +import axios, { isAxiosError } from '@nextcloud/axios' |
| 9 | +import { emit, subscribe } from '@nextcloud/event-bus' |
| 10 | +import { NodeStatus } from '@nextcloud/files' |
| 11 | +import { t } from '@nextcloud/l10n' |
| 12 | +import { basename, dirname } from 'path' |
| 13 | +import { defineStore } from 'pinia' |
| 14 | +import logger from '../logger' |
| 15 | +import Vue from 'vue' |
| 16 | + |
10 | 17 | export const useRenamingStore = function(...args) { |
11 | 18 | const store = defineStore('renaming', { |
12 | 19 | state: () => ({ |
13 | 20 | renamingNode: undefined, |
14 | 21 | newName: '', |
15 | 22 | } as RenamingStore), |
| 23 | + |
| 24 | + actions: { |
| 25 | + /** |
| 26 | + * Execute the renaming. |
| 27 | + * This will rename the node set as `renamingNode` to the configured new name `newName`. |
| 28 | + * @return true if success, false if skipped (e.g. new and old name are the same) |
| 29 | + * @throws Error if renaming fails, details are set in the error message |
| 30 | + */ |
| 31 | + async rename(): Promise<boolean> { |
| 32 | + if (this.renamingNode === undefined) { |
| 33 | + throw new Error('No node is currently being renamed') |
| 34 | + } |
| 35 | + |
| 36 | + const newName = this.newName.trim?.() || '' |
| 37 | + const oldName = this.renamingNode.basename |
| 38 | + const oldEncodedSource = this.renamingNode.encodedSource |
| 39 | + if (oldName === newName) { |
| 40 | + return false |
| 41 | + } |
| 42 | + |
| 43 | + const node = this.renamingNode |
| 44 | + Vue.set(node, 'status', NodeStatus.LOADING) |
| 45 | + |
| 46 | + try { |
| 47 | + // rename the node |
| 48 | + this.renamingNode.rename(newName) |
| 49 | + logger.debug('Moving file to', { destination: this.renamingNode.encodedSource, oldEncodedSource }) |
| 50 | + // create MOVE request |
| 51 | + await axios({ |
| 52 | + method: 'MOVE', |
| 53 | + url: oldEncodedSource, |
| 54 | + headers: { |
| 55 | + Destination: this.renamingNode.encodedSource, |
| 56 | + Overwrite: 'F', |
| 57 | + }, |
| 58 | + }) |
| 59 | + |
| 60 | + // Success 🎉 |
| 61 | + emit('files:node:updated', this.renamingNode as Node) |
| 62 | + emit('files:node:renamed', this.renamingNode as Node) |
| 63 | + emit('files:node:moved', { |
| 64 | + node: this.renamingNode as Node, |
| 65 | + oldSource: `${dirname(this.renamingNode.source)}/${oldName}`, |
| 66 | + }) |
| 67 | + this.$reset() |
| 68 | + return true |
| 69 | + } catch (error) { |
| 70 | + logger.error('Error while renaming file', { error }) |
| 71 | + // Rename back as it failed |
| 72 | + this.renamingNode.rename(oldName) |
| 73 | + if (isAxiosError(error)) { |
| 74 | + // TODO: 409 means current folder does not exist, redirect ? |
| 75 | + if (error?.response?.status === 404) { |
| 76 | + throw new Error(t('files', 'Could not rename "{oldName}", it does not exist any more', { oldName })) |
| 77 | + } else if (error?.response?.status === 412) { |
| 78 | + throw new Error(t( |
| 79 | + 'files', |
| 80 | + 'The name "{newName}" is already used in the folder "{dir}". Please choose a different name.', |
| 81 | + { |
| 82 | + newName, |
| 83 | + dir: basename(this.renamingNode.dirname), |
| 84 | + }, |
| 85 | + )) |
| 86 | + } |
| 87 | + } |
| 88 | + // Unknown error |
| 89 | + throw new Error(t('files', 'Could not rename "{oldName}"', { oldName })) |
| 90 | + } finally { |
| 91 | + Vue.set(node, 'status', undefined) |
| 92 | + } |
| 93 | + }, |
| 94 | + }, |
16 | 95 | }) |
17 | 96 |
|
18 | 97 | const renamingStore = store(...args) |
|
0 commit comments