|
|
|
|
@ -50,7 +50,8 @@ import type { TemplateFile } from '../types.ts'
|
|
|
|
|
import { getCurrentUser } from '@nextcloud/auth'
|
|
|
|
|
import { showError, spawnDialog } from '@nextcloud/dialogs'
|
|
|
|
|
import { emit } from '@nextcloud/event-bus'
|
|
|
|
|
import { File } from '@nextcloud/files'
|
|
|
|
|
import { File, Node } from '@nextcloud/files'
|
|
|
|
|
import { getClient, getRootPath, resultToNode, getDefaultPropfind } from '@nextcloud/files/dav'
|
|
|
|
|
import { translate as t } from '@nextcloud/l10n'
|
|
|
|
|
import { generateRemoteUrl } from '@nextcloud/router'
|
|
|
|
|
import { normalize, extname, join } from 'path'
|
|
|
|
|
@ -62,6 +63,7 @@ import NcModal from '@nextcloud/vue/dist/Components/NcModal.js'
|
|
|
|
|
import TemplatePreview from '../components/TemplatePreview.vue'
|
|
|
|
|
import TemplateFiller from '../components/TemplateFiller.vue'
|
|
|
|
|
import logger from '../logger.ts'
|
|
|
|
|
import type { FileStat, ResponseDataDetailed } from 'webdav'
|
|
|
|
|
|
|
|
|
|
const border = 2
|
|
|
|
|
const margin = 8
|
|
|
|
|
@ -165,6 +167,12 @@ export default defineComponent({
|
|
|
|
|
this.name = name
|
|
|
|
|
this.provider = provider
|
|
|
|
|
|
|
|
|
|
// Skip templates logic for external users.
|
|
|
|
|
if (getCurrentUser() === null) {
|
|
|
|
|
this.onSubmit()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const templates = await getTemplates()
|
|
|
|
|
const fetchedProvider = templates.find((fetchedProvider) => fetchedProvider.app === provider.app && fetchedProvider.label === provider.label)
|
|
|
|
|
if (fetchedProvider === null) {
|
|
|
|
|
@ -216,56 +224,80 @@ export default defineComponent({
|
|
|
|
|
this.name = `${this.name}${this.provider?.extension ?? ''}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const fileInfo = await createFromTemplate(
|
|
|
|
|
normalize(`${currentDirectory}/${this.name}`),
|
|
|
|
|
this.selectedTemplate?.filename as string ?? '',
|
|
|
|
|
this.selectedTemplate?.templateType as string ?? '',
|
|
|
|
|
templateFields,
|
|
|
|
|
)
|
|
|
|
|
logger.debug('Created new file', fileInfo)
|
|
|
|
|
|
|
|
|
|
const owner = getCurrentUser()?.uid || null
|
|
|
|
|
const node = new File({
|
|
|
|
|
id: fileInfo.fileid,
|
|
|
|
|
source: generateRemoteUrl(join(`dav/files/${owner}`, fileInfo.filename)),
|
|
|
|
|
root: `/files/${owner}`,
|
|
|
|
|
mime: fileInfo.mime,
|
|
|
|
|
mtime: new Date(fileInfo.lastmod * 1000),
|
|
|
|
|
owner,
|
|
|
|
|
size: fileInfo.size,
|
|
|
|
|
permissions: fileInfo.permissions,
|
|
|
|
|
attributes: {
|
|
|
|
|
// Inherit some attributes from parent folder like the mount type and real owner
|
|
|
|
|
'mount-type': this.parent?.attributes?.['mount-type'],
|
|
|
|
|
'owner-id': this.parent?.attributes?.['owner-id'],
|
|
|
|
|
'owner-display-name': this.parent?.attributes?.['owner-display-name'],
|
|
|
|
|
...fileInfo,
|
|
|
|
|
'has-preview': fileInfo.hasPreview,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
// Create a blank file for external users as we can't use the templates.
|
|
|
|
|
if (getCurrentUser() === null) {
|
|
|
|
|
const client = getClient()
|
|
|
|
|
const filename = join(getRootPath(), currentDirectory, this.name ?? '')
|
|
|
|
|
|
|
|
|
|
await client.putFileContents(filename, '')
|
|
|
|
|
const response = await client.stat(filename, { data: getDefaultPropfind(), details: true }) as ResponseDataDetailed<FileStat>
|
|
|
|
|
logger.debug('Created new file', { fileInfo: response.data })
|
|
|
|
|
|
|
|
|
|
const node = resultToNode(response.data)
|
|
|
|
|
|
|
|
|
|
// Update files list
|
|
|
|
|
emit('files:node:created', node)
|
|
|
|
|
|
|
|
|
|
// Open the new file
|
|
|
|
|
window.OCP.Files.Router.goToRoute(
|
|
|
|
|
null, // use default route
|
|
|
|
|
{ view: 'files', fileid: node.fileid },
|
|
|
|
|
{ dir: node.dirname, openfile: 'true' },
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Close the picker
|
|
|
|
|
this.close()
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error('Error while creating the new file from template', { error })
|
|
|
|
|
showError(t('files', 'Unable to create new file from template'))
|
|
|
|
|
} finally {
|
|
|
|
|
this.loading = false
|
|
|
|
|
this.handleFileCreation(node)
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
const fileInfo = await createFromTemplate(
|
|
|
|
|
normalize(`${currentDirectory}/${this.name}`),
|
|
|
|
|
this.selectedTemplate?.filename as string ?? '',
|
|
|
|
|
this.selectedTemplate?.templateType as string ?? '',
|
|
|
|
|
templateFields,
|
|
|
|
|
)
|
|
|
|
|
logger.debug('Created new file', { fileInfo })
|
|
|
|
|
|
|
|
|
|
const owner = getCurrentUser()?.uid || null
|
|
|
|
|
const node = new File({
|
|
|
|
|
id: fileInfo.fileid,
|
|
|
|
|
source: generateRemoteUrl(join(`dav/files/${owner}`, fileInfo.filename)),
|
|
|
|
|
root: `/files/${owner}`,
|
|
|
|
|
mime: fileInfo.mime,
|
|
|
|
|
mtime: new Date(fileInfo.lastmod * 1000),
|
|
|
|
|
owner,
|
|
|
|
|
size: fileInfo.size,
|
|
|
|
|
permissions: fileInfo.permissions,
|
|
|
|
|
attributes: {
|
|
|
|
|
// Inherit some attributes from parent folder like the mount type and real owner
|
|
|
|
|
'mount-type': this.parent?.attributes?.['mount-type'],
|
|
|
|
|
'owner-id': this.parent?.attributes?.['owner-id'],
|
|
|
|
|
'owner-display-name': this.parent?.attributes?.['owner-display-name'],
|
|
|
|
|
...fileInfo,
|
|
|
|
|
'has-preview': fileInfo.hasPreview,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
this.handleFileCreation(node)
|
|
|
|
|
|
|
|
|
|
// Close the picker
|
|
|
|
|
this.close()
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error('Error while creating the new file from template', { error })
|
|
|
|
|
showError(t('files', 'Unable to create new file from template'))
|
|
|
|
|
} finally {
|
|
|
|
|
this.loading = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
handleFileCreation(node: Node) {
|
|
|
|
|
// Update files list
|
|
|
|
|
emit('files:node:created', node)
|
|
|
|
|
|
|
|
|
|
// Open the new file
|
|
|
|
|
window.OCP.Files.Router.goToRoute(
|
|
|
|
|
null, // use default route
|
|
|
|
|
{ view: 'files', fileid: node.fileid },
|
|
|
|
|
{ dir: node.dirname, openfile: 'true' },
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async onSubmit() {
|
|
|
|
|
// Skip templates logic for external users.
|
|
|
|
|
if (getCurrentUser() === null) {
|
|
|
|
|
this.loading = true
|
|
|
|
|
return this.createFile()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.selectedTemplate?.fields?.length > 0) {
|
|
|
|
|
spawnDialog(TemplateFiller, {
|
|
|
|
|
fields: this.selectedTemplate.fields,
|
|
|
|
|
|