Skip to content

Commit a786e4b

Browse files
Open attachments with viewer
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
1 parent 4c097f6 commit a786e4b

File tree

8 files changed

+85
-66
lines changed

8 files changed

+85
-66
lines changed

lib/Controller/PageController.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,20 @@
3333
use OCA\Mail\Db\TagMapper;
3434
use OCA\Mail\Service\AccountService;
3535
use OCA\Mail\Service\AliasesService;
36+
use OCA\Viewer\Event\LoadViewer;
3637
use OCP\AppFramework\Controller;
3738
use OCP\AppFramework\Http\ContentSecurityPolicy;
3839
use OCP\AppFramework\Http\RedirectResponse;
3940
use OCP\AppFramework\Http\TemplateResponse;
4041
use OCP\AppFramework\Services\IInitialState;
42+
use OCP\EventDispatcher\IEventDispatcher;
4143
use OCP\IConfig;
4244
use OCP\IRequest;
4345
use OCP\IURLGenerator;
4446
use OCP\IUserSession;
4547
use Psr\Log\LoggerInterface;
4648
use Throwable;
49+
use function class_exists;
4750
use function json_decode;
4851

4952
class PageController extends Controller {
@@ -83,6 +86,7 @@ class PageController extends Controller {
8386

8487
/** @var OutboxService */
8588
private $outboxService;
89+
private IEventDispatcher $dispatcher;
8690

8791
public function __construct(string $appName,
8892
IRequest $request,
@@ -97,7 +101,8 @@ public function __construct(string $appName,
97101
TagMapper $tagMapper,
98102
IInitialState $initialStateService,
99103
LoggerInterface $logger,
100-
OutboxService $outboxService) {
104+
OutboxService $outboxService,
105+
IEventDispatcher $dispatcher) {
101106
parent::__construct($appName, $request);
102107

103108
$this->urlGenerator = $urlGenerator;
@@ -112,6 +117,7 @@ public function __construct(string $appName,
112117
$this->initialStateService = $initialStateService;
113118
$this->logger = $logger;
114119
$this->outboxService = $outboxService;
120+
$this->dispatcher = $dispatcher;
115121
}
116122

117123
/**
@@ -121,6 +127,10 @@ public function __construct(string $appName,
121127
* @return TemplateResponse renders the index page
122128
*/
123129
public function index(): TemplateResponse {
130+
if (class_exists(LoadViewer::class)) {
131+
$this->dispatcher->dispatchTyped(new LoadViewer());
132+
}
133+
124134
$this->initialStateService->provideInitialState(
125135
'debug',
126136
$this->config->getSystemValue('debug', false)

lib/Db/MessageMapper.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
use function array_udiff;
4949
use function get_class;
5050
use function ltrim;
51+
use function mb_convert_encoding;
5152
use function mb_strcut;
5253
use function OCA\Mail\array_flat_map;
5354

@@ -496,9 +497,9 @@ public function updatePreviewDataBulk(Message ...$messages): array {
496497
$query->setParameter('uid', $message->getUid(), IQueryBuilder::PARAM_INT);
497498
$query->setParameter('mailbox_id', $message->getMailboxId(), IQueryBuilder::PARAM_INT);
498499
$query->setParameter('flag_attachments', $message->getFlagAttachments(), $message->getFlagAttachments() === null ? IQueryBuilder::PARAM_NULL : IQueryBuilder::PARAM_BOOL);
499-
$previewText = $message->getPreviewText();
500-
if ($previewText !== null) {
501-
$previewText = mb_strcut(mb_convert_encoding($previewText, 'UTF-8', 'UTF-8'), 0, 255);
500+
$previewText = null;
501+
if ($message->getPreviewText() !== null) {
502+
$previewText = mb_strcut(mb_convert_encoding($message->getPreviewText(), 'UTF-8', 'UTF-8'), 0, 255);
502503
}
503504
$query->setParameter(
504505
'preview_text',

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"@nextcloud/l10n": "^1.6.0",
4242
"@nextcloud/logger": "^2.3.0",
4343
"@nextcloud/moment": "^1.2.1",
44+
"@nextcloud/paths": "^2.1.0",
4445
"@nextcloud/router": "^2.0.0",
4546
"@nextcloud/vue": "^6.0.0-beta.6",
4647
"@nextcloud/vue-dashboard": "^2",

src/components/AttachmentImageViewer.vue

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/components/MessageAttachment.vue

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
-->
2121

2222
<template>
23-
<div class="attachment">
23+
<div class="attachment" :class="{'message-attachment--can-preview': canPreview }" @click="$emit('click', $event)">
2424
<img v-if="isImage"
2525
class="mail-attached-image"
26-
:src="url"
27-
@click="$emit('click', $event)">
26+
:src="url">
2827
<img class="attachment-icon" :src="mimeUrl">
2928
<span class="attachment-name"
3029
:title="label">{{ name }}
@@ -140,6 +139,10 @@ export default {
140139
type: Boolean,
141140
default: false,
142141
},
142+
canPreview: {
143+
type: Boolean,
144+
default: false,
145+
},
143146
},
144147
data() {
145148
return {
@@ -240,14 +243,16 @@ export default {
240243
.attachment:hover,
241244
.attachment span:hover {
242245
background-color: var(--color-background-hover);
243-
cursor: pointer;
246+
247+
&.message-attachment--can-preview * {
248+
cursor: pointer;
249+
}
244250
}
245251
246252
.mail-attached-image {
247253
display: block;
248254
max-width: 100%;
249255
border-radius: var(--border-radius);
250-
cursor: pointer;
251256
}
252257
.attachment-import-popover {
253258
right: 32px;

src/components/MessageAttachments.vue

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<div class="mail-message-attachments">
2424
<div class="attachments">
2525
<MessageAttachment
26-
v-for="attachment in attachments"
26+
v-for="(attachment, idx) in attachments"
2727
:id="attachment.id"
2828
:key="attachment.id"
2929
:file-name="attachment.fileName"
@@ -33,10 +33,8 @@
3333
:is-calendar-event="attachment.isCalendarEvent"
3434
:mime="attachment.mime"
3535
:mime-url="attachment.mimeUrl"
36-
@click="showViewer(attachment)" />
37-
<AttachmentImageViewer v-if="attachmentImageURL && showPreview"
38-
:url="attachmentImageURL"
39-
@close="showPreview = false" />
36+
:can-preview="canPreview(fileInfos[idx])"
37+
@click="showViewer(fileInfos[idx])" />
4038
</div>
4139
<p v-if="moreThanOne" class="attachments-button-wrapper">
4240
<ButtonVue
@@ -64,6 +62,7 @@
6462
</template>
6563

6664
<script>
65+
import { basename } from '@nextcloud/paths'
6766
import ButtonVue from '@nextcloud/vue/dist/Components/NcButton'
6867
import IconLoading from '@nextcloud/vue/dist/Components/NcLoadingIcon'
6968
import IconFolder from 'vue-material-design-icons/Folder'
@@ -73,12 +72,10 @@ import { saveAttachmentsToFiles } from '../service/AttachmentService'
7372
7473
import MessageAttachment from './MessageAttachment'
7574
import Logger from '../logger'
76-
import AttachmentImageViewer from './AttachmentImageViewer'
7775
7876
export default {
7977
name: 'MessageAttachments',
8078
components: {
81-
AttachmentImageViewer,
8279
MessageAttachment,
8380
ButtonVue,
8481
IconLoading,
@@ -98,10 +95,27 @@ export default {
9895
return {
9996
savingToCloud: false,
10097
showPreview: false,
101-
attachmentImageURL: '',
10298
}
10399
},
104100
computed: {
101+
fileInfos() {
102+
return this.attachments.map(attachment => ({
103+
filename: attachment.downloadUrl,
104+
source: attachment.downloadUrl,
105+
basename: basename(attachment.downloadUrl),
106+
mime: attachment.mime,
107+
etag: 'fixme',
108+
hasPreview: false,
109+
fileid: parseInt(attachment.id, 10),
110+
}))
111+
},
112+
previewableFileInfos() {
113+
return this.fileInfos.filter(fileInfo => (fileInfo.mime.startsWith('image/')
114+
|| fileInfo.mime.startsWith('video/')
115+
|| fileInfo.mime.startsWith('audio/')
116+
|| fileInfo.mime === 'application/pdf')
117+
&& OCA.Viewer.mimetypes.includes(fileInfo.mime))
118+
},
105119
moreThanOne() {
106120
return this.attachments.length > 1
107121
},
@@ -112,6 +126,9 @@ export default {
112126
},
113127
},
114128
methods: {
129+
canPreview(fileInfo) {
130+
return this.previewableFileInfos.includes(fileInfo)
131+
},
115132
saveAll() {
116133
const picker = getFilePickerBuilder(t('mail', 'Choose a folder to store the attachments in'))
117134
.setMultiSelect(false)
@@ -140,10 +157,16 @@ export default {
140157
downloadZip() {
141158
window.location = this.zipUrl
142159
},
143-
showViewer(attachment) {
144-
if (attachment.isImage) {
145-
this.showPreview = true
146-
this.attachmentImageURL = attachment.downloadUrl
160+
showViewer(fileInfo) {
161+
if (!this.canPreview(fileInfo)) {
162+
return
163+
}
164+
165+
if (this.previewableFileInfos.includes(fileInfo)) {
166+
OCA.Viewer.open({
167+
fileInfo,
168+
list: this.previewableFileInfos,
169+
})
147170
}
148171
},
149172
},

tests/Unit/Controller/PageControllerTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use OCP\AppFramework\Http\RedirectResponse;
3838
use OCP\AppFramework\Http\TemplateResponse;
3939
use OCP\AppFramework\Services\IInitialState;
40+
use OCP\EventDispatcher\IEventDispatcher;
4041
use OCP\IConfig;
4142
use OCP\IRequest;
4243
use OCP\IURLGenerator;
@@ -89,6 +90,9 @@ class PageControllerTest extends TestCase {
8990
/** @var OutboxService|MockObject */
9091
private $outboxService;
9192

93+
/** @var IEventDispatcher|MockObject */
94+
private $eventDispatcher;
95+
9296
/** @var PageController */
9397
private $controller;
9498

@@ -109,6 +113,7 @@ protected function setUp(): void {
109113
$this->initialState = $this->createMock(IInitialState::class);
110114
$this->logger = $this->createMock(LoggerInterface::class);
111115
$this->outboxService = $this->createMock(OutboxService::class);
116+
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
112117

113118
$this->controller = new PageController(
114119
$this->appName,
@@ -124,7 +129,8 @@ protected function setUp(): void {
124129
$this->tagMapper,
125130
$this->initialState,
126131
$this->logger,
127-
$this->outboxService
132+
$this->outboxService,
133+
$this->eventDispatcher,
128134
);
129135
}
130136

0 commit comments

Comments
 (0)