Repository navigation
feat: add preview support for local composer attachments #13161
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,17 @@ | |
| - SPDX-License-Identifier: AGPL-3.0-or-later | ||
| --> | ||
| <template> | ||
| <li class="composer-attachment" :class="{ 'composer-attachment--with-error': attachment.error }"> | ||
| <li | ||
| class="composer-attachment" | ||
| :class="{ | ||
| 'composer-attachment--with-error': attachment.error, | ||
| 'composer-attachment--previewable': isPreviewable, | ||
| }" | ||
| @click="onPreview"> | ||
| <div class="attachment-preview"> | ||
| <img :src="previewSrc" class="attachment-preview-image"> | ||
| <img | ||
| :src="previewSrc" | ||
| class="attachment-preview-image"> | ||
| <span v-if="attachment.type === 'cloud'" class="cloud-attachment-icon"> | ||
| <Cloud :size="20" /> | ||
| </span> | ||
|
|
@@ -58,7 +66,7 @@ export default { | |
| }, | ||
| }, | ||
|
|
||
| emits: ['onDeleteAttachment'], | ||
| emits: ['onDeleteAttachment', 'preview'], | ||
|
|
||
| data() { | ||
| return { | ||
|
|
@@ -82,6 +90,18 @@ export default { | |
| return OC.MimeType.getIconUrl(this.attachment.fileType) | ||
| }, | ||
|
|
||
| isPreviewable() { | ||
| if (!this.attachment.finished || !OCA?.Viewer) { | ||
| return false | ||
| } | ||
|
|
||
| if (this.attachment.type === 'cloud') { | ||
| return !!this.attachment.hasPreview && !!this.attachment.davSource | ||
| } | ||
|
|
||
| return !!this.attachment.previewBlobUrl | ||
| }, | ||
|
Comment on lines
+93
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
# Verify the Viewer MIME contract and all Composer preview call sites.
rg -n -C 5 \
-g '*.js' -g '*.vue' \
'OCA\.Viewer\.(open|mimetypes)|mimetypes\.includes|previewableFilesInfos|isPreviewable' \
srcRepository: nextcloud/mail Length of output: 5295 🏁 Script executed: #!/bin/bash
set -euo pipefail
printf '%s\n' '--- ComposerAttachment.vue ---'
sed -n '1,120p' src/components/ComposerAttachment.vue
printf '%s\n' '--- ComposerAttachments.vue: computed and preview methods ---'
sed -n '140,195p' src/components/ComposerAttachments.vue
sed -n '500,580p' src/components/ComposerAttachments.vue
printf '%s\n' '--- AttachementMixin.js ---'
sed -n '1,60p' src/mixins/AttachementMixin.js
printf '%s\n' '--- Viewer references and MIME configuration ---'
rg -n -C 4 \
-g '*.js' -g '*.vue' -g '*.json' -g '*.php' \
'Viewer\.mimetypes|mimetypes\s*=|mimetypes:|OCA\.Viewer|previewBlobUrl|onPreviewAttachment|onPreview' \
src package.json composer.json 2>/dev/null || trueRepository: nextcloud/mail Length of output: 17610 🏁 Script executed: #!/bin/bash
set -euo pipefail
printf '%s\n' '--- ComposerAttachments tests and usages ---'
rg -n -C 5 \
-g '*.{spec,test}.{js,ts}' -g '*.vue' -g '*.js' \
'ComposerAttachments|ComposerAttachment|generatePreviewBlobUrl|previewableFilesInfos|onPreviewAttachment|isPreviewable' \
. 2>/dev/null || true
printf '%s\n' '--- Attachment creation and cleanup lifecycle ---'
sed -n '260,325p' src/components/ComposerAttachments.vue
sed -n '425,465p' src/components/ComposerAttachments.vue
rg -n -C 3 \
'previewBlobUrl|fileType|mime' \
src/components/ComposerAttachments.vue src/components/ComposerAttachment.vue
printf '%s\n' '--- Viewer dependency/configuration references ---'
rg -n -i -C 4 \
'viewer|mimetypes' \
package.json package-lock.json yarn.lock pnpm-lock.yaml composer.json src tests 2>/dev/null || true
printf '%s\n' '--- Deterministic MIME-set comparison ---'
python3 - <<'PY'
viewer_mimetypes = {
'image/png',
'video/mp4',
'audio/mpeg',
'application/pdf',
}
composer_mimetypes = [
'image/png',
'image/heic',
'video/mp4',
'video/x-msvideo',
'audio/mpeg',
'audio/flac',
'application/pdf',
]
accepted = lambda mime: (
mime.startswith('image/')
or mime.startswith('video/')
or mime.startswith('audio/')
or mime == 'application/pdf'
)
print('accepted_by_composer_but_not_viewer:',
[mime for mime in composer_mimetypes if accepted(mime) and mime not in viewer_mimetypes])
PYRepository: nextcloud/mail Length of output: 25943 🌐 Web query:
💡 Result: In the Nextcloud Viewer, the OCA.Viewer object provides a centralized API for managing file viewers and MIME type support. To integrate a custom viewer for specific file types, you should use the OCA.Viewer.registerHandler method [1][2]. Key mechanisms for MIME support and registration include: 1. Registering a Handler: You can register a new viewer component by providing an object containing a unique id, a Vue component to render the file, and an array of supported mimes [1][2]. import { registerHandler } from ' Citations:
🏁 Script executed: #!/bin/bash
set -euo pipefail
tree_json="$(mktemp)"
curl -fsSL 'https://api.github.com/repos/nextcloud/viewer/git/trees/master?recursive=1' > "$tree_json"
jq -r '.tree[] | select(.path | test("\\.(js|ts|vue)$")) | .path' "$tree_json" \
| while IFS= read -r path; do
case "$path" in
*Viewer.vue|*viewer*.js|*Viewer*.js|*main.js|*index.js|*api*.js|*api*.ts)
printf '%s\n' "$path"
;;
esac
done
printf '%s\n' '--- Relevant Viewer source references ---'
jq -r '.tree[] | select(.path | test("\\.(js|ts|vue)$")) | .path' "$tree_json" \
| while IFS= read -r path; do
case "$path" in
*Viewer.vue|*viewer*.js|*Viewer*.js|*main.js|*index.js|*api*.js|*api*.ts)
content="$(curl -fsSL "https://raw.githubusercontent.com/nextcloud/viewer/master/$path" || true)"
if printf '%s' "$content" | rg -q 'mimetypes|OCA\.Viewer|open\s*\('; then
printf '%s\n' "--- $path ---"
printf '%s\n' "$content" | rg -n -C 5 'mimetypes|OCA\.Viewer|open\s*\('
fi
;;
esac
doneRepository: nextcloud/mail Length of output: 203 🌐 Web query:
💡 Result: In the context of the Nextcloud Viewer application, OCA.Viewer is the global JavaScript object used to manage and register handlers for different file types [1][2][3]. When developers want to add support for viewing a new file type within the Nextcloud interface, they use OCA.Viewer.registerHandler to associate specific MIME types with a custom Vue.js component [1][3][4]. Key aspects of how OCA.Viewer handles MIME types include: Registration: Developers register a handler by providing an object that includes a unique ID, a group (e.g., 'media'), a list of supported MIME types, and the Vue component responsible for rendering the file [1][3][4]. Example structure: OCA.Viewer.registerHandler({ id: 'example-viewer', group: 'media', mimes: ['image/example', 'application/x-example'], component: ExampleViewComponent }); MIME Matching: When a user opens a file, the Viewer checks the file's MIME type against the registered handlers to determine which component should be used to display it [2]. Handling Unsupported Types: If no handler is registered for a specific MIME type, the viewer may default to a download action or fail to display the file [2][5]. Evolution: The API has evolved over different Nextcloud versions [3]. While registration was historically based primarily on MIME types, there have been requests and developments aimed at supporting registration via file extensions for files that lack standardized MIME types [6][4]. For technical implementation, developers are advised to ensure OCA.Viewer is available before attempting to register handlers, often by checking the object's existence or using appropriate event listeners (e.g., waiting for the DOM to be ready) [3][7]. Citations:
🏁 Script executed: #!/bin/bash
set -euo pipefail
for path in \
src/views/Viewer.vue \
src/main.js \
src/main.ts \
src/index.js \
src/index.ts \
src/services/Viewer.js \
src/services/viewer.js
do
url="https://raw.githubusercontent.com/nextcloud/viewer/master/$path"
body="$(curl -fsSL "$url" 2>/dev/null || true)"
if [ -n "$body" ] && printf '%s\n' "$body" | rg -q 'mimetypes|open\s*\(|registerHandler'; then
printf '%s\n' "--- $path ---"
printf '%s\n' "$body" | rg -n -C 6 'mimetypes|open\s*\(|registerHandler'
fi
doneRepository: nextcloud/mail Length of output: 7224 🏁 Script executed: #!/bin/bash
set -euo pipefail
for spec in \
'https://raw.githubusercontent.com/nextcloud/viewer/master/src/services/Viewer.js:280:350' \
'https://raw.githubusercontent.com/nextcloud/viewer/master/src/views/Viewer.vue:700:880' \
'https://raw.githubusercontent.com/nextcloud/viewer/master/src/views/Viewer.vue:880:1010'
do
url="${spec%%:*}"
rest="${spec#*:}"
start="${rest%%:*}"
end="${rest##*:}"
printf '%s\n' "--- $url lines $start-$end ---"
curl -fsSL "$url" | nl -ba | sed -n "${start},${end}p" \
| rg -n -C 8 'open\s*\(|fileInfo|registeredHandlers|handler|mimetype|mime'
doneRepository: nextcloud/mail Length of output: 337 🏁 Script executed: #!/bin/bash
set -euo pipefail
fetch() {
url="$1"
for attempt in 1 2 3; do
body="$(curl -fsSL "$url" 2>/dev/null || true)"
if [ -n "$body" ]; then
printf '%s\n' "$body"
return 0
fi
sleep 1
done
return 1
}
for path in \
src/services/Viewer.js \
src/handlers/Images.js \
src/handlers/Videos.js \
src/handlers/Audios.js
do
printf '%s\n' "--- $path ---"
fetch "https://raw.githubusercontent.com/nextcloud/viewer/master/$path" \
| grep -n -E -C 6 'mimes|mimetypes|registerHandler|open|fileInfo|handler' || true
done
printf '%s\n' '--- Viewer open contract ---'
fetch 'https://raw.githubusercontent.com/nextcloud/viewer/master/src/services/Viewer.js' \
| awk 'NR >= 290 && NR <= 345 { print NR ":" $0 }'Repository: nextcloud/mail Length of output: 11872 Gate Composer previews by
📍 Affects 2 files
|
||
|
|
||
| extension() { | ||
| return this.attachment.fileName.split('.').pop() | ||
| }, | ||
|
|
@@ -91,8 +111,11 @@ export default { | |
| onDelete(attachment) { | ||
| this.$emit('onDeleteAttachment', attachment) | ||
| }, | ||
| }, | ||
|
|
||
| onPreview() { | ||
| this.$emit('preview', this.attachment) | ||
| }, | ||
| }, | ||
| } | ||
| </script> | ||
|
|
||
|
|
@@ -112,6 +135,17 @@ export default { | |
| opacity: 0.5; | ||
| } | ||
|
|
||
| &--previewable { | ||
| cursor: pointer; | ||
| .attachment-preview, | ||
| .attachment-preview img, | ||
| .attachment-inner, | ||
| .new-message-attachment-name, | ||
| .new-message-attachment-size { | ||
| cursor: pointer; | ||
| } | ||
|
Comment on lines
+140
to
+146
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: looks a bit fragile. is there no other way to make the cursor apply for all sub elements?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me investigate and see |
||
| } | ||
|
|
||
| .cloud-attachment-icon { | ||
| position:absolute; | ||
| z-index: 2; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Separate the preview action from the delete action.
A click on the delete button at Line 35 bubbles to
onPreviewat Line 12. The delete path revokespreviewBlobUrlbefore the parent receives the preview event. The Viewer can then open a revoked URL.A non-focusable
lialso prevents keyboard users from starting the preview. Put preview on a dedicated keyboard-operable control. Stop propagation from the delete control.