Skip to content
Open
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
42 changes: 38 additions & 4 deletions src/components/ComposerAttachment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Comment on lines +6 to +12

Copy link
Copy Markdown

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 onPreview at Line 12. The delete path revokes previewBlobUrl before the parent receives the preview event. The Viewer can then open a revoked URL.

A non-focusable li also prevents keyboard users from starting the preview. Put preview on a dedicated keyboard-operable control. Stop propagation from the delete control.

<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>
Expand Down Expand Up @@ -58,7 +66,7 @@ export default {
},
},

emits: ['onDeleteAttachment'],
emits: ['onDeleteAttachment', 'preview'],

data() {
return {
Expand All @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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' \
  src

Repository: 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 || true

Repository: 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])
PY

Repository: nextcloud/mail

Length of output: 25943


🌐 Web query:

Nextcloud Viewer JavaScript OCA.Viewer.mimetypes open fileInfo MIME support source

💡 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 '@nextcloud/viewer' import CustomView from './CustomView.vue' registerHandler({ id: 'my-viewer-id', group: 'my-group', // Optional grouping for slideshow navigation mimes: ['application/x-custom-type', 'image/x-format'], component: CustomView }) 2. MIME Aliases: If a custom file type should be handled by an existing viewer component, you can use mimesAliases to map one MIME type to another registered MIME [3][4]. { mimesAliases: { 'image/x-dcraw': 'image/jpeg' } } 3. Forcing/Opening with a specific handler: Beyond automatic detection, you can programmatically open a file with a specific handler using OCA.Viewer.openWith [5]. OCA.Viewer.openWith('handler-id', { path: '/path/to/file' }) Implementation Notes: - JavaScript files must be loaded via \OCP\Util::addInitScript in your Nextcloud app to ensure handlers are registered before the Viewer initializes [2]. - The viewer component will automatically receive path and mime props [1][2]. - If multiple handlers are registered for the same MIME type, the Viewer may require additional configuration or server-side priority settings, as native "open with" selector behavior is a frequent topic of development in the project [6].

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
    done

Repository: nextcloud/mail

Length of output: 203


🌐 Web query:

site:github.com/nextcloud/viewer "mimetypes" "OCA.Viewer"

💡 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
done

Repository: 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'
done

Repository: 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 OCA.Viewer.mimetypes.

OCA.Viewer.mimetypes contains the exact MIME types registered by Viewer handlers. OCA.Viewer.open accepts unsupported fileInfo values without checking them. Apply this check in ComposerAttachment.vue#L89-L91, previewableFilesInfos, generatePreviewBlobUrl, and onPreviewAttachment. Otherwise, unsupported media types can receive blob URLs, appear clickable, enter the Viewer list, and fail to render.

📍 Affects 2 files
  • src/components/ComposerAttachment.vue#L89-L91 (this comment)
  • src/components/ComposerAttachments.vue#L162-L183
  • src/components/ComposerAttachments.vue#L530-L541
  • src/components/ComposerAttachments.vue#L547-L572


extension() {
return this.attachment.fileName.split('.').pop()
},
Expand All @@ -91,8 +111,11 @@ export default {
onDelete(attachment) {
this.$emit('onDeleteAttachment', attachment)
},
},

onPreview() {
this.$emit('preview', this.attachment)
},
},
}
</script>

Expand All @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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;
Expand Down
82 changes: 80 additions & 2 deletions src/components/ComposerAttachments.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
:key="attachment.id"
:attachment="attachment"
:uploading="uploading"
@onDeleteAttachment="onDelete(attachment)" />
@onDeleteAttachment="onDelete(attachment)"
@preview="onPreviewAttachment" />
</ul>

<input
Expand All @@ -54,7 +55,7 @@ import ChevronUp from 'vue-material-design-icons/ChevronUp.vue'
import ComposerAttachment from './ComposerAttachment.vue'
import logger from '../logger.js'
import { uploadLocalAttachment } from '../service/AttachmentService.js'
import { getFileData } from '../service/FileService.js'
import { getFileData, getFileDavUrl } from '../service/FileService.js'
import { shareFile } from '../service/FileSharingService.js'

const mimes = [
Expand Down Expand Up @@ -130,6 +131,28 @@ export default {
}
return total
},

previewableFilesInfos() {
return this.attachments
.filter((attachment) => {
const mime = attachment?.mime || attachment?.fileType
return mime
&& (
mime.startsWith('image/')
|| mime.startsWith('video/')
|| mime.startsWith('audio/')
|| mime === 'application/pdf'
)
})
.map((attachment) => ({
filename: attachment?.previewBlobUrl || attachment?.davSource,
source: attachment?.previewBlobUrl || attachment?.davSource,
basename: attachment?.fileName,
mime: attachment?.mime || attachment?.fileType,
etag: attachment?.etag || 'fixme',
hasPreview: attachment?.hasPreview || false,
}))
},
},

watch: {
Expand Down Expand Up @@ -270,6 +293,7 @@ export default {
fileName: file.name,
fileType: file.type,
imageBlobURL: this.generatePreview(file),
previewBlobUrl: this.generatePreviewBlobUrl(file),
displayName: trimStart('/', file.name),
progress: null,
percent: 0,
Expand Down Expand Up @@ -351,6 +375,7 @@ export default {
size: filesFromCloud[i].size,

}

const _toAttachmentData = {
finished: true,
imageBlobURL: this.generatePreview(_cloudFile),
Expand All @@ -360,6 +385,10 @@ export default {
// don't know, may be it will be conflict if cloud & local has equal IDs?
id: filesFromCloud[i].fileid,
uploaded: 0,
mime: filesFromCloud[i].getcontenttype,
etag: filesFromCloud[i].getetag,
davSource: getFileDavUrl(name),

}

this.attachments.push(Object.assign(_toAttachmentData, _cloudFile))
Expand Down Expand Up @@ -420,6 +449,10 @@ export default {
attachment.controller.abort()
}

if (attachment.previewBlobUrl) {
URL.revokeObjectURL(attachment.previewBlobUrl)
}

const val = {
fileName: attachment.fileName,
displayName: attachment.displayName,
Expand Down Expand Up @@ -493,9 +526,54 @@ export default {
}
},

generatePreviewBlobUrl(file) {
return this.isPreviewable(file)
? URL.createObjectURL(file)
: null
},

isPreviewable(file) {
return file.type.startsWith('image/')
|| file.type.startsWith('video/')
|| file.type.startsWith('audio/')
|| file.type === 'application/pdf'
},

isImage(file) {
return file.type && mimes.indexOf(file.type) !== -1
},

onPreviewAttachment(attachment) {
if (!attachment.finished) {
return
}

const isCloudPreviewable = attachment?.type === 'cloud' && attachment?.hasPreview && attachment?.davSource
const isLocalPreviewable = attachment?.type !== 'cloud' && attachment?.previewBlobUrl

if (!isCloudPreviewable && !isLocalPreviewable) {
return
}

if (!OCA.Viewer) {
return
}

const fileInfo = {
filename: attachment?.previewBlobUrl || attachment?.davSource,
source: attachment?.previewBlobUrl || attachment?.davSource,
basename: attachment?.fileName,
mime: attachment?.mime || attachment?.fileType,
etag: attachment?.etag || 'fixme',
hasPreview: attachment?.hasPreview || false,
}

OCA.Viewer.open({
fileInfo,
list: this.previewableFilesInfos,
})
},

},
}
</script>
Expand Down
6 changes: 6 additions & 0 deletions src/service/FileService.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ export async function getFileData(path) {
<oc:size />
<oc:fileid />
<nc:has-preview />
<d:getcontenttype />
<d:getetag />
</d:prop>
</d:propfind>`,
details: true,
})

return response?.data?.props
}

export function getFileDavUrl(path) {
return getClient('files').getFileDownloadLink(path)
}
Loading