Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MessageAttachment.vue « components « src - github.com/nextcloud/mail.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18164c29b27af470bc1a3d80962540d40e43e3bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<!--
  - @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  -
  - @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  -
  - @license AGPL-3.0-or-later
  -
  - This program is free software: you can redistribute it and/or modify
  - it under the terms of the GNU Affero General Public License as
  - published by the Free Software Foundation, either version 3 of the
  - License, or (at your option) any later version.
  -
  - This program is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU Affero General Public License for more details.
  -
  - You should have received a copy of the GNU Affero General Public License
  - along with this program.  If not, see <http://www.gnu.org/licenses/>.
  -->

<template>
	<div class="attachment">
		<img v-if="isImage"
			class="mail-attached-image"
			:src="url"
			@click="$emit('click', $event)">
		<img class="attachment-icon" :src="mimeUrl">
		<span class="attachment-name"
			:title="label">{{ name }}
			<span class="attachment-size">({{ humanReadable(size) }})</span>
		</span>
		<Actions :boundaries-element="boundariesElement">
			<ActionButton
				v-if="isCalendarEvent"
				class="attachment-import calendar"
				:icon="{'icon-add': !loadingCalendars, 'icon-loading-small': loadingCalendars}"
				:disabled="loadingCalendars"
				@click.stop="loadCalendars">
				{{ t('mail', 'Import into calendar') }}
			</ActionButton>
			<ActionButton icon="icon-download"
				class="attachment-download"
				@click="download">
				{{ t('mail', 'Download attachment') }}
			</ActionButton>
			<ActionButton
				class="attachment-save-to-cloud"
				:icon="{'icon-folder': !savingToCloud, 'icon-loading-small': savingToCloud}"
				:disabled="savingToCloud"
				@click.stop="saveToCloud">
				{{ t('mail', 'Save to Files') }}
			</ActionButton>
			<div
				v-on-click-outside="closeCalendarPopover"
				class="popovermenu bubble attachment-import-popover hidden"
				:class="{open: showCalendarPopover}">
				<PopoverMenu :menu="calendarMenuEntries" />
			</div>
		</Actions>
	</div>
</template>

<script>

import { formatFileSize } from '@nextcloud/files'
import { translate as t } from '@nextcloud/l10n'
import { getFilePickerBuilder } from '@nextcloud/dialogs'
import { mixin as onClickOutside } from 'vue-on-click-outside'
import PopoverMenu from '@nextcloud/vue/dist/Components/PopoverMenu'
import Actions from '@nextcloud/vue/dist/Components/Actions'
import ActionButton from '@nextcloud/vue/dist/Components/ActionButton'

import Logger from '../logger'

import { downloadAttachment, saveAttachmentToFiles } from '../service/AttachmentService'
import { getUserCalendars, importCalendarEvent } from '../service/DAVService'

export default {
	name: 'MessageAttachment',
	components: {
		PopoverMenu,
		Actions,
		ActionButton,
	},
	mixins: [onClickOutside],
	props: {
		id: {
			type: String,
			required: true,
		},
		fileName: {
			type: String,
			default: t('mail', 'Unnamed'),
			required: false,
		},
		url: {
			type: String,
			required: true,
		},
		size: {
			type: Number,
			required: true,
		},
		mime: {
			type: String,
			required: true,
		},
		mimeUrl: {
			type: String,
			required: true,
		},
		isImage: {
			type: Boolean,
			default: false,
		},
		isCalendarEvent: {
			type: Boolean,
			default: false,
		},
	},
	data() {
		return {
			savingToCloud: false,
			loadingCalendars: false,
			calendars: [],
			showCalendarPopover: false,
		}
	},
	computed: {
		name() {
			if (this.mime === 'message/rfc822') {
				return t('mail', 'Embedded message')
			}
			return this.fileName
		},
		label() {
			if (this.mime === 'message/rfc822') {
				return t('mail', 'Embedded message') + ' (' + formatFileSize(this.size) + ')'
			}
			return this.fileName + ' (' + formatFileSize(this.size) + ')'
		},
		calendarMenuEntries() {
			return this.calendars.map((cal) => {
				return {
					icon: 'icon-add',
					text: cal.displayname,
					action: this.importCalendar(cal.url),
				}
			})
		},
		boundariesElement() {
			return document.querySelector('#content-vue')
		},
	},
	methods: {
		humanReadable(size) {
			return formatFileSize(size)
		},
		saveToCloud() {
			const saveAttachment = (id, attachmentId) => (directory) => {
				return saveAttachmentToFiles(id, attachmentId, directory)
			}
			const id = this.$route.params.threadId
			const picker = getFilePickerBuilder(t('mail', 'Choose a folder to store the attachment in'))
				.setMultiSelect(false)
				.addMimeTypeFilter('httpd/unix-directory')
				.setModal(true)
				.setType(1)
				.allowDirectories(true)
				.build()

			return picker
				.pick()
				.then((dest) => {
					this.savingToCloud = true
					return dest
				})
				.then(saveAttachment(id, this.id))
				.then(() => Logger.info('saved'))
				.catch((e) => Logger.error('not saved', { error: e }))
				.then(() => (this.savingToCloud = false))
		},
		download() {
			window.location = this.url
		},
		loadCalendars() {
			this.loadingCalendars = true
			getUserCalendars().then((calendars) => {
				this.calendars = calendars
				this.showCalendarPopover = true
				this.loadingCalendars = false
			})
		},
		closeCalendarPopover() {
			this.showCalendarPopover = false
		},
		importCalendar(url) {
			return () => {
				downloadAttachment(this.url)
					.then(importCalendarEvent(url))
					.then(() => Logger.info('calendar imported'))
					.catch((e) => Logger.error('import error', { error: e }))
					.then(() => (this.showCalendarPopover = false))
			}
		},
	},
}
</script>

<style lang="scss" scoped>
.attachment {
	position: relative;
	display: inline-block;
	width: calc(100% - 32px);
	padding: 16px;
}

.attachment:hover,
.attachment span:hover {
	background-color: var(--color-background-hover);
	cursor: pointer;
}

.mail-attached-image {
	display: block;
	max-width: 100%;
	border-radius: var(--border-radius);
	cursor: pointer;
}
.attachment-import-popover {
	right: 32px;
	top: 42px;
}
.mail-attached-image:hover {
	opacity: 0.8;
}
.attachment-name {
	display: inline-block;
	width: calc(100% - 72px);
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	vertical-align: middle;
	margin-bottom: 20px;
}

/* show attachment size less prominent */
.attachment-size {
	-ms-filter: 'progid:DXImageTransform.Microsoft.Alpha(Opacity=50)';
	opacity: 0.5;
}

.attachment-icon {
	vertical-align: middle;
	text-align: left;
	margin-bottom: 20px;
}
.action-item {
	display: inline-block !important;
	position: relative !important;
}
.mail-message-attachments {
	overflow-x: auto;
	overflow-y: auto;
}
</style>