2016-07-07 05:03:30 +08:00
|
|
|
import ko from 'ko';
|
|
|
|
|
2021-02-17 21:40:21 +08:00
|
|
|
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
|
|
|
|
2021-03-11 05:41:35 +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.isInline = false;
|
|
|
|
this.isLinked = false;
|
|
|
|
this.isThumbnail = false;
|
|
|
|
this.cid = '';
|
2020-09-20 17:15:00 +08:00
|
|
|
this.cidWithoutTags = '';
|
2016-07-07 05:03:30 +08:00
|
|
|
this.contentLocation = '';
|
|
|
|
this.download = '';
|
|
|
|
this.folder = '';
|
|
|
|
this.uid = '';
|
|
|
|
this.mimeIndex = '';
|
|
|
|
this.framed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @static
|
2020-10-19 01:46:47 +08:00
|
|
|
* @param {FetchJsonAttachment} json
|
2016-07-07 05:03:30 +08:00
|
|
|
* @returns {?AttachmentModel}
|
|
|
|
*/
|
2020-10-19 01:46:47 +08:00
|
|
|
static reviveFromJson(json) {
|
|
|
|
const attachment = super.reviveFromJson(json);
|
|
|
|
if (attachment) {
|
2021-02-17 21:40:21 +08:00
|
|
|
attachment.friendlySize = FileInfo.friendlySize(json.EstimatedSize);
|
2020-10-19 01:46:47 +08:00
|
|
|
attachment.cidWithoutTags = attachment.cid.replace(/^<+/, '').replace(/>+$/, '');
|
|
|
|
|
2021-02-17 21:40:21 +08:00
|
|
|
attachment.fileNameExt = FileInfo.getExtension(attachment.fileName);
|
|
|
|
attachment.fileType = FileInfo.getType(attachment.fileNameExt, attachment.mimeType);
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
2020-10-19 01:46:47 +08:00
|
|
|
return attachment;
|
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 (
|
2021-03-11 05:41:35 +08:00
|
|
|
(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 attachmentDownload(this.download);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
linkPreview() {
|
2021-02-04 18:25:00 +08:00
|
|
|
return serverRequestRaw('View', this.download);
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
linkThumbnail() {
|
2021-02-04 18:25:00 +08:00
|
|
|
return this.hasThumbnail() ? serverRequestRaw('ViewThumbnail', this.download) : '';
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
linkThumbnailPreviewStyle() {
|
|
|
|
const link = this.linkThumbnail();
|
2020-07-28 23:20:14 +08:00
|
|
|
return link ? 'background:url(' + link + ')' : '';
|
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.substr(0, 4)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|