1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-24 22:33:16 +00:00

Add support for saving binary files

This commit is contained in:
Jeremy Ruston 2013-03-25 12:11:34 +00:00
parent 19b62c30d8
commit 10b192e743
2 changed files with 46 additions and 18 deletions

View File

@ -0,0 +1,8 @@
title: $:/core/templates/tiddler-metadata
<!--
This template is used for saving tiddler metadata *.meta files
--><$fields exclude='text' template='$name$: $value$
'></$fields>

View File

@ -30,7 +30,6 @@ $tw.config.typeInfo = {
template: "$:/core/templates/tid-tiddler" template: "$:/core/templates/tid-tiddler"
}, },
"image/jpeg" : { "image/jpeg" : {
fileType: "application/x-tiddler-binary",
hasMetaFile: true hasMetaFile: true
} }
}; };
@ -44,6 +43,13 @@ FileSystemAdaptor.prototype.getTiddlerFileInfo = function(tiddler,callback) {
var self = this, var self = this,
title = tiddler.fields.title, title = tiddler.fields.title,
fileInfo = $tw.boot.files[title]; fileInfo = $tw.boot.files[title];
// Get information about how to save tiddlers of this type
var type = tiddler.fields.type || "text/vnd.tiddlywiki",
typeInfo = $tw.config.typeInfo[type];
if(!typeInfo) {
typeInfo = $tw.config.typeInfo["text/vnd.tiddlywiki"];
}
var extension = typeInfo.extension || "";
if(!fileInfo) { if(!fileInfo) {
// If not, we'll need to generate it // If not, we'll need to generate it
// Start by getting a list of the existing files in the directory // Start by getting a list of the existing files in the directory
@ -51,17 +57,10 @@ FileSystemAdaptor.prototype.getTiddlerFileInfo = function(tiddler,callback) {
if(err) { if(err) {
return callback(err); return callback(err);
} }
// Get information about how to save tiddlers of this type
var type = tiddler.fields.type || "text/vnd.tiddlywiki",
typeInfo = $tw.config.typeInfo[type];
if(!typeInfo) {
typeInfo = $tw.config.typeInfo["text/vnd.tiddlywiki"];
}
var extension = typeInfo.extension || "";
// Assemble the new fileInfo // Assemble the new fileInfo
fileInfo = {}; fileInfo = {};
fileInfo.filepath = $tw.boot.wikiTiddlersPath + "/" + self.generateTiddlerFilename(title,extension,files); fileInfo.filepath = $tw.boot.wikiTiddlersPath + "/" + self.generateTiddlerFilename(title,extension,files);
fileInfo.type = typeInfo.fileType; fileInfo.type = typeInfo.fileType || tiddler.fields.type;
fileInfo.hasMetaFile = typeInfo.hasMetaFile; fileInfo.hasMetaFile = typeInfo.hasMetaFile;
// Save the newly created fileInfo // Save the newly created fileInfo
$tw.boot.files[title] = fileInfo; $tw.boot.files[title] = fileInfo;
@ -99,19 +98,40 @@ Save a tiddler and invoke the callback with (err,adaptorInfo,revision)
*/ */
FileSystemAdaptor.prototype.saveTiddler = function(tiddler,callback) { FileSystemAdaptor.prototype.saveTiddler = function(tiddler,callback) {
this.getTiddlerFileInfo(tiddler,function(err,fileInfo) { this.getTiddlerFileInfo(tiddler,function(err,fileInfo) {
var template, content, encoding;
if(err) { if(err) {
return callback(err); return callback(err);
} }
var template = $tw.config.typeTemplates[fileInfo.type]; if(fileInfo.hasMetaFile) {
console.log(fileInfo,template) // Save the tiddler as a separate body and meta file
var content = $tw.wiki.renderTiddler("text/plain",template,{tiddlerTitle: tiddler.fields.title}); console.log("Saving fileInfo",fileInfo)
fs.writeFile(fileInfo.filepath,content,{encoding: "utf8"},function (err) { var typeInfo = $tw.config.contentTypeInfo[fileInfo.type];
if(err) { console.log("Saving typeInfo",typeInfo)
return callback(err); fs.writeFile(fileInfo.filepath,tiddler.fields.text,{encoding: typeInfo.encoding},function(err) {
} if(err) {
return callback(err);
}
content = $tw.wiki.renderTiddler("text/plain","$:/core/templates/tiddler-metadata",{tiddlerTitle: tiddler.fields.title});
fs.writeFile(fileInfo.filepath + ".meta",content,{encoding: "utf8"},function (err) {
if(err) {
return callback(err);
}
console.log("FileSystem: Saved file",fileInfo.filepath); console.log("FileSystem: Saved file",fileInfo.filepath);
callback(null,{},0); callback(null,{},0);
}); });
});
} else {
// Save the tiddler as a self contained templated file
template = $tw.config.typeTemplates[fileInfo.type];
content = $tw.wiki.renderTiddler("text/plain",template,{tiddlerTitle: tiddler.fields.title});
fs.writeFile(fileInfo.filepath,content,{encoding: "utf8"},function (err) {
if(err) {
return callback(err);
}
console.log("FileSystem: Saved file",fileInfo.filepath);
callback(null,{},0);
});
}
}); });
}; };