snappymail/dev/Model/Attachment.js

192 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-07-07 05:03:30 +08:00
import ko from 'ko';
import { FileInfo, FileType } from 'Common/File';
2019-07-05 03:19:24 +08:00
import {
attachmentDownload,
2021-02-04 18:25:00 +08:00
serverRequestRaw
2019-07-05 03:19:24 +08:00
} from 'Common/Links';
2016-07-07 05:03:30 +08:00
2019-07-05 03:19:24 +08:00
import { AbstractModel } from 'Knoin/AbstractModel';
2016-07-07 05:03:30 +08:00
import { SMAudio } from 'Common/Audio';
2016-07-07 05:03:30 +08:00
2021-01-22 23:32:08 +08:00
export class AttachmentModel extends AbstractModel {
2016-07-16 05:29:42 +08:00
constructor() {
2020-10-19 01:19:45 +08:00
super();
2016-07-07 05:03:30 +08:00
this.checked = ko.observable(false);
this.mimeType = '';
this.fileName = '';
this.fileNameExt = '';
this.fileType = FileType.Unknown;
this.friendlySize = '';
this.isLinked = false;
this.isThumbnail = false;
this.cid = '';
this.contentLocation = '';
this.download = '';
this.folder = '';
this.uid = '';
this.url = '';
2016-07-07 05:03:30 +08:00
this.mimeIndex = '';
this.framed = false;
this.addObservables({
isInline: false
});
2016-07-07 05:03:30 +08:00
}
/**
* @static
* @param {FetchJsonAttachment} json
2016-07-07 05:03:30 +08:00
* @returns {?AttachmentModel}
*/
static reviveFromJson(json) {
const attachment = super.reviveFromJson(json);
if (attachment) {
attachment.friendlySize = FileInfo.friendlySize(json.EstimatedSize);
attachment.fileNameExt = FileInfo.getExtension(attachment.fileName);
attachment.fileType = FileInfo.getType(attachment.fileNameExt, attachment.mimeType);
2016-07-07 05:03:30 +08:00
}
return attachment;
2016-07-07 05:03:30 +08:00
}
contentId() {
return this.cid.replace(/^<+|>+$/g, '');
}
2016-07-07 05:03:30 +08:00
/**
* @returns {boolean}
*/
isImage() {
return FileType.Image === this.fileType;
}
/**
* @returns {boolean}
*/
isMp3() {
return FileType.Audio === this.fileType && 'mp3' === this.fileNameExt;
}
/**
* @returns {boolean}
*/
isOgg() {
return FileType.Audio === this.fileType && ('oga' === this.fileNameExt || 'ogg' === this.fileNameExt);
}
/**
* @returns {boolean}
*/
isWav() {
return FileType.Audio === this.fileType && 'wav' === this.fileNameExt;
}
/**
* @returns {boolean}
*/
hasThumbnail() {
return this.isThumbnail;
}
/**
* @returns {boolean}
*/
isText() {
2021-04-13 20:11:17 +08:00
return FileType.Text === this.fileType || FileType.Eml === this.fileType;
2016-07-07 05:03:30 +08:00
}
/**
* @returns {boolean}
*/
2021-04-13 20:11:17 +08:00
pdfPreview() {
return null != navigator.mimeTypes['application/pdf'] && FileType.Pdf === this.fileType;
2016-07-07 05:03:30 +08:00
}
/**
* @returns {boolean}
*/
hasPreview() {
2021-04-13 20:11:17 +08:00
return this.isImage() || this.pdfPreview() || this.isText();
2016-07-07 05:03:30 +08:00
}
/**
* @returns {boolean}
*/
hasPreplay() {
2019-07-05 03:19:24 +08:00
return (
(SMAudio.supportedMp3 && this.isMp3()) ||
(SMAudio.supportedOgg && this.isOgg()) ||
(SMAudio.supportedWav && this.isWav())
2019-07-05 03:19:24 +08:00
);
2016-07-07 05:03:30 +08:00
}
/**
* @returns {string}
*/
linkDownload() {
return this.url || attachmentDownload(this.download);
2016-07-07 05:03:30 +08:00
}
/**
* @returns {string}
*/
linkPreview() {
return this.url || serverRequestRaw('View', this.download);
2016-07-07 05:03:30 +08:00
}
/**
* @returns {string}
*/
linkThumbnailPreviewStyle() {
2022-01-10 21:30:55 +08:00
return this.hasThumbnail() ? 'background:url(' + serverRequestRaw('ViewThumbnail', this.download) + ')' : '';
2016-07-07 05:03:30 +08:00
}
/**
* @returns {string}
*/
linkPreviewMain() {
let result = '';
2019-07-05 03:19:24 +08:00
switch (true) {
2016-07-07 05:03:30 +08:00
case this.isImage():
2021-04-13 20:11:17 +08:00
case this.pdfPreview():
2016-07-07 05:03:30 +08:00
result = this.linkPreview();
break;
case this.isText():
2021-04-13 20:11:17 +08:00
result = serverRequestRaw('ViewAsPlain', this.download);
2016-07-07 05:03:30 +08:00
break;
// no default
}
return result;
}
/**
* @param {AttachmentModel} attachment
* @param {*} event
* @returns {boolean}
*/
eventDragStart(attachment, event) {
const localEvent = event.originalEvent || event;
2019-07-05 03:19:24 +08:00
if (attachment && localEvent && localEvent.dataTransfer && localEvent.dataTransfer.setData) {
2021-04-13 20:11:17 +08:00
let link = this.linkDownload();
if ('http' !== link.slice(0, 4)) {
2021-04-13 20:11:17 +08:00
link = location.protocol + '//' + location.host + location.pathname + link;
}
localEvent.dataTransfer.setData('DownloadURL', this.mimeType + ':' + this.fileName + ':' + link);
2016-07-07 05:03:30 +08:00
}
return true;
}
/**
* @returns {string}
*/
iconClass() {
2021-04-13 20:11:17 +08:00
return FileInfo.getTypeIconClass(this.fileType);
2016-07-07 05:03:30 +08:00
}
}