Skip to content

Build your personal knowledge base with Trilium Notes

server-ts: Port share/shaca/sattachment

Elian Doranc4c2259e69f9
diff --git a/src/services/utils.ts b/src/services/utils.ts
index 6b2974df0c..aef261a6af 100644
--- a/src/services/utils.ts
+++ b/src/services/utils.ts
@@ -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);
}
diff --git a/src/share/shaca/entities/sattachment.js b/src/share/shaca/entities/sattachment.ts
similarity index 61%
rename from src/share/shaca/entities/sattachment.js
rename to src/share/shaca/entities/sattachment.ts
index 998c5ac952..352d347490 100644
--- a/src/share/shaca/entities/sattachment.js
+++ b/src/share/shaca/entities/sattachment.ts
@@ -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;
diff --git a/src/share/shaca/entities/snote.ts b/src/share/shaca/entities/snote.ts
index 5883fdf88a..1574271be4 100644
--- a/src/share/shaca/entities/snote.ts
+++ b/src/share/shaca/entities/snote.ts
@@ -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;
diff --git a/src/share/shaca/shaca_loader.js b/src/share/shaca/shaca_loader.js
index 77348e697c..418311a790 100644
--- a/src/share/shaca/shaca_loader.js
+++ b/src/share/shaca/shaca_loader.js
@@ -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');