mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-30 05:19:57 +00:00
dde4182830
* ignore .env testing new implementation almost there closer bug, desyncing fixed final testing final testing cleanup cleanup * isEditableFile flow fixed * removed `basepath` logic * callback to delete title from $tw.boot.files * comment fix * have syncer delete from boot.files * syntax * bugfix: error on missing directory * bugifx * remove !draft check * fix relative filepaths * cleanup * cleanup !draft * catch undefined filepaths in deleteTiddlerFile() * typo * whitelist wiki dir, encodeURIComponent otherwise * test for wikiPath, not wikiPath/tiddlers * don't need to .normailze() * whitelist wiki directory, move cleanup to util * use cleanup util & fail EPERM & EACCESS gracefully * comments * final bugs fixed * improved sync error
147 lines
4.7 KiB
JavaScript
147 lines
4.7 KiB
JavaScript
/*\
|
|
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.boot = options.boot || $tw.boot;
|
|
this.logger = new $tw.utils.Logger("filesystem",{colour: "blue"});
|
|
// Create the <wiki>/tiddlers folder if it doesn't exist
|
|
$tw.utils.createDirectory(this.boot.wikiTiddlersPath);
|
|
}
|
|
|
|
FileSystemAdaptor.prototype.name = "filesystem";
|
|
|
|
FileSystemAdaptor.prototype.supportsLazyLoading = false;
|
|
|
|
FileSystemAdaptor.prototype.isReady = function() {
|
|
// The file system adaptor is always ready
|
|
return true;
|
|
};
|
|
|
|
FileSystemAdaptor.prototype.getTiddlerInfo = function(tiddler) {
|
|
//Returns the existing fileInfo for the tiddler. To regenerate, call getTiddlerFileInfo().
|
|
var title = tiddler.fields.title;
|
|
return this.boot.files[title];
|
|
};
|
|
|
|
/*
|
|
Return a fileInfo object for a tiddler, creating it if necessary:
|
|
filepath: the absolute path to the file containing the tiddler
|
|
type: the type of the tiddler file (NOT the type of the tiddler -- see below)
|
|
hasMetaFile: true if the file also has a companion .meta file
|
|
|
|
The boot process populates this.boot.files for each of the tiddler files that it loads.
|
|
The type is found by looking up the extension in $tw.config.fileExtensionInfo (eg "application/x-tiddler" for ".tid" files).
|
|
|
|
It is the responsibility of the filesystem adaptor to update this.boot.files for new files that are created.
|
|
*/
|
|
FileSystemAdaptor.prototype.getTiddlerFileInfo = function(tiddler,callback) {
|
|
// See if we've already got information about this file
|
|
var title = tiddler.fields.title,
|
|
newInfo, fileInfo = this.boot.files[title];
|
|
// Always generate a fileInfo object when this fuction is called
|
|
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"),
|
|
wiki: this.wiki,
|
|
fileInfo: fileInfo
|
|
});
|
|
this.boot.files[title] = newInfo;
|
|
callback(null,newInfo);
|
|
};
|
|
|
|
|
|
/*
|
|
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) {
|
|
if(err) {
|
|
return callback(err);
|
|
}
|
|
$tw.utils.saveTiddlerToFile(tiddler,fileInfo,function(err) {
|
|
if(err) {
|
|
if ((err.code == "EPERM" || err.code == "EACCES") && err.syscall == "open") {
|
|
var bootInfo = self.boot.files[tiddler.fields.title];
|
|
bootInfo.writeError = true;
|
|
self.boot.files[tiddler.fields.title] = bootInfo;
|
|
$tw.syncer.displayError("Sync for tiddler [["+tiddler.fields.title+"]] will be retried with encoded filepath", encodeURIComponent(bootInfo.filepath));
|
|
return callback(err);
|
|
} else {
|
|
return callback(err);
|
|
}
|
|
}
|
|
// Cleanup duplicates if the file moved or changed extensions
|
|
var options = {
|
|
adaptorInfo: ($tw.syncer.tiddlerInfo[tiddler.fields.title] || {adaptorInfo: {} }).adaptorInfo,
|
|
bootInfo: self.boot.files[tiddler.fields.title] || {},
|
|
title: tiddler.fields.title
|
|
};
|
|
$tw.utils.cleanupTiddlerFiles(options, function(err){
|
|
if(err) {
|
|
return callback(err);
|
|
}
|
|
return callback(null, self.boot.files[tiddler.fields.title]);
|
|
});
|
|
});
|
|
});
|
|
};
|
|
|
|
/*
|
|
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 = this.boot.files[title];
|
|
// Only delete the tiddler if we have writable information for the file
|
|
if(fileInfo) {
|
|
$tw.utils.deleteTiddlerFile(fileInfo, function(err){
|
|
if(err) {
|
|
if ((err.code == "EPERM" || err.code == "EACCES") && err.syscall == "unlink") {
|
|
// Error deleting the file on disk, should fail gracefully
|
|
$tw.syncer.displayError("Server desynchronized. Error deleting file for deleted tiddler: "+title, err);
|
|
return callback(null);
|
|
} else {
|
|
return callback(err);
|
|
}
|
|
}
|
|
return callback(null);
|
|
});
|
|
} else {
|
|
callback(null);
|
|
}
|
|
};
|
|
|
|
if(fs) {
|
|
exports.adaptorClass = FileSystemAdaptor;
|
|
}
|
|
|
|
})();
|