|
19 | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | 20 | * |
21 | 21 | */ |
22 | | -import { type Entry, addNewFileMenuEntry, Permission, Folder, View } from '@nextcloud/files' |
| 22 | +import type { Entry, Node } from '@nextcloud/files' |
| 23 | + |
| 24 | +import { addNewFileMenuEntry, Permission, Folder } from '@nextcloud/files' |
| 25 | +import { basename, extname } from 'path' |
| 26 | +import { emit } from '@nextcloud/event-bus' |
| 27 | +import { getCurrentUser } from '@nextcloud/auth' |
| 28 | +import { showSuccess } from '@nextcloud/dialogs' |
23 | 29 | import { translate as t } from '@nextcloud/l10n' |
| 30 | +import axios from '@nextcloud/axios' |
24 | 31 | import FolderPlusSvg from '@mdi/svg/svg/folder-plus.svg?raw' |
| 32 | +import Vue from 'vue' |
| 33 | + |
| 34 | +type createFolderResponse = { |
| 35 | + fileid: number |
| 36 | + source: string |
| 37 | +} |
| 38 | + |
| 39 | +const createNewFolder = async (root: string, name: string): Promise<createFolderResponse> => { |
| 40 | + const source = root + '/' + name |
| 41 | + const response = await axios({ |
| 42 | + method: 'MKCOL', |
| 43 | + url: source, |
| 44 | + headers: { |
| 45 | + Overwrite: 'F', |
| 46 | + }, |
| 47 | + }) |
| 48 | + return { |
| 49 | + fileid: parseInt(response.headers['oc-fileid']), |
| 50 | + source, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// TODO: move to @nextcloud/files |
| 55 | +export const getUniqueName = (name: string, names: string[]): string => { |
| 56 | + let newName = name |
| 57 | + let i = 1 |
| 58 | + while (names.includes(newName)) { |
| 59 | + const ext = extname(name) |
| 60 | + newName = `${basename(name, ext)} (${i++})${ext}` |
| 61 | + } |
| 62 | + return newName |
| 63 | +} |
25 | 64 |
|
26 | 65 | const entry = { |
27 | 66 | id: 'newFolder', |
28 | 67 | displayName: t('files', 'New folder'), |
29 | 68 | if: (context: Folder) => (context.permissions & Permission.CREATE) !== 0, |
30 | 69 | iconSvgInline: FolderPlusSvg, |
31 | | - handler(context: Folder, view: View) { |
32 | | - console.debug(context, view) |
| 70 | + async handler(context: Folder, content: Node[]) { |
| 71 | + const contentNames = content.map((node: Node) => node.basename) |
| 72 | + const name = getUniqueName(t('files', 'New Folder'), contentNames) |
| 73 | + const { fileid, source } = await createNewFolder(context.source, name) |
| 74 | + |
| 75 | + // Create the folder in the store |
| 76 | + const folder = new Folder({ |
| 77 | + source, |
| 78 | + id: fileid, |
| 79 | + mtime: new Date(), |
| 80 | + owner: getCurrentUser()?.uid || null, |
| 81 | + permissions: Permission.ALL, |
| 82 | + root: context?.root || '/files/' + getCurrentUser()?.uid, |
| 83 | + }) |
| 84 | + |
| 85 | + if (!context._children) { |
| 86 | + Vue.set(context, '_children', []) |
| 87 | + } |
| 88 | + context._children.push(folder.fileid) |
| 89 | + |
| 90 | + showSuccess(t('files', 'Created new folder "{name}"', { name: basename(source) })) |
| 91 | + emit('files:node:created', folder) |
| 92 | + emit('files:node:rename', folder) |
33 | 93 | }, |
34 | 94 | } as Entry |
35 | 95 |
|
|
0 commit comments