MWS: fix editing attachment tiddlers (#8455)

* fix breaking bug in image tiddler attachment

* fix comments

* fix code format

* refactor processIncomingTiddler flow

* remove whitespaces after if statements

* refactor attachment_blob persistence flow

* refactor process tiddler to support different attachments

* add tests for attachment

* add more attachement test cases

* working on adding instanbul for test coverage report

* code coverage report generation

* remove unnecessary packages

* fix comments
This commit is contained in:
webplusai
2024-08-28 18:13:52 +02:00
committed by GitHub
parent eac8a2c3d8
commit 3287dce40c
8 changed files with 309 additions and 28 deletions

View File

@@ -53,6 +53,7 @@ Saves an attachment to a file. Options include:
text: text content (may be binary)
type: MIME type of content
reference: reference to use for debugging
_canonical_uri: canonical uri of the content
*/
AttachmentStore.prototype.saveAttachment = function(options) {
const path = require("path"),
@@ -69,6 +70,7 @@ AttachmentStore.prototype.saveAttachment = function(options) {
fs.writeFileSync(path.resolve(attachmentPath,dataFilename),options.text,contentTypeInfo.encoding);
// Save the meta.json file
fs.writeFileSync(path.resolve(attachmentPath,"meta.json"),JSON.stringify({
_canonical_uri: options._canonical_uri,
created: $tw.utils.stringifyDate(new Date()),
modified: $tw.utils.stringifyDate(new Date()),
contentHash: contentHash,
@@ -81,7 +83,7 @@ AttachmentStore.prototype.saveAttachment = function(options) {
/*
Adopts an attachment file into the store
*/
AttachmentStore.prototype.adoptAttachment = function(incomingFilepath,type,hash) {
AttachmentStore.prototype.adoptAttachment = function(incomingFilepath,type,hash,_canonical_uri) {
const path = require("path"),
fs = require("fs");
// Choose the best file extension for the attachment given its type
@@ -95,6 +97,7 @@ AttachmentStore.prototype.adoptAttachment = function(incomingFilepath,type,hash)
fs.renameSync(incomingFilepath,dataFilepath);
// Save the meta.json file
fs.writeFileSync(path.resolve(attachmentPath,"meta.json"),JSON.stringify({
_canonical_uri: _canonical_uri,
created: $tw.utils.stringifyDate(new Date()),
modified: $tw.utils.stringifyDate(new Date()),
contentHash: hash,
@@ -137,6 +140,42 @@ AttachmentStore.prototype.getAttachmentStream = function(attachment_name) {
return null;
};
/*
Get the size of an attachment file given the contentHash.
Returns the size in bytes, or null if the file doesn't exist.
*/
AttachmentStore.prototype.getAttachmentFileSize = function(contentHash) {
const path = require("path"),
fs = require("fs");
// Construct the path to the attachment directory
const attachmentPath = path.resolve(this.storePath, "files", contentHash);
// Read the meta.json file
const metaJsonPath = path.resolve(attachmentPath, "meta.json");
if(fs.existsSync(metaJsonPath) && fs.statSync(metaJsonPath).isFile()) {
const meta = $tw.utils.parseJSONSafe(fs.readFileSync(metaJsonPath, "utf8"), function() { return null; });
if(meta) {
const dataFilepath = path.resolve(attachmentPath, meta.filename);
// Check if the data file exists and return its size
if(fs.existsSync(dataFilepath) && fs.statSync(dataFilepath).isFile()) {
return fs.statSync(dataFilepath).size;
}
}
}
// Return null if the file doesn't exist or there was an error
return null;
};
AttachmentStore.prototype.getAttachmentMetadata = function(attachmentBlob) {
const path = require("path"),
fs = require("fs");
const attachmentPath = path.resolve(this.storePath, "files", attachmentBlob);
const metaJsonPath = path.resolve(attachmentPath, "meta.json");
if(fs.existsSync(metaJsonPath)) {
const metadata = JSON.parse(fs.readFileSync(metaJsonPath, "utf8"));
return metadata;
}
return null;
};
exports.AttachmentStore = AttachmentStore;
})();