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" "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 // 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/') || mime.startsWith('text/')
|| STRING_MIME_TYPES.includes(mime); || STRING_MIME_TYPES.includes(mime);
} }

View File

@ -1,39 +1,44 @@
"use strict"; "use strict";
const sql = require('../../sql'); import sql = require('../../sql');
const utils = require('../../../services/utils'); import utils = require('../../../services/utils');
const AbstractShacaEntity = require('./abstract_shaca_entity'); 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 { 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(); super();
/** @param {string} */
this.attachmentId = attachmentId; this.attachmentId = attachmentId;
/** @param {string} */
this.ownerId = ownerId; this.ownerId = ownerId;
/** @param {string} */
this.title = title; this.title = title;
/** @param {string} */
this.role = role; this.role = role;
/** @param {string} */
this.mime = mime; this.mime = mime;
/** @param {string} */
this.blobId = blobId; this.blobId = blobId;
/** @param {string} */ this.utcDateModified = utcDateModified;
this.utcDateModified = utcDateModified; // used for caching of images
this.shaca.attachments[this.attachmentId] = this; this.shaca.attachments[this.attachmentId] = this;
this.shaca.notes[this.ownerId].attachments.push(this); this.shaca.notes[this.ownerId].attachments.push(this);
} }
/** @returns {SNote} */ get note(): SNote {
get note() {
return this.shaca.notes[this.ownerId]; return this.shaca.notes[this.ownerId];
} }
getContent(silentNotFoundError = false) { 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 (!row) {
if (silentNotFoundError) { 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() { hasStringContent() {
return utils.isStringNote(null, this.mime); return utils.isStringNote(null, this.mime);
} }
@ -67,11 +72,10 @@ class SAttachment extends AbstractShacaEntity {
role: this.role, role: this.role,
mime: this.mime, mime: this.mime,
title: this.title, title: this.title,
position: this.position,
blobId: this.blobId, blobId: this.blobId,
utcDateModified: this.utcDateModified 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 escape = require('escape-html');
import { AttributeRow } from '../../../becca/entities/rows'; import { AttributeRow } from '../../../becca/entities/rows';
import { Blob } from '../../../services/blob-interface'; import { Blob } from '../../../services/blob-interface';
import SAttachment = require('./sattachment');
const LABEL = 'label'; const LABEL = 'label';
const RELATION = 'relation'; const RELATION = 'relation';
@ -30,7 +31,7 @@ class SNote extends AbstractShacaEntity {
private __attributeCache: any[] | null; // fixme private __attributeCache: any[] | null; // fixme
private __inheritableAttributeCache: any[] | null; // fixme private __inheritableAttributeCache: any[] | null; // fixme
private targetRelations: any[]; // fixme: SAttribute[] private targetRelations: any[]; // fixme: SAttribute[]
private attachments: any[] ; // fixme: SAttachment[] private attachments: SAttachment[];
constructor([noteId, title, type, mime, blobId, utcDateModified, isProtected]: NoteRow) { constructor([noteId, title, type, mime, blobId, utcDateModified, isProtected]: NoteRow) {
super(); super();
@ -120,15 +121,9 @@ class SNote extends AbstractShacaEntity {
let content = row.content; let content = row.content;
if (this.hasStringContent()) { if (this.hasStringContent()) {
if (content == null) { return content === null
return ""; ? ""
} : content.toString("utf-8");
if (typeof content !== "string") {
return content.toString("utf-8");
}
return content;
} }
else { else {
return content; return content;
@ -470,17 +465,14 @@ class SNote extends AbstractShacaEntity {
return this.targetRelations; return this.targetRelations;
} }
/** @returns {SAttachment[]} */
getAttachments() { getAttachments() {
return this.attachments; return this.attachments;
} }
/** @returns {SAttachment} */
getAttachmentByTitle(title: string) { getAttachmentByTitle(title: string) {
return this.attachments.find(attachment => attachment.title === title); return this.attachments.find(attachment => attachment.title === title);
} }
/** @returns {string} */
get shareId() { get shareId() {
if (this.hasOwnedLabel('shareRoot')) { if (this.hasOwnedLabel('shareRoot')) {
return ""; 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 SNote = require('./entities/snote');
const SBranch = require('./entities/sbranch.js'); const SBranch = require('./entities/sbranch.js');
const SAttribute = require('./entities/sattribute.js'); const SAttribute = require('./entities/sattribute.js');
const SAttachment = require('./entities/sattachment.js'); const SAttachment = require('./entities/sattachment');
const shareRoot = require('../share_root'); const shareRoot = require('../share_root');
const eventService = require('../../services/events'); const eventService = require('../../services/events');