Fix file creation from template without ext

Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
This commit is contained in:
John Molakvoæ 2021-09-17 09:52:02 +02:00
parent 62a814f4fb
commit ed8aacf415
No known key found for this signature in database
GPG key ID: 60C25B8C072916CF
10 changed files with 64 additions and 44 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -27,3 +27,19 @@ export const getTemplates = async function() {
const response = await axios.get(generateOcsUrl('apps/files/api/v1/templates'))
return response.data.ocs.data
}
/**
* Create a new file from a specified template
*
* @param {string} filePath The new file destination path
* @param {string} templatePath The template source path
* @param {string} templateType The template type e.g 'user'
*/
export const createFromTemplate = async function(filePath, templatePath, templateType) {
const response = await axios.post(generateOcsUrl('apps/files/api/v1/templates/create'), {
filePath,
templatePath,
templateType,
})
return response.data.ocs.data
}

View file

@ -66,14 +66,13 @@
</template>
<script>
import { generateOcsUrl } from '@nextcloud/router'
import { normalize } from 'path'
import { showError } from '@nextcloud/dialogs'
import axios from '@nextcloud/axios'
import EmptyContent from '@nextcloud/vue/dist/Components/EmptyContent'
import Modal from '@nextcloud/vue/dist/Components/Modal'
import { getCurrentDirectory } from '../utils/davUtils'
import { getTemplates } from '../services/Templates'
import { createFromTemplate, getTemplates } from '../services/Templates'
import TemplatePreview from '../components/TemplatePreview'
const border = 2
@ -113,7 +112,9 @@ export default {
* @returns {string}
*/
nameWithoutExt() {
return this.name.indexOf('.') > -1 ? this.name.split('.').slice(0, -1).join('.') : this.name
return this.name.indexOf('.') > -1
? this.name.split('.').slice(0, -1).join('.')
: this.name
},
emptyTemplate() {
@ -198,14 +199,18 @@ export default {
const currentDirectory = getCurrentDirectory()
const fileList = OCA?.Files?.App?.currentFileList
try {
const response = await axios.post(generateOcsUrl('apps/files/api/v1/templates/create'), {
filePath: `${currentDirectory}/${this.name}`,
templatePath: this.selectedTemplate?.filename,
templateType: this.selectedTemplate?.templateType,
})
// If the file doesn't have an extension, add the default one
if (this.nameWithoutExt === this.name) {
this.logger.debug('Fixed invalid filename', { name: this.name, extension: this.provider?.extension })
this.name = this.name + this.provider?.extension
}
const fileInfo = response.data.ocs.data
try {
const fileInfo = await createFromTemplate(
normalize(`${currentDirectory}/${this.name}`),
this.selectedTemplate?.filename,
this.selectedTemplate?.templateType,
)
this.logger.debug('Created new file', fileInfo)
await fileList?.addAndFetchFileInfo(this.name)