-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystemfunctions.js
302 lines (279 loc) · 11.1 KB
/
filesystemfunctions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*\
title: $:/plugins/OokTech/Gatekeeper/filesystemfunctions.js
type: application/javascript
module-type: startup
A module that contains functions used to save tiddlers as files
\*/
(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;
var setup = function () {
//$tw.utils.createDirectory($tw.boot.wikiTiddlersPath);
$tw.Gatekeeper.FileSystemFunctions = $tw.Gatekeeper.FileSystemFunctions || {};
/*
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 $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.
*/
$tw.Gatekeeper.FileSystemFunctions.getTiddlerFileInfo = function(tiddler,callback) {
if (!callback) {
callback = function () {
}
}
// See if we've already got information about this file
var self = this,
title = tiddler.fields.title,
fileInfo = $tw.boot.files[title];
if(fileInfo) {
// If so, just invoke the callback
callback(null,fileInfo);
} else {
// Otherwise, we'll need to generate it
fileInfo = {};
var tiddlerType = tiddler.fields.type || "text/vnd.tiddlywiki";
// Get the content type info
var contentTypeInfo = $tw.config.contentTypeInfo[tiddlerType] || {};
// Get the file type by looking up the extension
var extension = contentTypeInfo.extension || ".tid";
fileInfo.type = ($tw.config.fileExtensionInfo[extension] || {type: "application/x-tiddler"}).type;
// Use a .meta file unless we're saving a .tid file.
// (We would need more complex logic if we supported other template rendered tiddlers besides .tid)
fileInfo.hasMetaFile = (fileInfo.type !== "application/x-tiddler") && (fileInfo.type !== "application/json");
if(!fileInfo.hasMetaFile) {
extension = ".tid";
}
// Generate the base filepath and ensure the directories exist
var baseFilepath = path.resolve($tw.boot.wikiTiddlersPath,$tw.Gatekeeper.FileSystemFunctions.generateTiddlerBaseFilepath(title));
$tw.utils.createFileDirectories(baseFilepath);
// Start by getting a list of the existing files in the directory
fs.readdir(path.dirname(baseFilepath),function(err,files) {
if(err) {
return callback(err);
}
// Start with the base filename plus the extension
var filepath = baseFilepath;
if(filepath.substr(-extension.length).toLocaleLowerCase() !== extension.toLocaleLowerCase()) {
filepath = filepath + extension;
}
var filename = path.basename(filepath),
count = 1;
// Add a discriminator if we're clashing with an existing filename while
// handling case-insensitive filesystems (NTFS, FAT/FAT32, etc.)
while(files.some(function(value) {return value.toLocaleLowerCase() === filename.toLocaleLowerCase();})) {
filepath = baseFilepath + " " + (count++) + extension;
filename = path.basename(filepath);
}
// Set the final fileInfo
fileInfo.filepath = filepath;
console.log("\x1b[1;35m" + "For " + title + ", type is " + fileInfo.type + " hasMetaFile is " + fileInfo.hasMetaFile + " filepath is " + fileInfo.filepath + "\x1b[0m");
$tw.boot.files[title] = fileInfo;
// Pass it to the callback
callback(null,fileInfo);
});
}
};
/*
Given a list of filters, apply every one in turn to source, and return the first result of the first filter with non-empty result.
*/
$tw.Gatekeeper.FileSystemFunctions.findFirstFilter = function(filters,source) {
for(var i=0; i<filters.length; i++) {
var result = $tw.wiki.filterTiddlers(filters[i],null,source);
if(result.length > 0) {
return result[0];
}
}
return null;
};
/*
Given a tiddler title and an array of existing filenames, generate a new legal filename for the title, case insensitively avoiding the array of existing filenames
*/
$tw.Gatekeeper.FileSystemFunctions.generateTiddlerBaseFilepath = function(title) {
var baseFilename;
// Check whether the user has configured a tiddler -> pathname mapping
var pathNameFilters = $tw.wiki.getTiddlerText("$:/config/FileSystemPaths");
if(pathNameFilters) {
var source = $tw.wiki.makeTiddlerIterator([title]);
baseFilename = $tw.Gatekeeper.FileSystemFunctions.findFirstFilter(pathNameFilters.split("\n"),source);
if(baseFilename) {
// Interpret "/" and "\" as path separator
baseFilename = baseFilename.replace(/\/|\\/g,path.sep);
}
}
if(!baseFilename) {
// No mappings provided, or failed to match this tiddler so we use title as filename
baseFilename = title.replace(/\/|\\/g,"_");
}
// Remove any of the characters that are illegal in Windows filenames
var baseFilename = $tw.utils.transliterate(baseFilename.replace(/<|>|\:|\"|\||\?|\*|\^/g,"_"));
// Truncate the filename if it is too long
if(baseFilename.length > 200) {
baseFilename = baseFilename.substr(0,200);
}
return baseFilename;
};
/*
Save a tiddler and invoke the callback with (err,adaptorInfo,revision)
*/
$tw.Gatekeeper.FileSystemFunctions.saveTiddler = function(tiddler,callback) {
if (!callback) {
callback = function () {
}
}
if (tiddler && $tw.Gatekeeper.ExcludeList.indexOf(tiddler.fields.title) === -1 && !tiddler.fields.title.startsWith('$:/state/') && !tiddler.fields.title.startsWith('$:/temp/')) {
console.log('save tiddler function')
var self = this;
$tw.Gatekeeper.FileSystemFunctions.getTiddlerFileInfo(tiddler,function(err,fileInfo) {
if(err) {
return callback(err);
}
var filepath = fileInfo.filepath,
error = $tw.utils.createDirectory(path.dirname(filepath));
if(error) {
return callback(error);
}
if(fileInfo.hasMetaFile) {
// Save the tiddler as a separate body and meta file
var typeInfo = $tw.config.contentTypeInfo[tiddler.fields.type || "text/plain"] || {encoding: "utf8"};
fs.writeFile(filepath,tiddler.fields.text,{encoding: typeInfo.encoding},function(err) {
if(err) {
return callback(err);
}
content = $tw.wiki.renderTiddler("text/plain","$:/core/templates/tiddler-metadata",{variables: {currentTiddler: tiddler.fields.title}});
fs.writeFile(fileInfo.filepath + ".meta",content,{encoding: "utf8"},function (err) {
if(err) {
return callback(err);
}
//self.logger.log("Saved file",filepath);
return callback(null);
});
});
} else {
console.log('saving')
// Save the tiddler as a self contained templated file
var content = makeTiddlerFile(tiddler);
fs.writeFile(filepath,content,{encoding: "utf8"},function (err) {
if(err) {
return callback(err);
}
console.log('saved file', filepath)
$tw.wiki.addTiddler(new $tw.Tiddler(tiddler.fields));
Object.keys($tw.connections).forEach(function(connection) {
$tw.Gatekeeper.WaitingList[connection][tiddler.fields.title] = true;
});
//self.logger.log("Saved file",filepath);
return callback(null);
});
}
});
}
};
function makeTiddlerFile(tiddler) {
var output = "";
Object.keys(tiddler.fields).forEach(function(fieldName, index) {
if (fieldName === 'created' || fieldName === 'modified') {
output += `${fieldName}: ${$tw.utils.stringifyDate(new Date(tiddler.fields[fieldName]))}\n`;
} else if (fieldName === 'list'){
output += `${fieldName}: ${$tw.utils.stringifyList(tiddler.fields[fieldName])}`;
} else if (fieldName !== 'text') {
output += `${fieldName}: ${tiddler.fields[fieldName]}\n`;
}
})
output += `\n${tiddler.fields.text}`;
return output;
}
/*
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.
*/
$tw.Gatekeeper.FileSystemFunctions.loadTiddler = function(title,callback) {
if (!callback) {
callback = function () {
}
}
callback(null,null);
};
/*
Delete a tiddler and invoke the callback with (err)
*/
$tw.Gatekeeper.FileSystemFunctions.deleteTiddler = function(title,callback,options) {
if (!callback) {
callback = function () {
}
}
var self = this,
fileInfo = $tw.boot.files[title];
// Only delete the tiddler if we have writable information for the file
if(fileInfo) {
//console.log(fileInfo.filepath)
// Delete the file
fs.unlink(fileInfo.filepath,function(err) {
if(err) {
return callback(err);
}
//self.logger.log("Deleted file",fileInfo.filepath);
// Delete the metafile if present
if(fileInfo.hasMetaFile) {
fs.unlink(fileInfo.filepath + ".meta",function(err) {
if(err) {
return callback(err);
}
return $tw.utils.deleteEmptyDirs(path.dirname(fileInfo.filepath),callback);
});
} else {
return $tw.utils.deleteEmptyDirs(path.dirname(fileInfo.filepath),callback);
}
});
} else {
callback(null);
}
};
/*
Check if the file version matches the in-browser version of a tiddler
*/
$tw.Gatekeeper.FileSystemFunctions.TiddlerHasChanged = function (tiddler, tiddlerFileObject) {
if (!tiddlerFileObject) {
return true;
}
if (!tiddler) {
return true;
}
var changed = false;
var longer = Object.keys(tiddler.fields).length > Object.keys(tiddlerFileObject.tiddlers[0])?Object.keys(tiddler.fields).length:Object.keys(tiddlerFileObject.tiddlers[0]);
// check to see if the field values are the same, ignore modified for now
longer.forEach(function(field) {
if (field !== 'modified' && field !== 'created' && field !== 'list' && field !== 'tags') {
if (!tiddlerFileObject.tiddlers[0][field] || tiddlerFileObject.tiddlers[0][field] !== tiddler.fields[field]) {
// There is a difference!
changed = true;
}
} else if (field === 'list' || field === 'tags') {
if (tiddler.fields[field] && tiddlerFileObject.tiddlers[0][field]) {
if ($tw.utils.parseStringArray(tiddlerFileObject.tiddlers[0][field]).length !== tiddler.fields[field].length) {
changed = true;
} else {
var arrayList = $tw.utils.parseStringArray(tiddlerFileObject.tiddlers[0][field]);
arrayList.forEach(function(item) {
if (tiddler.fields[field].indexOf(item) === -1) {
changed = true;
}
})
}
} else {
changed = true;
}
}
})
return changed;
};
}
if(fs) {
setup();
}
})();