@@ -52,7 +52,8 @@ import type { TemplateFile } from '../types.ts'
5252import { getCurrentUser } from ' @nextcloud/auth'
5353import { showError , spawnDialog } from ' @nextcloud/dialogs'
5454import { emit } from ' @nextcloud/event-bus'
55- import { File } from ' @nextcloud/files'
55+ import { File , Node } from ' @nextcloud/files'
56+ import { getClient , getRootPath , resultToNode , getDefaultPropfind } from ' @nextcloud/files/dav'
5657import { translate as t } from ' @nextcloud/l10n'
5758import { generateRemoteUrl } from ' @nextcloud/router'
5859import { normalize , extname , join } from ' path'
@@ -64,6 +65,7 @@ import NcModal from '@nextcloud/vue/dist/Components/NcModal.js'
6465import TemplatePreview from ' ../components/TemplatePreview.vue'
6566import TemplateFiller from ' ../components/TemplateFiller.vue'
6667import logger from ' ../logger.ts'
68+ import type { FileStat , ResponseDataDetailed } from ' webdav'
6769
6870const border = 2
6971const margin = 8
@@ -167,6 +169,12 @@ export default defineComponent({
167169 this .name = name
168170 this .provider = provider
169171
172+ // Skip templates logic for external users.
173+ if (getCurrentUser () === null ) {
174+ this .onSubmit ()
175+ return
176+ }
177+
170178 const templates = await getTemplates ()
171179 const fetchedProvider = templates .find ((fetchedProvider ) => fetchedProvider .app === provider .app && fetchedProvider .label === provider .label )
172180 if (fetchedProvider === null ) {
@@ -224,56 +232,80 @@ export default defineComponent({
224232 this .name = ` ${this .name }${this .provider ?.extension ?? ' ' } `
225233 }
226234
227- try {
228- const fileInfo = await createFromTemplate (
229- normalize (` ${currentDirectory }/${this .name } ` ),
230- this .selectedTemplate ?.filename as string ?? ' ' ,
231- this .selectedTemplate ?.templateType as string ?? ' ' ,
232- templateFields ,
233- )
234- logger .debug (' Created new file' , fileInfo )
235-
236- const owner = getCurrentUser ()?.uid || null
237- const node = new File ({
238- id: fileInfo .fileid ,
239- source: generateRemoteUrl (join (` dav/files/${owner } ` , fileInfo .filename )),
240- root: ` /files/${owner } ` ,
241- mime: fileInfo .mime ,
242- mtime: new Date (fileInfo .lastmod * 1000 ),
243- owner ,
244- size: fileInfo .size ,
245- permissions: fileInfo .permissions ,
246- attributes: {
247- // Inherit some attributes from parent folder like the mount type and real owner
248- ' mount-type' : this .parent ?.attributes ?.[' mount-type' ],
249- ' owner-id' : this .parent ?.attributes ?.[' owner-id' ],
250- ' owner-display-name' : this .parent ?.attributes ?.[' owner-display-name' ],
251- ... fileInfo ,
252- ' has-preview' : fileInfo .hasPreview ,
253- },
254- })
235+ // Create a blank file for external users as we can't use the templates.
236+ if (getCurrentUser () === null ) {
237+ const client = getClient ()
238+ const filename = join (getRootPath (), currentDirectory , this .name ?? ' ' )
239+
240+ await client .putFileContents (filename , ' ' )
241+ const response = await client .stat (filename , { data: getDefaultPropfind (), details: true }) as ResponseDataDetailed <FileStat >
242+ logger .debug (' Created new file' , { fileInfo: response .data })
243+
244+ const node = resultToNode (response .data )
255245
256- // Update files list
257- emit (' files:node:created' , node )
258-
259- // Open the new file
260- window .OCP .Files .Router .goToRoute (
261- null , // use default route
262- { view: ' files' , fileid: node .fileid },
263- { dir: node .dirname , openfile: ' true' },
264- )
265-
266- // Close the picker
267- this .close ()
268- } catch (error ) {
269- logger .error (' Error while creating the new file from template' , { error })
270- showError (t (' files' , ' Unable to create new file from template' ))
271- } finally {
272- this .loading = false
246+ this .handleFileCreation (node )
247+ } else {
248+ try {
249+ const fileInfo = await createFromTemplate (
250+ normalize (` ${currentDirectory }/${this .name } ` ),
251+ this .selectedTemplate ?.filename as string ?? ' ' ,
252+ this .selectedTemplate ?.templateType as string ?? ' ' ,
253+ templateFields ,
254+ )
255+ logger .debug (' Created new file' , { fileInfo })
256+
257+ const owner = getCurrentUser ()?.uid || null
258+ const node = new File ({
259+ id: fileInfo .fileid ,
260+ source: generateRemoteUrl (join (` dav/files/${owner } ` , fileInfo .filename )),
261+ root: ` /files/${owner } ` ,
262+ mime: fileInfo .mime ,
263+ mtime: new Date (fileInfo .lastmod * 1000 ),
264+ owner ,
265+ size: fileInfo .size ,
266+ permissions: fileInfo .permissions ,
267+ attributes: {
268+ // Inherit some attributes from parent folder like the mount type and real owner
269+ ' mount-type' : this .parent ?.attributes ?.[' mount-type' ],
270+ ' owner-id' : this .parent ?.attributes ?.[' owner-id' ],
271+ ' owner-display-name' : this .parent ?.attributes ?.[' owner-display-name' ],
272+ ... fileInfo ,
273+ ' has-preview' : fileInfo .hasPreview ,
274+ },
275+ })
276+
277+ this .handleFileCreation (node )
278+
279+ // Close the picker
280+ this .close ()
281+ } catch (error ) {
282+ logger .error (' Error while creating the new file from template' , { error })
283+ showError (t (' files' , ' Unable to create new file from template' ))
284+ } finally {
285+ this .loading = false
286+ }
273287 }
274288 },
275289
290+ handleFileCreation(node : Node ) {
291+ // Update files list
292+ emit (' files:node:created' , node )
293+
294+ // Open the new file
295+ window .OCP .Files .Router .goToRoute (
296+ null , // use default route
297+ { view: ' files' , fileid: node .fileid },
298+ { dir: node .dirname , openfile: ' true' },
299+ )
300+ },
301+
276302 async onSubmit() {
303+ // Skip templates logic for external users.
304+ if (getCurrentUser () === null ) {
305+ this .loading = true
306+ return this .createFile ()
307+ }
308+
277309 const fileId = this .selectedTemplate ?.fileid
278310
279311 // Only request field extraction if there is a valid template
0 commit comments