mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-06-28 08:13:14 +00:00
Add support for saving binary files
This commit is contained in:
parent
19b62c30d8
commit
10b192e743
8
core/templates/tiddler-metadata.tid
Normal file
8
core/templates/tiddler-metadata.tid
Normal 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>
|
|
@ -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,13 +43,6 @@ 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];
|
||||||
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);
|
|
||||||
}
|
|
||||||
// Get information about how to save tiddlers of this type
|
// Get information about how to save tiddlers of this type
|
||||||
var type = tiddler.fields.type || "text/vnd.tiddlywiki",
|
var type = tiddler.fields.type || "text/vnd.tiddlywiki",
|
||||||
typeInfo = $tw.config.typeInfo[type];
|
typeInfo = $tw.config.typeInfo[type];
|
||||||
|
@ -58,10 +50,17 @@ FileSystemAdaptor.prototype.getTiddlerFileInfo = function(tiddler,callback) {
|
||||||
typeInfo = $tw.config.typeInfo["text/vnd.tiddlywiki"];
|
typeInfo = $tw.config.typeInfo["text/vnd.tiddlywiki"];
|
||||||
}
|
}
|
||||||
var extension = typeInfo.extension || "";
|
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
|
// 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,12 +98,32 @@ 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)
|
||||||
|
var typeInfo = $tw.config.contentTypeInfo[fileInfo.type];
|
||||||
|
console.log("Saving typeInfo",typeInfo)
|
||||||
|
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);
|
||||||
|
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) {
|
fs.writeFile(fileInfo.filepath,content,{encoding: "utf8"},function (err) {
|
||||||
if(err) {
|
if(err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
@ -112,6 +131,7 @@ console.log(fileInfo,template)
|
||||||
console.log("FileSystem: Saved file",fileInfo.filepath);
|
console.log("FileSystem: Saved file",fileInfo.filepath);
|
||||||
callback(null,{},0);
|
callback(null,{},0);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user