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

feat(files): add Viewer Files ressource handler #44012

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(files): add Viewer Files ressource handler
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Mar 7, 2024
commit bfa65cf0cb6875a4e62955be962e082d727f713d
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ registerWidget('file', (el, { richObjectType, richObject, accessible }) => {
accessible,
},
}).$mount(el)
})
}, { hasInteractiveView: true })

registerCustomPickerElement('files', (el, { providerId, accessible }) => {
const Element = Vue.extend(FileReferencePickerElement)
Expand Down
102 changes: 97 additions & 5 deletions apps/files/src/views/ReferenceFileWidget.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@
</p>
</div>
</div>

<!-- Live preview if a handler is available -->
<component :is="viewerHandler.component"
v-else-if="viewerHandler && !failedViewer"
skjnldsv marked this conversation as resolved.
Show resolved Hide resolved
:active="true"
:can-swipe="false"
:can-zoom="false"
:is-embedded="true"
v-bind="viewerFile"
:file-list="[viewerFile]"
:is-full-screen="false"
:is-sidebar-shown="false"
class="widget-file"
@error="failedViewer = true" />

<!-- The file is accessible -->
<a v-else
class="widget-file"
:href="richObject.link"
Expand All @@ -43,28 +59,100 @@
</div>
</a>
</template>
<script>
import { generateUrl } from '@nextcloud/router'

<script lang="ts">
import { defineComponent, type Component, type PropType } from 'vue'
import { generateRemoteUrl, generateUrl } from '@nextcloud/router'
import path from 'path'
import { getCurrentUser } from '@nextcloud/auth'

// see lib/private/Collaboration/Reference/File/FileReferenceProvider.php
type Ressource = {
id: number
name: string
size: number
path: string
link: string
mimetype: string
mtime: number // as unix timestamp
'preview-available': boolean
}

export default {
type ViewerHandler = {
id: string
group: string
mimes: string[]
component: Component
}

/**
* Minimal mock of the legacy Viewer FileInfo
* TODO: replace by Node object
*/
type ViewerFile = {
filename: string // the path to the root folder
basename: string // the file name
lastmod: Date // the last modification date
size: number // the file size in bytes
type: string
mime: string
fileid: number
failed: boolean
loaded: boolean
davPath: string
source: string
}

export default defineComponent({
name: 'ReferenceFileWidget',
props: {
richObject: {
type: Object,
type: Object as PropType<Ressource>,
required: true,
},
accessible: {
type: Boolean,
default: true,
},
interactive: {
type: Bool,
default: true,
}
},

data() {
return {
previewUrl: window.OC.MimeType.getIconUrl(this.richObject.mimetype),
failedViewer: false,
}
},

computed: {
availableViewerHandlers(): ViewerHandler[] {
return (window?.OCA?.Viewer?.availableHandlers || []) as ViewerHandler[]
},
viewerHandler(): ViewerHandler | undefined {
return this.availableViewerHandlers
.find(handler => handler.mimes.includes(this.richObject.mimetype))
},
viewerFile(): ViewerFile {
const davSource = generateRemoteUrl(`dav/files/${getCurrentUser()?.uid}/${this.richObject.path}`)
.replace(/\/\/$/, '/')
return {
filename: this.richObject.path,
basename: this.richObject.name,
lastmod: new Date(this.richObject.mtime * 1000),
size: this.richObject.size,
type: 'file',
mime: this.richObject.mimetype,
fileid: this.richObject.id,
failed: false,
loaded: true,
davPath: davSource,
source: davSource,
}
},

fileSize() {
return window.OC.Util.humanFileSize(this.richObject.size)
},
Expand Down Expand Up @@ -94,6 +182,7 @@ export default {

},
},

mounted() {
if (this.richObject['preview-available']) {
const previewUrl = generateUrl('/core/preview?fileId={fileId}&x=250&y=250', {
Expand All @@ -108,6 +197,8 @@ export default {
}
img.src = previewUrl
}

console.debug('ReferenceFileWidget', this.richObject)
},
methods: {
navigate() {
Expand All @@ -118,8 +209,9 @@ export default {
window.location = this.richObject.link
},
},
}
})
</script>

<style lang="scss" scoped>
.widget-file {
display: flex;
Expand Down
2 changes: 1 addition & 1 deletion webpack.modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = {
main: path.join(__dirname, 'apps/files/src', 'main.ts'),
init: path.join(__dirname, 'apps/files/src', 'init.ts'),
'personal-settings': path.join(__dirname, 'apps/files/src', 'main-personal-settings.js'),
'reference-files': path.join(__dirname, 'apps/files/src', 'reference-files.js'),
'reference-files': path.join(__dirname, 'apps/files/src', 'reference-files.ts'),
},
files_external: {
init: path.join(__dirname, 'apps/files_external/src', 'init.ts'),
Expand Down