2013-03-24 12:22:21 +00:00
|
|
|
/*\
|
|
|
|
title: $:/plugins/tiddlywiki/filesystem/filesystemadaptor.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: syncadaptor
|
|
|
|
|
2014-08-14 10:12:25 +00:00
|
|
|
A sync adaptor module for synchronising with the local filesystem via node.js APIs
|
2013-03-24 12:22:21 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// Get a reference to the file system
|
2014-12-18 19:52:15 +00:00
|
|
|
var fs = $tw.node ? require("fs") : null,
|
|
|
|
path = $tw.node ? require("path") : null;
|
2013-10-11 21:10:10 +00:00
|
|
|
|
2014-08-14 10:12:25 +00:00
|
|
|
function FileSystemAdaptor(options) {
|
2013-10-11 22:43:51 +00:00
|
|
|
var self = this;
|
2014-08-14 10:12:25 +00:00
|
|
|
this.wiki = options.wiki;
|
2017-09-04 13:55:12 +00:00
|
|
|
this.logger = new $tw.utils.Logger("filesystem",{colour: "blue"});
|
2013-12-18 21:11:52 +00:00
|
|
|
// Create the <wiki>/tiddlers folder if it doesn't exist
|
2014-09-28 22:46:55 +00:00
|
|
|
$tw.utils.createDirectory($tw.boot.wikiTiddlersPath);
|
2013-03-24 12:22:21 +00:00
|
|
|
}
|
|
|
|
|
2017-02-04 17:25:30 +00:00
|
|
|
FileSystemAdaptor.prototype.name = "filesystem";
|
|
|
|
|
2016-07-05 10:29:59 +00:00
|
|
|
FileSystemAdaptor.prototype.isReady = function() {
|
|
|
|
// The file system adaptor is always ready
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2013-03-24 12:22:21 +00:00
|
|
|
FileSystemAdaptor.prototype.getTiddlerInfo = function(tiddler) {
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
2017-02-11 12:56:42 +00:00
|
|
|
/*
|
|
|
|
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
|
2013-03-25 10:43:46 +00:00
|
|
|
|
2017-02-11 12:56:42 +00:00
|
|
|
The boot process populates $tw.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 $tw.boot.files for new files that are created.
|
|
|
|
*/
|
2013-03-25 10:43:46 +00:00
|
|
|
FileSystemAdaptor.prototype.getTiddlerFileInfo = function(tiddler,callback) {
|
|
|
|
// See if we've already got information about this file
|
2019-04-13 13:59:44 +00:00
|
|
|
var title = tiddler.fields.title,
|
2013-03-25 10:43:46 +00:00
|
|
|
fileInfo = $tw.boot.files[title];
|
2019-04-13 13:59:44 +00:00
|
|
|
if(!fileInfo) {
|
2017-02-11 12:56:42 +00:00
|
|
|
// Otherwise, we'll need to generate it
|
2019-04-13 13:59:44 +00:00
|
|
|
fileInfo = $tw.utils.generateTiddlerFileInfo(tiddler,{
|
|
|
|
directory: $tw.boot.wikiTiddlersPath,
|
2019-08-22 08:24:40 +00:00
|
|
|
pathFilters: this.wiki.getTiddlerText("$:/config/FileSystemPaths","").split("\n"),
|
2019-04-13 13:59:44 +00:00
|
|
|
wiki: this.wiki
|
2013-03-25 10:43:46 +00:00
|
|
|
});
|
2019-04-13 13:59:44 +00:00
|
|
|
$tw.boot.files[title] = fileInfo;
|
2013-03-25 10:43:46 +00:00
|
|
|
}
|
2019-04-13 13:59:44 +00:00
|
|
|
callback(null,fileInfo);
|
2013-03-25 10:43:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-03-24 12:22:21 +00:00
|
|
|
/*
|
|
|
|
Save a tiddler and invoke the callback with (err,adaptorInfo,revision)
|
|
|
|
*/
|
|
|
|
FileSystemAdaptor.prototype.saveTiddler = function(tiddler,callback) {
|
2013-10-11 21:10:10 +00:00
|
|
|
var self = this;
|
2013-03-25 10:43:46 +00:00
|
|
|
this.getTiddlerFileInfo(tiddler,function(err,fileInfo) {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2019-04-13 13:59:44 +00:00
|
|
|
$tw.utils.saveTiddlerToFile(tiddler,fileInfo,callback);
|
2013-03-25 10:43:46 +00:00
|
|
|
});
|
2013-03-24 12:22:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Load a tiddler and invoke the callback with (err,tiddlerFields)
|
2013-12-11 11:45:15 +00:00
|
|
|
|
|
|
|
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.
|
2013-03-24 12:22:21 +00:00
|
|
|
*/
|
|
|
|
FileSystemAdaptor.prototype.loadTiddler = function(title,callback) {
|
2013-12-11 11:45:15 +00:00
|
|
|
callback(null,null);
|
2013-03-24 12:22:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Delete a tiddler and invoke the callback with (err)
|
|
|
|
*/
|
2014-08-14 10:12:25 +00:00
|
|
|
FileSystemAdaptor.prototype.deleteTiddler = function(title,callback,options) {
|
2013-03-25 20:16:12 +00:00
|
|
|
var self = this,
|
|
|
|
fileInfo = $tw.boot.files[title];
|
|
|
|
// Only delete the tiddler if we have writable information for the file
|
|
|
|
if(fileInfo) {
|
2014-02-06 21:36:30 +00:00
|
|
|
// Delete the file
|
|
|
|
fs.unlink(fileInfo.filepath,function(err) {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
2013-10-11 21:10:10 +00:00
|
|
|
}
|
2014-02-06 21:36:30 +00:00
|
|
|
// Delete the metafile if present
|
|
|
|
if(fileInfo.hasMetaFile) {
|
2017-02-11 12:56:42 +00:00
|
|
|
fs.unlink(fileInfo.filepath + ".meta",function(err) {
|
2014-02-06 21:36:30 +00:00
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-02-11 12:56:42 +00:00
|
|
|
return $tw.utils.deleteEmptyDirs(path.dirname(fileInfo.filepath),callback);
|
2014-02-06 21:36:30 +00:00
|
|
|
});
|
|
|
|
} else {
|
2017-02-11 12:56:42 +00:00
|
|
|
return $tw.utils.deleteEmptyDirs(path.dirname(fileInfo.filepath),callback);
|
2014-02-06 21:36:30 +00:00
|
|
|
}
|
|
|
|
});
|
2013-03-25 20:16:12 +00:00
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
2013-03-24 12:22:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if(fs) {
|
|
|
|
exports.adaptorClass = FileSystemAdaptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
})();
|