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

ComposerAttachments.vue « components « src - github.com/nextcloud/mail.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a8c5899a12f6fc930a2dee40eee2091ed5fed79d (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<!--
  - @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  - @copyright 2020 Gary Kim <gary@garykim.dev>
  -
  - @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  -
  - @license GNU AGPL version 3 or any later version
  -
  - 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="new-message-attachments">
		<div v-if="hasNextLine" class="message-attachments-count" @click="isToggle = !isToggle">
			<span>
				{{ n('mail', '{count} attachment', '{count} attachments', attachments.length, { count: attachments.length }) }} ({{ formatBytes(totalSizeOfUpload()) }})
			</span>
			<ChevronUp v-if="isToggle" :size="24" />
			<ChevronDown v-if="!isToggle" :size="24" />
		</div>
		<ul class="new-message-attachments--list" :class="isToggle ? 'hide' : (hasNextLine ? 'active' : '')">
			<ComposerAttachment
				v-for="attachment in attachments"
				ref="attachments"
				:key="attachment.id"
				:bus="bus"
				:attachment="attachment"
				:uploading="uploading"
				@on-delete-attachment="onDelete(attachment)" />
		</ul>

		<input ref="localAttachments"
			type="file"
			multiple
			style="display: none;"
			@change="onLocalAttachmentSelected">
	</div>
</template>

<script>
import map from 'lodash/fp/map'
import trimStart from 'lodash/fp/trimCharsStart'
import { getRequestToken } from '@nextcloud/auth'
import { formatFileSize } from '@nextcloud/files'
import prop from 'lodash/fp/prop'
import { getFilePickerBuilder, showWarning } from '@nextcloud/dialogs'
import sumBy from 'lodash/fp/sumBy'
import { translate as t, translatePlural as n } from '@nextcloud/l10n'

import Vue from 'vue'

import logger from '../logger'
import { getFileData } from '../service/FileService'
import { shareFile } from '../service/FileSharingService'
import { uploadLocalAttachment } from '../service/AttachmentService'

import ComposerAttachment from './ComposerAttachment.vue'

import ChevronDown from 'vue-material-design-icons/ChevronDown'
import ChevronUp from 'vue-material-design-icons/ChevronUp'

const mimes = [
	'image/gif',
	'image/jpeg',
	'image/pjpeg',
	'image/png',
	'image/webp',
]

export default {
	name: 'ComposerAttachments',
	components: {
		ComposerAttachment,
		ChevronDown,
		ChevronUp,
	},
	props: {
		value: {
			type: Array,
			required: true,
		},
		bus: {
			type: Object,
			required: true,
		},
		uploadSizeLimit: {
			type: Number,
			default: 0,
		},
	},
	data() {
		return {
			uploading: false,
			uploads: {},
			// this need if we want to pass in value only corrected uploaded files
			attachments: [],
			isToggle: false,
			hasNextLine: false,
		}
	},
	computed: {
		uploadProgress() {
			let uploaded = 0
			let total = 0
			for (const id in this.uploads) {
				uploaded += this.uploads[id].uploaded
				total += this.uploads[id].total
			}
			return ((uploaded / total) * 100).toFixed(1)
		},
		total() {
			let total = 0
			for (const id in this.uploads) {
				total += this.uploads[id].total
			}
			return total
		},
	},
	watch: {
		attachments() {
			this.$nextTick(function() {
				let prevTop = null
				this.$refs.attachments.some((attachment, i) => {
					const top = attachment.$el.getBoundingClientRect().top
					if (prevTop !== null && prevTop !== top) {
						if (!this.hasNextLine) {
							this.isToggle = true
							this.hasNextLine = true
						}
						return true
					} else {
						prevTop = top
						if (this.$refs.attachments.length === i + 1) {
							this.hasNextLine = false
							this.isToggle = false
							return true
						}
					}
					return false
				})
			})
		},
	},
	created() {
		this.bus.$on('onAddLocalAttachment', this.onAddLocalAttachment)
		this.bus.$on('onAddCloudAttachment', this.onAddCloudAttachment)
		this.bus.$on('onAddCloudAttachmentLink', this.onAddCloudAttachmentLink)
		this.value.map(attachment => {
			this.attachments.push({
				id: attachment.id,
				fileName: attachment.fileName,
				displayName: trimStart('/', attachment.fileName),
				total: attachment.size,
				finished: true,
				sizeString: this.formatBytes(attachment.size),
				imageBlobURL: attachment.isImage ? attachment.downloadUrl : attachment.mimeUrl,
			})
			return attachment
		})
	},
	methods: {
		onAddLocalAttachment() {
			this.$refs.localAttachments.click()
		},
		emitNewAttachments(attachments) {
			this.$emit('input', this.value.concat(attachments))
		},
		totalSizeOfUpload() {
			return Object.values(this.value).reduce((acc, upload) => {
				if (!upload.type === 'local') {
					// Ignore link shares
					return acc
				}

				return acc + upload.size
			}, 0)
		},
		onLocalAttachmentSelected(e) {
			this.uploading = true
			// BUG - if choose again - progress lost/ move to complete()
			Vue.set(this, 'uploads', {})

			const toUpload = sumBy(prop('size'), Object.values(e.target.files))
			const newTotal = toUpload + this.totalSizeOfUpload()
			logger.debug('checking upload size limit', {
				existingUploads: this.totalSizeOfUpload(),
				toUpload,
				limit: this.uploadSizeLimit,
				newTotal,
			})
			if (this.uploadSizeLimit && newTotal > this.uploadSizeLimit) {
				this.showAttachmentFileSizeWarning(e.target.files.length)
				this.uploading = false
				return
			}

			const progress = (id) => (prog, uploaded) => {
				this.uploads[id].uploaded = uploaded
				this.attachments.map((item, i) => {
					if (item.displayName === id) {
						this.attachments[i].progress = uploaded
						this.changeProgress(item, uploaded)
					}
					return item
				})

			}
			// TODO bug: cancel axios on close or delete attachment
			const promises = map((file) => {
				const controller = new AbortController()
				this.attachments.push({
					fileName: file.name,
					fileType: file.type,
					imageBlobURL: this.generatePreview(file),
					displayName: trimStart('/', file.name),
					progress: null,
					percent: 0,
					total: file.size,
					finished: false,
					error: false,
					hasPreview: false,
					controller,
				})

				Vue.set(this.uploads, file.name, {
					total: file.size,
					uploaded: 0,
				})
				try {
					return uploadLocalAttachment(file, progress(file.name), controller)
						.catch(() => {
							this.attachments.some(attachment => {
								if (attachment.displayName === file.name && !attachment.error) {
									this.$set(attachment, 'error', true)
									return true
								}
								return false
							})
						})
						.then(({ file, id }) => {
							logger.info('uploaded')
							this.attachments.some((attachment, i) => {
								if (attachment.displayName === file.name) {
									this.attachments[i].id = id
									this.attachments[i].finished = true
									return true
								}
								return false
							})

							this.emitNewAttachments([{
								fileName: file.name,
								displayName: trimStart('/', file.name),
								id,
								size: file.size,
								type: 'local',
							}])
						})
				} catch (error) {

				}
			}, e.target.files)

			const done = Promise.all(promises)
				.catch((error) => logger.error('could not upload all attachments', { error }))
				.then(() => (this.uploading = false))

			this.$emit('upload', done)

			return done
		},
		async onAddCloudAttachment() {
			const picker = getFilePickerBuilder(t('mail', 'Choose a file to add as attachment')).setMultiSelect(true).build()

			try {
				const paths = await picker.pick(t('mail', 'Choose a file to add as attachment'))
				// maybe fiiled front with placeholder loader...?
				const filesFromCloud = await Promise.all(paths.map(getFileData))

				const sum = filesFromCloud.reduce((sum, item) => {
					return sum + item.size
				}, 0)

				const newTotal = sum + this.totalSizeOfUpload()

				if (this.uploadSizeLimit && newTotal > this.uploadSizeLimit) {
					this.showAttachmentFileSizeWarning(paths.length)

					return
				}

				this.emitNewAttachments(paths.map((name, i) => {
					const _cloudFile = {
						fileName: name,
						displayName: trimStart('/', name),
						type: 'cloud',
						size: filesFromCloud[i].size,

					}
					const _toAttachmentData = {
						finished: true,
						imageBlobURL: this.generatePreview(_cloudFile),
						total: filesFromCloud[i].size,
						sizeString: this.formatBytes(filesFromCloud[i].size),
						hasPreview: filesFromCloud[i]['has-preview'],
						// dont know, may be it will be conflict if cloud & local has equal IDs?
						id: filesFromCloud[i].fileid,
						uploaded: 0,
					}

					this.attachments.push(Object.assign(_toAttachmentData, _cloudFile))

					return _cloudFile
				}))
			} catch (error) {
				logger.error('could not choose a file as attachment', { error })
			}
		},
		async onAddCloudAttachmentLink() {
			const picker = getFilePickerBuilder(t('mail', 'Choose a file to share as a link')).build()

			try {
				const path = await picker.pick(t('mail', 'Choose a file to share as a link'))
				const url = await shareFile(path, getRequestToken())

				this.appendToBodyAtCursor(`<a href="${url}">${url}</a>`)
			} catch (error) {
				logger.error('could not choose a file as attachment link', { error })
			}
		},
		showAttachmentFileSizeWarning(num) {
			showWarning(n(
				'mail',
				'The attachment exceed the allowed attachments size of {size}. Please share the file via link instead.',
				'The attachments exceed the allowed attachments size of {size}. Please share the files via link instead.',
				num,
				{
					size: formatFileSize(this.uploadSizeLimit),
				}
			))
		},
		onDelete(attachment) {
			if (!attachment.finished) {
				attachment.controller.abort()
			}
			const val = {
				fileName: attachment.fileName,
				displayName: attachment.displayName,
				id: attachment.id,
				size: attachment.total,
				type: attachment.type,
			}
			const _att = this.attachments.filter((a) => {
				return a !== attachment
			})
			this.attachments = _att

			this.$emit(
				'input',
				this.value.filter((a) => {
					if (val.type === 'cloud') {
						return a.fileName !== val.fileName
					} else {
						return a.id !== val.id
					}

				})
			)
		},
		appendToBodyAtCursor(toAppend) {
			this.bus.$emit('appendToBodyAtCursor', toAppend)
		},
		formatBytes(bytes, decimals = 2) {
			if (bytes === 0) return '0 B'
			const k = 1024
			const dm = decimals < 0 ? 0 : decimals
			const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
			const i = Math.floor(Math.log(bytes) / Math.log(k))
			return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
		},
		changeProgress(item, progress) {
			this.attachments.map((attachment, i) => {
				if (item.fileName === attachment.fileName) {
					if (!attachment.finished) {
						const _progress = progress <= attachment.total ? progress : attachment.total
						this.$set(attachment, 'progress', _progress)
						this.$set(attachment, 'sizeString', this.formatBytes(_progress))
						this.$set(attachment, 'percent', (_progress / attachment.total) * 100).toFixed(1)
						if (item.total <= _progress) {
							this.$set(attachment, 'finished', true)
						}
					}
				}
				return attachment
			})
		},
		generatePreview(file) {
			if (this.isImage(file)) {
				return URL.createObjectURL(file)
			} else {
				return false
			}
		},
		isImage(file) {
			return file.type && mimes.indexOf(file.type) !== -1
		},
	},
}
</script>

<style scoped lang="scss">

.new-message-attachments {
	ul.new-message-attachments--list {
		display: flex;
		flex-wrap: wrap;
		// 2 and a half attachment height
		overflow: auto;
		transition: max-height 0.5s cubic-bezier(0, 1, 0, 1);
		padding: 0 10px;

		&.hide {
			overflow: hidden;
			max-height:0;
			transition: max-height 0.5s cubic-bezier(0, 1, 0, 1);
		}

		&.active {
			overflow: auto;
			max-height: 287px;
		}
	}

	.message-attachments-count {
		color: var(--color-text-maxcontrast);
		padding: 10px 20px;
		cursor:pointer;
		display:flex;
		align-items: center;
	}
}
</style>