aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/newMenu/newFolder.ts
blob: 37dcf6d3d895f87aa818cd797fb87f1b6ab3811e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
 * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
 *
 * @author John Molakvoæ <skjnldsv@protonmail.com>
 *
 * @license AGPL-3.0-or-later
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
import type { Entry, Node } from '@nextcloud/files'

import { basename } from 'path'
import { emit } from '@nextcloud/event-bus'
import { getCurrentUser } from '@nextcloud/auth'
import { Permission, Folder } from '@nextcloud/files'
import { showSuccess } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import axios from '@nextcloud/axios'

import FolderPlusSvg from '@mdi/svg/svg/folder-plus.svg?raw'

import { getUniqueName } from '../utils/fileUtils.ts'
import logger from '../logger'

type createFolderResponse = {
	fileid: number
	source: string
}

const createNewFolder = async (root: Folder, name: string): Promise<createFolderResponse> => {
	const source = root.source + '/' + name
	const encodedSource = root.encodedSource + '/' + encodeURIComponent(name)

	const response = await axios({
		method: 'MKCOL',
		url: encodedSource,
		headers: {
			Overwrite: 'F',
		},
	})
	return {
		fileid: parseInt(response.headers['oc-fileid']),
		source,
	}
}

export const entry = {
	id: 'newFolder',
	displayName: t('files', 'New folder'),
	enabled: (context: Folder) => (context.permissions & Permission.CREATE) !== 0,
	iconSvgInline: FolderPlusSvg,
	order: 0,
	async handler(context: Folder, content: Node[]) {
		const contentNames = content.map((node: Node) => node.basename)
		const name = getUniqueName(t('files', 'New folder'), contentNames)
		const { fileid, source } = await createNewFolder(context, name)

		// Create the folder in the store
		const folder = new Folder({
			source,
			id: fileid,
			mtime: new Date(),
			owner: getCurrentUser()?.uid || null,
			permissions: Permission.ALL,
			root: context?.root || '/files/' + getCurrentUser()?.uid,
		})

		showSuccess(t('files', 'Created new folder "{name}"', { name: basename(source) }))
		logger.debug('Created new folder', { folder, source })
		emit('files:node:created', folder)
		emit('files:node:rename', folder)
	},
} as Entry