From b0f6d50b6096955b93725c88f6204a971de6110e Mon Sep 17 00:00:00 2001 From: Joshua Fontany Date: Sun, 6 Dec 2020 01:41:03 -0800 Subject: [PATCH] fix filesystem bugs (#5213) --- core/modules/utils/filesystem.js | 40 ++++++++++--------- .../filesystem/filesystemadaptor.js | 12 ++++-- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/core/modules/utils/filesystem.js b/core/modules/utils/filesystem.js index 9ee2e8acd..370a1eaf5 100644 --- a/core/modules/utils/filesystem.js +++ b/core/modules/utils/filesystem.js @@ -217,6 +217,10 @@ Options include: */ exports.generateTiddlerFileInfo = function(tiddler,options) { var fileInfo = {}, metaExt; + // Propigate the isEditableFile flag + if(options.fileInfo) { + fileInfo.isEditableFile = options.fileInfo.isEditableFile || false; + } // Check if the tiddler has any unsafe fields that can't be expressed in a .tid or .meta file: containing control characters, or leading/trailing whitespace var hasUnsafeFields = false; $tw.utils.each(tiddler.getFieldStrings(),function(value,fieldName) { @@ -248,20 +252,22 @@ exports.generateTiddlerFileInfo = function(tiddler,options) { extFilters: options.extFilters, wiki: options.wiki }); - if(metaExt === ".tid") { - // Overriding to the .tid extension needs special handling - fileInfo.type = "application/x-tiddler"; - fileInfo.hasMetaFile = false; - } else if (metaExt === ".json") { - // Overriding to the .json extension needs special handling - fileInfo.type = "application/json"; - fileInfo.hasMetaFile = false; - } else if (metaExt) { - //If the new type matches a known extention, use that MIME type's encoding - var extInfo = $tw.utils.getFileExtensionInfo(metaExt); - fileInfo.type = extInfo ? extInfo.type : null; - fileInfo.encoding = $tw.utils.getTypeEncoding(metaExt); - fileInfo.hasMetaFile = true; + if(metaExt){ + if(metaExt === ".tid") { + // Overriding to the .tid extension needs special handling + fileInfo.type = "application/x-tiddler"; + fileInfo.hasMetaFile = false; + } else if (metaExt === ".json") { + // Overriding to the .json extension needs special handling + fileInfo.type = "application/json"; + fileInfo.hasMetaFile = false; + } else { + //If the new type matches a known extention, use that MIME type's encoding + var extInfo = $tw.utils.getFileExtensionInfo(metaExt); + fileInfo.type = extInfo ? extInfo.type : null; + fileInfo.encoding = $tw.utils.getTypeEncoding(metaExt); + fileInfo.hasMetaFile = true; + } } } } @@ -276,10 +282,6 @@ exports.generateTiddlerFileInfo = function(tiddler,options) { fileInfo: options.fileInfo, originalpath: options.originalpath }); - // Propigate the isEditableFile flag - if(options.fileInfo) { - fileInfo.isEditableFile = options.fileInfo.isEditableFile || false; - } return fileInfo; }; @@ -380,7 +382,7 @@ exports.generateTiddlerFilepath = function(title,options) { count++; } while(fs.existsSync(fullPath)); //If the path does not start with the wikiPath directory or the wikiTiddlersPath directory, or if the last write failed - var encode = !(fullPath.indexOf($tw.boot.wikiPath) == 0 || fullPath.indexOf($tw.boot.wikiTiddlersPath) == 0) || ((options.fileInfo || {writeError: false}).writeError == true); + var encode = !(fullPath.indexOf(path.resolve($tw.boot.wikiPath)) == 0 || fullPath.indexOf($tw.boot.wikiTiddlersPath) == 0) || ((options.fileInfo || {writeError: false}).writeError == true); if(encode){ //encodeURIComponent() and then resolve to tiddler directory fullPath = path.resolve(directory, encodeURIComponent(fullPath)); diff --git a/plugins/tiddlywiki/filesystem/filesystemadaptor.js b/plugins/tiddlywiki/filesystem/filesystemadaptor.js index 5cef917bf..7e3cd2fa2 100644 --- a/plugins/tiddlywiki/filesystem/filesystemadaptor.js +++ b/plugins/tiddlywiki/filesystem/filesystemadaptor.js @@ -53,11 +53,17 @@ It is the responsibility of the filesystem adaptor to update this.boot.files for */ FileSystemAdaptor.prototype.getTiddlerFileInfo = function(tiddler,callback) { // Always generate a fileInfo object when this fuction is called - var title = tiddler.fields.title, newInfo; + var title = tiddler.fields.title, newInfo, pathFilters, extFilters; + if(this.wiki.tiddlerExists("$:/config/FileSystemPaths")){ + pathFilters = this.wiki.getTiddlerText("$:/config/FileSystemPaths","").split("\n"); + } + if(this.wiki.tiddlerExists("$:/config/FileSystemExtensions")){ + extFilters = this.wiki.getTiddlerText("$:/config/FileSystemExtensions","").split("\n"); + } newInfo = $tw.utils.generateTiddlerFileInfo(tiddler,{ directory: this.boot.wikiTiddlersPath, - pathFilters: this.wiki.getTiddlerText("$:/config/FileSystemPaths","").split("\n"), - extFilters: this.wiki.getTiddlerText("$:/config/FileSystemExtensions","").split("\n"), + pathFilters: pathFilters, + extFilters: extFilters, wiki: this.wiki, fileInfo: this.boot.files[title], originalpath: this.wiki.extractTiddlerDataItem("$:/config/OriginalTiddlerPaths",title, "")