Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable22] Fix file creation from template without ext #28881

Merged
merged 3 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/files/js/dist/files-app-settings.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/files/js/dist/files-app-settings.js.map

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions apps/files/js/dist/personal-settings.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/files/js/dist/personal-settings.js.map

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions apps/files/js/dist/sidebar.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/files/js/dist/sidebar.js.map

Large diffs are not rendered by default.

17 changes: 8 additions & 9 deletions apps/files/js/dist/templates.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/files/js/dist/templates.js.map

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions apps/files/src/services/Templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,20 @@ export const getTemplates = async function() {
const response = await axios.get(generateOcsUrl('apps/files/api/v1', 2) + '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', 2) + 'create', {
filePath,
templatePath,
templateType,
})

return response.data.ocs.data
}
27 changes: 16 additions & 11 deletions apps/files/src/views/TemplatePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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', 2) + '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)
Expand Down