-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.js
More file actions
139 lines (122 loc) · 4.68 KB
/
init.js
File metadata and controls
139 lines (122 loc) · 4.68 KB
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
/*
Usage: node init.js path_to_folder_to_use_as_wiki_content
This script recursively walks through a folder and adds each file to Ethertoff’s mongo livedb/share.js storage
Binary files are not actually stored:
{
"_id" : "1cef0b09-c1b5-41b2-8d5c-45711be62aac",
"absPath" : "/Users/e/Documents/Titanium_Studio_Workspace/raduga-server/geo.pyc",
"path" : "geo.pyc",
"mime" : "application/octet-stream",
"binary" : true
}
Text-based files get the data stored in the mongo database directly:
{
"_id" : "57bab03c-b703-4524-a122-4331283ce73e",
"absPath" : "/Users/e/Documents/Titanium_Studio_Workspace/raduga-server/alerts.py",
"path" : "alerts.py",
"mime" : "text/x-python",
"binary" : false,
"data" : "# -*- coding: utf-8 -*-\n\nimport json\nimport sys\nimport urllib2\n\nimport pymongo\n\nfrom utils import logger\nfrom settings import *\nfrom users import synch_users, delayed_synch_users\n\nclient = pymongo.MongoClient()\n(…)"
}
To find out a files mime-type and whether it is binary or not, the script relies on file extensions,
with a fall back to the libmagic library—much in the way webservers like Apache or nginx do it.
*/
// npm
var livedb = require('derby/node_modules/racer/node_modules/share/node_modules/livedb');
var mime = require('serve-static/node_modules/send/node_modules/mime');
var mmm = require('mmmagic'),
Magic = mmm.Magic;
var textExtensions = require('istextorbinary/node_modules/textextensions');
var binaryExtensions = require('istextorbinary/node_modules/binaryextensions');
var dive = require('dive');
var uuid = require('uuid').v4;
// local
var defaults = require('./config/defaults');
// node
var path = require('path');
var fs = require('fs');
var sys = require('sys');
// Parse command line options
if (process.argv.length > 3) {
console.log("Too many arguments");
console.log("Usage: node init.js path_to_folder_to_use_as_wiki_content");
process.exit(1);
}
// Read in configuration from the ethertoff derby.js project
for(var key in defaults) {
process.env[key] = process.env[key] || defaults[key];
}
// initialise database and mime-stuff
mime.default_type = "unknown";
var magic = new Magic(mmm.MAGIC_MIME_TYPE | mmm.MAGIC_MIME_ENCODING);
var db = require('livedb-mongo')(process.env.MONGO_URL + '?auto_reconnect', {safe:true});
var backend = livedb.client(db);
// initialise other vars
// if no folder is specified, we load the example content
var folder = process.argv.length === 3 ? process.argv[2] : 'example_content';
// TODO: create a separate script that initialises Ethertoff with process.cwd() as folder
var toDo = 0;
// this function we are going to need to insert documents
var insertDoc = function(doc) {
var submit = function(doc) {
backend.submit(process.env.COLLECTION_NAME, doc._id, {
create : {
type : 'json0',
data : doc
}
}, function(err, version, transformedByOps, snapshot) {
toDo -= 1;
if (toDo === 0) db.close();
});
};
if (doc.binary) {
submit(doc);
} else {
fs.readFile(doc.absPath, "utf-8", function(err, contents) {
doc.text = contents;
submit(doc);
});
}
};
// First drop the existing collection, then go!
db.mongo.dropCollection(process.env.COLLECTION_NAME, function(err, reply) {
db.mongo.dropCollection(process.env.COLLECTION_NAME + "_ops", function(err, reply) {
console.log("dropped existing collections");
dive(folder, {}, function(err, file) {
if (err) throw err;
toDo += 1;
var doc = {};
doc._id = uuid();
doc.absPath = file;
doc.path = path.relative(folder, file);
doc.mime = mime.lookup(file);
var extension = path.extname(file).replace('.','');
if (binaryExtensions.indexOf(extension) >= 0) {
doc.binary = true;
} else if (textExtensions.indexOf(extension) >= 0) {
doc.binary = false;
} else {
doc.binary = 'unknown';
}
if(doc.mime === "unknown" || doc.binary === "unknown") {
// libmagic to the rescue!
magic.detectFile(file, function(err, result) {
if (err) throw err;
doc.mime = result.split(';')[0];
if (result.indexOf("binary") >= 0) {
doc.binary = true;
} else {
doc.binary = false;
}
console.log(doc);
insertDoc(doc);
});
} else {
console.log(doc);
insertDoc(doc);
}
}, function() {
console.log('complete');
});
});
});