1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-19 14:40:27 +00:00
TiddlyWiki5/plugins/tiddlywiki/filesystem/filesystemadaptor.js

240 lines
8.1 KiB
JavaScript
Raw Normal View History

/*\
title: $:/plugins/tiddlywiki/filesystem/filesystemadaptor.js
type: application/javascript
module-type: syncadaptor
A sync adaptor module for synchronising with the local filesystem via node.js APIs
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
// Get a reference to the file system
var fs = $tw.node ? require("fs") : null,
path = $tw.node ? require("path") : null;
function FileSystemAdaptor(options) {
var self = this;
this.wiki = options.wiki;
this.logger = new $tw.utils.Logger("FileSystem");
// Create the <wiki>/tiddlers folder if it doesn't exist
$tw.utils.createDirectory($tw.boot.wikiTiddlersPath);
}
FileSystemAdaptor.prototype.isReady = function() {
// The file system adaptor is always ready
return true;
};
FileSystemAdaptor.prototype.getTiddlerInfo = function(tiddler) {
return {};
};
$tw.config.typeInfo = {
"text/vnd.tiddlywiki": {
fileType: "application/x-tiddler",
2013-03-25 12:12:53 +00:00
extension: ".tid"
}
};
FileSystemAdaptor.prototype.getTiddlerFileInfo = function(tiddler,callback) {
// See if we've already got information about this file
var self = this,
title = tiddler.fields.title,
fileInfo = $tw.boot.files[title];
2013-03-25 12:11:34 +00:00
// Get information about how to save tiddlers of this type
Handle binary files better when saving on Node.JS (#2420) * Save binary tiddlers with meta file The filesystemadaptor plugin was a little simplistic in its understanding of a binary file. It was using the typeInfo dictionary to choose what tiddler types were binary (and hence needed a meta file when saving). I looked as if it was trying to be smart by looking for the hasMetaFile *OR* had the encoding of base64. Unfortunately the typeInfo only defined image/jpeg and so any other base64 encoded tiddler was assumed to be of type text/vnd.tiddlywiki. The net effect was only JPG images got a meta file and everything else were saved as .tid files with base64 encoding. It all still worked but made working with binary data in a Git repo a bit daunting. There is enough information in the $tw.config.contentTypeInfo to determine if a tiddler type is encoded with base64 or not. A better list is available from boot/boot.js who registers all the types thorough the registerFileType and marks then with base64 were appropriate. This commit uses the typeInfo dictionary first for any filesystem specific overrides, then the contentTypeInfo, and finally defaults to the typeInfo["text/vnd.tiddlywiki"]. It also eliminates the now unnecessary override for image/jpeg. I think this might have been the original intent from commit 10b192e7. From my limited testing all files described in boot/boot.js (lines 1832-1856) with an encoding of base64 now save as the original binary and a meta file. Meaning that when you start the node server and then drag-n-drop a binary file (i.e. image/png) it will PUT to the server and then save it on the filesystem as-is allowing the file to be managed as a binary file and not a text file. (Binary diffs are better and GitHub supports them as well). * Prevent duplicate file extensions A side effects of using the $tw.config.contentFileInfo in the previous commit is that it will always append a file extension to the tiddler title when saving. In most cases this is the correct course of action. However, sometimes that title is already a proper filename with an extension (for example importing 'foobar.png' would save a file named 'foobar.png.png') which seemed silly. This commit simply checks to make sure the title does not already end with the file extension before appending it to the filename. A little convenience really. Since IE apparently doesn't have the String endsWith method I took the liberty to add a helper method to $tw.utils trying to follow the other polyfill patterns. I figured this was more generic and readable then attempting to use a one-off solution inline. I got the polyfill code from MDN. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith#Polyfill Is strEndsWith the best method name?
2016-07-11 10:18:19 +00:00
var type = tiddler.fields.type || "text/vnd.tiddlywiki";
var typeInfo = $tw.config.typeInfo[type] ||
$tw.config.contentTypeInfo[type] ||
$tw.config.typeInfo["text/vnd.tiddlywiki"];
2013-03-25 12:11:34 +00:00
var extension = typeInfo.extension || "";
if(!fileInfo) {
// If not, we'll need to generate it
// Start by getting a list of the existing files in the directory
fs.readdir($tw.boot.wikiTiddlersPath,function(err,files) {
if(err) {
return callback(err);
}
// Assemble the new fileInfo
fileInfo = {};
fileInfo.filepath = $tw.boot.wikiTiddlersPath + path.sep + self.generateTiddlerFilename(title,extension,files);
2013-03-25 12:11:34 +00:00
fileInfo.type = typeInfo.fileType || tiddler.fields.type;
fileInfo.hasMetaFile = typeInfo.hasMetaFile;
// Save the newly created fileInfo
$tw.boot.files[title] = fileInfo;
// Pass it to the callback
callback(null,fileInfo);
});
} else {
// Otherwise just invoke the callback
callback(null,fileInfo);
}
};
2014-12-23 05:48:43 +00:00
/*
Transliterate string from cyrillic russian to latin
*/
var transliterate = function(cyrillyc) {
var a = {"Ё":"YO","Й":"I","Ц":"TS","У":"U","К":"K","Е":"E","Н":"N","Г":"G","Ш":"SH","Щ":"SCH","З":"Z","Х":"H","Ъ":"'","ё":"yo","й":"i","ц":"ts","у":"u","к":"k","е":"e","н":"n","г":"g","ш":"sh","щ":"sch","з":"z","х":"h","ъ":"'","Ф":"F","Ы":"I","В":"V","А":"a","П":"P","Р":"R","О":"O","Л":"L","Д":"D","Ж":"ZH","Э":"E","ф":"f","ы":"i","в":"v","а":"a","п":"p","р":"r","о":"o","л":"l","д":"d","ж":"zh","э":"e","Я":"Ya","Ч":"CH","С":"S","М":"M","И":"I","Т":"T","Ь":"'","Б":"B","Ю":"YU","я":"ya","ч":"ch","с":"s","м":"m","и":"i","т":"t","ь":"'","б":"b","ю":"yu"};
return cyrillyc.split("").map(function (char) {
return a[char] || char;
}).join("");
};
Make tiddler file paths configurable (#2379) When saving new tiddlers on node.js, allow the user to override the path of the generated .tid file. This is done by creating a tiddler $:/config/FileSystemPaths which contains one or more filter expressions, one per line. These filters are applied in turn to the tiddler to be saved, and the first output produced is taken as a logical path relative to the wiki's tiddlers directory. Any occurences of "/" in the logical path are replaced with the platform's path separator, the extension ".tid" is appended, illegal characters are replaced by "_" and the path is disambiguated (if necessary) in order to arrive at the final tiddler file path. If none of the filters matches, or the configuration tiddler does not exist, fall back to the previous file naming scheme (i.e. replacing "/" by "_"). This implies we will now, for tiddlers matching the user-specified filters, create directory trees below the tiddlers directory. In order to avoid cluttering it with empty directory trees when renaming or removing tiddlers, any directories that become empty by deleting a tiddler file are removed (recursively). Benefits of this configuration option include the ability to organize git repositories of TiddlyWikis running on node.js, ability to replace characters that cause trouble with particular operating systems or workflows (e.g. '$' on unix) and the ability to replicate tiddler "paths" in the filesystem (by including a filter like "[!has[draft.of]]") without forcing such a (potentially problematic) change on all users.
2016-04-25 07:36:32 +00:00
/*
Given a list of filters, apply every one in turn to source, and return the first result of the first filter with non-empty result.
*/
FileSystemAdaptor.prototype.findFirstFilter = function(filters,source) {
var numFilters = filters.length;
for(var i=0; i<numFilters; i++) {
var result = this.wiki.filterTiddlers(filters[i],null,source);
if(result.length > 0) {
return result[0];
}
}
};
/*
Add file extension to a file path if it doesn't already exist.
*/
2016-07-20 15:16:21 +00:00
FileSystemAdaptor.addFileExtension = function(file,extension) {
return $tw.utils.strEndsWith(file,extension) ? file : file + extension;
};
Make tiddler file paths configurable (#2379) When saving new tiddlers on node.js, allow the user to override the path of the generated .tid file. This is done by creating a tiddler $:/config/FileSystemPaths which contains one or more filter expressions, one per line. These filters are applied in turn to the tiddler to be saved, and the first output produced is taken as a logical path relative to the wiki's tiddlers directory. Any occurences of "/" in the logical path are replaced with the platform's path separator, the extension ".tid" is appended, illegal characters are replaced by "_" and the path is disambiguated (if necessary) in order to arrive at the final tiddler file path. If none of the filters matches, or the configuration tiddler does not exist, fall back to the previous file naming scheme (i.e. replacing "/" by "_"). This implies we will now, for tiddlers matching the user-specified filters, create directory trees below the tiddlers directory. In order to avoid cluttering it with empty directory trees when renaming or removing tiddlers, any directories that become empty by deleting a tiddler file are removed (recursively). Benefits of this configuration option include the ability to organize git repositories of TiddlyWikis running on node.js, ability to replace characters that cause trouble with particular operating systems or workflows (e.g. '$' on unix) and the ability to replicate tiddler "paths" in the filesystem (by including a filter like "[!has[draft.of]]") without forcing such a (potentially problematic) change on all users.
2016-04-25 07:36:32 +00:00
/*
Given a tiddler title and an array of existing filenames, generate a new legal filename for the title, case insensitively avoiding the array of existing filenames
*/
FileSystemAdaptor.prototype.generateTiddlerFilename = function(title,extension,existingFilenames) {
Make tiddler file paths configurable (#2379) When saving new tiddlers on node.js, allow the user to override the path of the generated .tid file. This is done by creating a tiddler $:/config/FileSystemPaths which contains one or more filter expressions, one per line. These filters are applied in turn to the tiddler to be saved, and the first output produced is taken as a logical path relative to the wiki's tiddlers directory. Any occurences of "/" in the logical path are replaced with the platform's path separator, the extension ".tid" is appended, illegal characters are replaced by "_" and the path is disambiguated (if necessary) in order to arrive at the final tiddler file path. If none of the filters matches, or the configuration tiddler does not exist, fall back to the previous file naming scheme (i.e. replacing "/" by "_"). This implies we will now, for tiddlers matching the user-specified filters, create directory trees below the tiddlers directory. In order to avoid cluttering it with empty directory trees when renaming or removing tiddlers, any directories that become empty by deleting a tiddler file are removed (recursively). Benefits of this configuration option include the ability to organize git repositories of TiddlyWikis running on node.js, ability to replace characters that cause trouble with particular operating systems or workflows (e.g. '$' on unix) and the ability to replicate tiddler "paths" in the filesystem (by including a filter like "[!has[draft.of]]") without forcing such a (potentially problematic) change on all users.
2016-04-25 07:36:32 +00:00
var baseFilename;
// Check whether the user has configured a tiddler -> pathname mapping
var pathNameFilters = this.wiki.getTiddlerText("$:/config/FileSystemPaths");
if(pathNameFilters) {
var source = this.wiki.makeTiddlerIterator([title]);
var result = this.findFirstFilter(pathNameFilters.split("\n"),source);
if(result) {
// interpret "/" as path separator
baseFilename = result.replace(/\//g,path.sep);
}
}
if(!baseFilename) {
// no mapping configured, or it did not match this tiddler
// in this case, we fall back to legacy behaviour
baseFilename = title.replace(/\//g,"_");
}
// Remove any of the characters that are illegal in Windows filenames
var baseFilename = transliterate(baseFilename.replace(/<|>|\:|\"|\\|\||\?|\*|\^|\s/g,"_"));
// Truncate the filename if it is too long
if(baseFilename.length > 200) {
baseFilename = baseFilename.substr(0,200);
}
// Start with the base filename plus the extension
2016-07-20 15:16:21 +00:00
var filename = FileSystemAdaptor.addFileExtension(baseFilename,extension),
count = 1;
2015-01-03 15:33:22 +00:00
// Add a discriminator if we're clashing with an existing filename while
// handling case-insensitive filesystems (NTFS, FAT/FAT32, etc.)
while(existingFilenames.some(function(value) {return value.toLocaleLowerCase() === filename.toLocaleLowerCase();})) {
filename = baseFilename + " " + (count++) + extension;
}
return filename;
};
/*
Save a tiddler and invoke the callback with (err,adaptorInfo,revision)
*/
FileSystemAdaptor.prototype.saveTiddler = function(tiddler,callback) {
var self = this;
this.getTiddlerFileInfo(tiddler,function(err,fileInfo) {
var template, content, encoding, filepath,
_finish = function() {
callback(null, {}, 0);
};
if(err) {
return callback(err);
}
Make tiddler file paths configurable (#2379) When saving new tiddlers on node.js, allow the user to override the path of the generated .tid file. This is done by creating a tiddler $:/config/FileSystemPaths which contains one or more filter expressions, one per line. These filters are applied in turn to the tiddler to be saved, and the first output produced is taken as a logical path relative to the wiki's tiddlers directory. Any occurences of "/" in the logical path are replaced with the platform's path separator, the extension ".tid" is appended, illegal characters are replaced by "_" and the path is disambiguated (if necessary) in order to arrive at the final tiddler file path. If none of the filters matches, or the configuration tiddler does not exist, fall back to the previous file naming scheme (i.e. replacing "/" by "_"). This implies we will now, for tiddlers matching the user-specified filters, create directory trees below the tiddlers directory. In order to avoid cluttering it with empty directory trees when renaming or removing tiddlers, any directories that become empty by deleting a tiddler file are removed (recursively). Benefits of this configuration option include the ability to organize git repositories of TiddlyWikis running on node.js, ability to replace characters that cause trouble with particular operating systems or workflows (e.g. '$' on unix) and the ability to replicate tiddler "paths" in the filesystem (by including a filter like "[!has[draft.of]]") without forcing such a (potentially problematic) change on all users.
2016-04-25 07:36:32 +00:00
var error = $tw.utils.createDirectory(path.dirname(fileInfo.filepath));
if(error) {
return callback(error);
}
var typeInfo = $tw.config.contentTypeInfo[fileInfo.type];
if(fileInfo.hasMetaFile || typeInfo.encoding === "base64") {
2013-03-25 12:11:34 +00:00
// Save the tiddler as a separate body and meta file
filepath = fileInfo.filepath;
fs.writeFile(filepath,tiddler.fields.text,{encoding: typeInfo.encoding},function(err) {
2013-03-25 12:11:34 +00:00
if(err) {
return callback(err);
}
content = self.wiki.renderTiddler("text/plain","$:/core/templates/tiddler-metadata",{variables: {currentTiddler: tiddler.fields.title}});
2016-07-20 15:16:21 +00:00
filepath = FileSystemAdaptor.addFileExtension(fileInfo.filepath,".meta");
fs.writeFile(filepath,content,{encoding: "utf8"},function (err) {
2013-03-25 12:11:34 +00:00
if(err) {
return callback(err);
}
2016-07-20 15:16:21 +00:00
self.logger.log("Saved file",filepath);
_finish();
2013-03-25 12:11:34 +00:00
});
});
} else {
// Save the tiddler as a self contained templated file
content = self.wiki.renderTiddler("text/plain","$:/core/templates/tid-tiddler",{variables: {currentTiddler: tiddler.fields.title}});
2016-07-20 15:16:21 +00:00
filepath = FileSystemAdaptor.addFileExtension(fileInfo.filepath,".tid");
fs.writeFile(filepath,content,{encoding: "utf8"},function (err) {
2013-03-25 12:11:34 +00:00
if(err) {
return callback(err);
}
2016-07-20 15:16:21 +00:00
self.logger.log("Saved file",filepath);
_finish();
2013-03-25 12:11:34 +00:00
});
}
});
};
/*
Load a tiddler and invoke the callback with (err,tiddlerFields)
We don't need to implement loading for the file system adaptor, because all the tiddler files will have been loaded during the boot process.
*/
FileSystemAdaptor.prototype.loadTiddler = function(title,callback) {
callback(null,null);
};
/*
Delete a tiddler and invoke the callback with (err)
*/
FileSystemAdaptor.prototype.deleteTiddler = function(title,callback,options) {
var self = this,
fileInfo = $tw.boot.files[title];
// Only delete the tiddler if we have writable information for the file
if(fileInfo) {
// Delete the file
fs.unlink(fileInfo.filepath,function(err) {
if(err) {
return callback(err);
}
self.logger.log("Deleted file",fileInfo.filepath);
// Delete the metafile if present
if(fileInfo.hasMetaFile) {
2016-07-20 15:16:21 +00:00
fs.unlink(FileSystemAdaptor.addFileExtension(fileInfo.filepath,".meta"),function(err) {
if(err) {
return callback(err);
}
Make tiddler file paths configurable (#2379) When saving new tiddlers on node.js, allow the user to override the path of the generated .tid file. This is done by creating a tiddler $:/config/FileSystemPaths which contains one or more filter expressions, one per line. These filters are applied in turn to the tiddler to be saved, and the first output produced is taken as a logical path relative to the wiki's tiddlers directory. Any occurences of "/" in the logical path are replaced with the platform's path separator, the extension ".tid" is appended, illegal characters are replaced by "_" and the path is disambiguated (if necessary) in order to arrive at the final tiddler file path. If none of the filters matches, or the configuration tiddler does not exist, fall back to the previous file naming scheme (i.e. replacing "/" by "_"). This implies we will now, for tiddlers matching the user-specified filters, create directory trees below the tiddlers directory. In order to avoid cluttering it with empty directory trees when renaming or removing tiddlers, any directories that become empty by deleting a tiddler file are removed (recursively). Benefits of this configuration option include the ability to organize git repositories of TiddlyWikis running on node.js, ability to replace characters that cause trouble with particular operating systems or workflows (e.g. '$' on unix) and the ability to replicate tiddler "paths" in the filesystem (by including a filter like "[!has[draft.of]]") without forcing such a (potentially problematic) change on all users.
2016-04-25 07:36:32 +00:00
$tw.utils.deleteEmptyDirs(path.dirname(fileInfo.filepath),callback);
});
} else {
Make tiddler file paths configurable (#2379) When saving new tiddlers on node.js, allow the user to override the path of the generated .tid file. This is done by creating a tiddler $:/config/FileSystemPaths which contains one or more filter expressions, one per line. These filters are applied in turn to the tiddler to be saved, and the first output produced is taken as a logical path relative to the wiki's tiddlers directory. Any occurences of "/" in the logical path are replaced with the platform's path separator, the extension ".tid" is appended, illegal characters are replaced by "_" and the path is disambiguated (if necessary) in order to arrive at the final tiddler file path. If none of the filters matches, or the configuration tiddler does not exist, fall back to the previous file naming scheme (i.e. replacing "/" by "_"). This implies we will now, for tiddlers matching the user-specified filters, create directory trees below the tiddlers directory. In order to avoid cluttering it with empty directory trees when renaming or removing tiddlers, any directories that become empty by deleting a tiddler file are removed (recursively). Benefits of this configuration option include the ability to organize git repositories of TiddlyWikis running on node.js, ability to replace characters that cause trouble with particular operating systems or workflows (e.g. '$' on unix) and the ability to replicate tiddler "paths" in the filesystem (by including a filter like "[!has[draft.of]]") without forcing such a (potentially problematic) change on all users.
2016-04-25 07:36:32 +00:00
$tw.utils.deleteEmptyDirs(path.dirname(fileInfo.filepath),callback);
}
});
} else {
callback(null);
}
};
if(fs) {
exports.adaptorClass = FileSystemAdaptor;
}
})();