server-ts: Port share/shaca/sattachment

This commit is contained in:
Elian Doran 2024-04-09 22:19:54 +03:00
parent 3e4b0d5f91
commit c4c2259e69
No known key found for this signature in database
4 changed files with 31 additions and 35 deletions

View File

@ -156,9 +156,9 @@ const STRING_MIME_TYPES = [
"image/svg+xml"
];
function isStringNote(type: string, mime: string) {
function isStringNote(type: string | null, mime: string) {
// render and book are string note in the sense that they are expected to contain empty string
return ["text", "code", "relationMap", "search", "render", "book", "mermaid", "canvas"].includes(type)
return (type && ["text", "code", "relationMap", "search", "render", "book", "mermaid", "canvas"].includes(type))
|| mime.startsWith('text/')
|| STRING_MIME_TYPES.includes(mime);
}

View File

@ -1,39 +1,44 @@
"use strict";
const sql = require('../../sql');
const utils = require('../../../services/utils');
const AbstractShacaEntity = require('./abstract_shaca_entity');
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';
type AttachmentRow = [ string, string, string, string, string, string, string ];
class SAttachment extends AbstractShacaEntity {
constructor([attachmentId, ownerId, role, mime, title, blobId, utcDateModified]) {
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;
constructor([attachmentId, ownerId, role, mime, title, blobId, utcDateModified]: AttachmentRow) {
super();
/** @param {string} */
this.attachmentId = attachmentId;
/** @param {string} */
this.ownerId = ownerId;
/** @param {string} */
this.title = title;
/** @param {string} */
this.role = role;
/** @param {string} */
this.mime = mime;
/** @param {string} */
this.blobId = blobId;
/** @param {string} */
this.utcDateModified = utcDateModified; // used for caching of images
this.utcDateModified = utcDateModified;
this.shaca.attachments[this.attachmentId] = this;
this.shaca.notes[this.ownerId].attachments.push(this);
}
/** @returns {SNote} */
get note() {
get note(): SNote {
return this.shaca.notes[this.ownerId];
}
getContent(silentNotFoundError = false) {
const row = sql.getRow(`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]);
const row = sql.getRow<Pick<Blob, "content">>(`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]);
if (!row) {
if (silentNotFoundError) {
@ -56,7 +61,7 @@ class SAttachment extends AbstractShacaEntity {
}
}
/** @returns {boolean} true if the attachment has string content (not binary) */
/** @returns true if the attachment has string content (not binary) */
hasStringContent() {
return utils.isStringNote(null, this.mime);
}
@ -67,11 +72,10 @@ class SAttachment extends AbstractShacaEntity {
role: this.role,
mime: this.mime,
title: this.title,
position: this.position,
blobId: this.blobId,
utcDateModified: this.utcDateModified
};
}
}
module.exports = SAttachment;
export = SAttachment;

View File

@ -6,6 +6,7 @@ import AbstractShacaEntity = require('./abstract_shaca_entity');
import escape = require('escape-html');
import { AttributeRow } from '../../../becca/entities/rows';
import { Blob } from '../../../services/blob-interface';
import SAttachment = require('./sattachment');
const LABEL = 'label';
const RELATION = 'relation';
@ -30,7 +31,7 @@ class SNote extends AbstractShacaEntity {
private __attributeCache: any[] | null; // fixme
private __inheritableAttributeCache: any[] | null; // fixme
private targetRelations: any[]; // fixme: SAttribute[]
private attachments: any[] ; // fixme: SAttachment[]
private attachments: SAttachment[];
constructor([noteId, title, type, mime, blobId, utcDateModified, isProtected]: NoteRow) {
super();
@ -120,15 +121,9 @@ class SNote extends AbstractShacaEntity {
let content = row.content;
if (this.hasStringContent()) {
if (content == null) {
return "";
}
if (typeof content !== "string") {
return content.toString("utf-8");
}
return content;
return content === null
? ""
: content.toString("utf-8");
}
else {
return content;
@ -470,17 +465,14 @@ class SNote extends AbstractShacaEntity {
return this.targetRelations;
}
/** @returns {SAttachment[]} */
getAttachments() {
return this.attachments;
}
/** @returns {SAttachment} */
getAttachmentByTitle(title: string) {
return this.attachments.find(attachment => attachment.title === title);
}
/** @returns {string} */
get shareId() {
if (this.hasOwnedLabel('shareRoot')) {
return "";
@ -519,4 +511,4 @@ class SNote extends AbstractShacaEntity {
}
}
module.exports = SNote;
export = SNote;

View File

@ -6,7 +6,7 @@ const log = require('../../services/log');
const SNote = require('./entities/snote');
const SBranch = require('./entities/sbranch.js');
const SAttribute = require('./entities/sattribute.js');
const SAttachment = require('./entities/sattachment.js');
const SAttachment = require('./entities/sattachment');
const shareRoot = require('../share_root');
const eventService = require('../../services/events');