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

create files for available mimetypes #5890

Closed
wants to merge 2 commits into from
Closed
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
124 changes: 121 additions & 3 deletions packages/web-app-files/src/components/AppBar/AppBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,23 @@
</oc-button>
</div>
</li>
<template v-if="mimeTypes">
<li v-for="(mimetype, key) in mimetypesAllowedForCreation" :key="key">
<div>
<oc-button
appearance="raw"
justify-content="left"
:class="['uk-width-1-1']"
@click="showCreateResourceModal(false, mimetype.ext, false, true)"
>
<oc-icon :name="mimetype.icon || 'file'" />
<translate :translate-params="{ name: mimetype.name }"
>New %{name}</translate
>
</oc-button>
</div>
</li>
</template>
</ul>
</oc-drop>
</template>
Expand All @@ -119,6 +136,7 @@
import { mapActions, mapGetters, mapState, mapMutations } from 'vuex'
import pathUtil from 'path'
import { useRouter } from '../../composables'
import get from 'lodash-es/get'

import Mixins from '../../mixins'
import MixinFileActions, { EDITOR_MODE_CREATE } from '../../mixins/fileActions'
Expand Down Expand Up @@ -158,11 +176,25 @@ export default {
fileFolderCreationLoading: false
}),
computed: {
...mapGetters(['getToken', 'configuration', 'newFileHandlers', 'quota', 'user']),
...mapGetters('External', ['mimeTypes']),
...mapGetters([
'getToken',
'capabilities',
'configuration',
'newFileHandlers',
'quota',
'user'
]),
...mapGetters('Files', ['files', 'currentFolder', 'selectedFiles', 'publicLinkPassword']),
...mapState(['route']),
...mapState('Files', ['areHiddenFilesShown']),

mimetypesAllowedForCreation() {
if (!get(this, 'mimeTypes', []).length) {
return []
}
return this.mimeTypes.filter((mimetype) => mimetype.allow_creation) || []
},
newButtonTooltip() {
if (!this.canUpload) {
return this.$gettext('You have no permission to upload!')
Expand Down Expand Up @@ -313,7 +345,12 @@ export default {
...mapMutations('Files', ['UPSERT_RESOURCE', 'SET_HIDDEN_FILES_VISIBILITY']),
...mapMutations(['SET_QUOTA']),

showCreateResourceModal(isFolder = true, ext = 'txt', openAction = null) {
showCreateResourceModal(
isFolder = true,
ext = 'txt',
openAction = null,
addAppProviderFile = false
) {
const defaultName = isFolder
? this.$gettext('New folder')
: this.$gettext('New file') + '.' + ext
Expand All @@ -340,7 +377,11 @@ export default {
? this.checkNewFolderName(defaultName)
: this.checkNewFileName(defaultName),
onCancel: this.hideModal,
onConfirm: isFolder ? this.addNewFolder : this.addNewFile,
onConfirm: isFolder
? this.addNewFolder
: addAppProviderFile
? this.addAppProviderFile
: this.addNewFile,
onInput: checkInputValue
}

Expand Down Expand Up @@ -489,7 +530,84 @@ export default {

this.fileFolderCreationLoading = false
},
async addAppProviderFile(fileName) {
if (fileName === '') {
return
}
try {
const parent = this.currentFolder.fileId
const publicToken = (this.$router.currentRoute.params.item || '').split('/')[0]

const configUrl = this.configuration.server
const appNewUrl = this.capabilities.files.app_providers[0].new_url.replace(/^\/+/, '')
const url =
configUrl +
appNewUrl +
`?parent_container_id=${parent}&filename=${encodeURIComponent(fileName)}`

const headers = {
'X-Requested-With': 'XMLHttpRequest',
...(this.isPublicFilesRoute && {
'public-token': publicToken
}),
...(this.publicLinkPassword && {
Authorization:
'Basic ' +
Buffer.from(['public', this.publicLinkPassword].join(':')).toString('base64')
}),
...(this.getToken && {
Authorization: 'Bearer ' + this.getToken
})
}

const response = await fetch(url, {
method: 'POST',
headers
})

if (response.status !== 200) {
const message = `An error has occured: ${response.status}`
throw new Error(message)
}

const path = pathUtil.join(this.currentPath, fileName)
let resource
if (this.isSpacesLocation) {
resource = await this.$client.files.fileInfo(path, DavProperties.Default)
} else {
resource = await this.$client.publicFiles.getFileInfo(
path,
this.publicLinkPassword,
DavProperties.PublicLink
)
}
resource = buildResource(resource)

this.$_fileActions_triggerDefaultAction(resource)

this.UPSERT_RESOURCE(resource)
this.hideModal()

if (this.isSpacesLocation) {
this.loadIndicators({
client: this.$client,
currentFolder: this.currentFolder.path
})
}

this.showMessage({
title: this.$gettextInterpolate(this.$gettext('"%{fileName}" was created successfully'), {
fileName
})
})
} catch (error) {
console.error(error)
this.showMessage({
title: this.$gettext('Failed to create file'),
status: 'danger'
})
}
},
checkNewFileName(fileName) {
if (fileName === '') {
return this.$gettext('File name cannot be empty')
Expand Down