2023-06-05 23:05:05 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-04-09 22:19:54 +03:00
|
|
|
import sql = require('../../sql');
|
|
|
|
import utils = require('../../../services/utils');
|
|
|
|
import AbstractShacaEntity = require('./abstract_shaca_entity');
|
|
|
|
import SNote = require('./snote');
|
|
|
|
import { Blob } from '../../../services/blob-interface';
|
|
|
|
|
2023-06-05 23:05:05 +02:00
|
|
|
class SAttachment extends AbstractShacaEntity {
|
2024-04-09 22:19:54 +03:00
|
|
|
private attachmentId: string;
|
|
|
|
private ownerId: string;
|
|
|
|
title: string;
|
|
|
|
private role: string;
|
|
|
|
private mime: string;
|
|
|
|
private blobId: string;
|
|
|
|
/** used for caching of images */
|
|
|
|
private utcDateModified: string;
|
|
|
|
|
2024-04-09 22:49:05 +03:00
|
|
|
constructor([attachmentId, ownerId, role, mime, title, blobId, utcDateModified]: SAttachmentRow) {
|
2023-06-05 23:05:05 +02:00
|
|
|
super();
|
|
|
|
|
|
|
|
this.attachmentId = attachmentId;
|
2023-07-14 17:01:56 +02:00
|
|
|
this.ownerId = ownerId;
|
2023-06-05 23:05:05 +02:00
|
|
|
this.title = title;
|
|
|
|
this.role = role;
|
|
|
|
this.mime = mime;
|
|
|
|
this.blobId = blobId;
|
2024-04-09 22:19:54 +03:00
|
|
|
this.utcDateModified = utcDateModified;
|
2023-06-05 23:05:05 +02:00
|
|
|
|
|
|
|
this.shaca.attachments[this.attachmentId] = this;
|
2023-07-14 17:01:56 +02:00
|
|
|
this.shaca.notes[this.ownerId].attachments.push(this);
|
2023-06-05 23:05:05 +02:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:19:54 +03:00
|
|
|
get note(): SNote {
|
2023-07-14 17:01:56 +02:00
|
|
|
return this.shaca.notes[this.ownerId];
|
2023-06-05 23:05:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getContent(silentNotFoundError = false) {
|
2024-04-09 22:19:54 +03:00
|
|
|
const row = sql.getRow<Pick<Blob, "content">>(`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]);
|
2023-06-05 23:05:05 +02:00
|
|
|
|
|
|
|
if (!row) {
|
|
|
|
if (silentNotFoundError) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error(`Cannot find blob for attachment '${this.attachmentId}', blob '${this.blobId}'`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let content = row.content;
|
|
|
|
|
|
|
|
if (this.hasStringContent()) {
|
|
|
|
return content === null
|
|
|
|
? ""
|
|
|
|
: content.toString("utf-8");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 22:19:54 +03:00
|
|
|
/** @returns true if the attachment has string content (not binary) */
|
2023-06-05 23:05:05 +02:00
|
|
|
hasStringContent() {
|
|
|
|
return utils.isStringNote(null, this.mime);
|
|
|
|
}
|
|
|
|
|
|
|
|
getPojo() {
|
|
|
|
return {
|
|
|
|
attachmentId: this.attachmentId,
|
|
|
|
role: this.role,
|
|
|
|
mime: this.mime,
|
|
|
|
title: this.title,
|
|
|
|
blobId: this.blobId,
|
|
|
|
utcDateModified: this.utcDateModified
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 22:19:54 +03:00
|
|
|
export = SAttachment;
|