mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-10-24 20:27:38 +00:00
Add support for importing encrypted TiddlyWiki documents
This commit is contained in:
@@ -1071,17 +1071,70 @@ exports.readFile = function(file,callback) {
|
||||
// Onload
|
||||
reader.onload = function(event) {
|
||||
// Deserialise the file contents
|
||||
var tiddlerFields = {title: file.name || "Untitled", type: type};
|
||||
var text = event.target.result,
|
||||
tiddlerFields = {title: file.name || "Untitled", type: type};
|
||||
// Are we binary?
|
||||
if(isBinary) {
|
||||
// The base64 section starts after the first comma in the data URI
|
||||
var commaPos = event.target.result.indexOf(",");
|
||||
var commaPos = text.indexOf(",");
|
||||
if(commaPos !== -1) {
|
||||
tiddlerFields.text = event.target.result.substr(commaPos+1);
|
||||
tiddlerFields.text = text.substr(commaPos+1);
|
||||
callback([tiddlerFields]);
|
||||
}
|
||||
} else {
|
||||
callback(self.deserializeTiddlers(type,event.target.result,tiddlerFields));
|
||||
// Check whether this is an encrypted TiddlyWiki file
|
||||
var encryptedStoreAreaStartMarker = "<pre id=\"encryptedStoreArea\" type=\"text/plain\" style=\"display:none;\">",
|
||||
encryptedStoreAreaStart = text.indexOf(encryptedStoreAreaStartMarker),
|
||||
encryptedStoreAreaEnd = encryptedStoreAreaStart !== -1 ? text.indexOf("</pre>",encryptedStoreAreaStart) : -1;
|
||||
if(encryptedStoreAreaStart !== -1 && encryptedStoreAreaEnd !== -1) {
|
||||
var encryptedJson = $tw.utils.htmlDecode(text.substring(encryptedStoreAreaStart + encryptedStoreAreaStartMarker.length,encryptedStoreAreaEnd-1)),
|
||||
attemptDecryption = function() {
|
||||
var decryptedText = $tw.crypto.decrypt(encryptedJson);
|
||||
if(decryptedText) {
|
||||
var json = JSON.parse(decryptedText),
|
||||
tiddlers = [];
|
||||
for(var title in json) {
|
||||
tiddlers.push(json[title]);
|
||||
}
|
||||
return tiddlers;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
// Try to decrypt with the current password
|
||||
var tiddlers = attemptDecryption();
|
||||
if(tiddlers) {
|
||||
callback(tiddlers);
|
||||
} else {
|
||||
// Prompt for a new password and keep trying
|
||||
$tw.passwordPrompt.createPrompt({
|
||||
serviceName: "Enter a password to decrypt the imported TiddlyWiki",
|
||||
noUserName: true,
|
||||
canCancel: true,
|
||||
submitText: "Decrypt",
|
||||
callback: function(data) {
|
||||
// Exit if the user cancelled
|
||||
if(!data) {
|
||||
return false;
|
||||
}
|
||||
// Attempt to decrypt the tiddlers
|
||||
$tw.crypto.setPassword(data.password);
|
||||
var tiddlers = attemptDecryption();
|
||||
if(tiddlers) {
|
||||
callback(tiddlers);
|
||||
// Exit and remove the password prompt
|
||||
return true;
|
||||
} else {
|
||||
// We didn't decrypt everything, so continue to prompt for password
|
||||
return false;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Try to deserialise any tiddlers in the file
|
||||
callback(self.deserializeTiddlers(type,text,tiddlerFields));
|
||||
}
|
||||
}
|
||||
};
|
||||
// Kick off the read
|
||||
|
||||
Reference in New Issue
Block a user