aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/newMenu/newTemplatesFolder.ts
blob: bf6862bda0806003c6aacf4e0b5ec02d8d1d725e (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
/**
 * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
import type { Entry, Folder, Node } from '@nextcloud/files'

import { getCurrentUser } from '@nextcloud/auth'
import { showError } from '@nextcloud/dialogs'
import { Permission, removeNewFileMenuEntry } from '@nextcloud/files'
import { loadState } from '@nextcloud/initial-state'
import { translate as t } from '@nextcloud/l10n'
import { generateOcsUrl } from '@nextcloud/router'
import { join } from 'path'
import { newNodeName } from '../utils/newNodeDialog'

import PlusSvg from '@mdi/svg/svg/plus.svg?raw'
import axios from '@nextcloud/axios'
import logger from '../logger.ts'

const templatesEnabled = loadState<boolean>('files', 'templates_enabled', true)
let templatesPath = loadState<string|false>('files', 'templates_path', false)
logger.debug('Templates folder enabled', { templatesEnabled })
logger.debug('Initial templates folder', { templatesPath })

/**
 * Init template folder
 * @param directory Folder where to create the templates folder
 * @param name Name to use or the templates folder
 */
const initTemplatesFolder = async function(directory: Folder, name: string) {
	const templatePath = join(directory.path, name)
	try {
		logger.debug('Initializing the templates directory', { templatePath })
		const { data } = await axios.post(generateOcsUrl('apps/files/api/v1/templates/path'), {
			templatePath,
			copySystemTemplates: true,
		})

		// Go to template directory
		window.OCP.Files.Router.goToRoute(
			null, // use default route
			{ view: 'files', fileid: undefined },
			{ dir: templatePath },
		)

		logger.info('Created new templates folder', {
			...data.ocs.data,
		})
		templatesPath = data.ocs.data.templates_path as string
	} catch (error) {
		logger.error('Unable to initialize the templates directory')
		showError(t('files', 'Unable to initialize the templates directory'))
	}
}

export const entry = {
	id: 'template-picker',
	displayName: t('files', 'Create templates folder'),
	iconSvgInline: PlusSvg,
	order: 30,
	enabled(context: Folder): boolean {
		// Templates disabled or templates folder already initialized
		if (!templatesEnabled || templatesPath) {
			return false
		}
		// Allow creation on your own folders only
		if (context.owner !== getCurrentUser()?.uid) {
			return false
		}
		return (context.permissions & Permission.CREATE) !== 0
	},
	async handler(context: Folder, content: Node[]) {
		const name = await newNodeName(t('files', 'Templates'), content, { name: t('files', 'New template folder') })

		if (name !== null) {
			// Create the template folder
			initTemplatesFolder(context, name)

			// Remove the menu entry
			removeNewFileMenuEntry('template-picker')
		}
	},
} as Entry