This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·311 lines (245 loc) · 7.91 KB
/
index.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
303
304
305
306
307
308
309
310
311
#!/usr/bin/env node
const commander = require('commander');
const filehash = require('./lib/filehash');
const rar = require('./lib/rar');
const uid = require('uid-safe');
const Table = require('cli-table');
const rmrf = require('rimraf');
const path = require('path');
const fs = require('fs');
const package = require('./package.json');
const mega = require('./lib/mega');
const db = require('./lib/db');
const Account = require('./models/account');
const File = require('./models/file');
const MAX_STORAGE = 53000000000; // ~50GB
commander
.version(package.version)
.command('upload <file>')
.description('Uploads a new file to the first available backup account')
.action(async (file) => {
const password = uid.sync(32);
try {
var hash = await filehash(file);
} catch (e) {
console.error('Failed to compute file hash!');
console.error(e);
process.exit(1);
}
console.log(`File hash is: ${hash}`);
if (await File.findOne({hash, type: 'backup'})) {
console.error('This file is already on a backup account!');
process.exit(1);
}
console.log(`Adding ${file} to passworded rar archive (PASSWD: ${password})...`);
try {
var rarFile = await rar.rar(file, password);
} catch (e) {
console.error('Failed to rar archive!');
console.error(e);
process.exit(1);
}
const size = fs.statSync(rarFile).size;
const availableAccount = await Account.findOne({type: 'backup', usedStorage: {$lt: MAX_STORAGE - size}});
if (!availableAccount) {
fs.unlinkSync(rarFile);
console.error('Could not find a backup account with enough space. Please add a new backup account.');
process.exit(1);
}
console.log(`Uploading to mega (${availableAccount.email})...`);
try {
await mega.upload(availableAccount.email, availableAccount.password, rarFile, hash + '.rar');
} catch (e) {
fs.unlinkSync(rarFile);
console.error('Could not upload file to mega!');
console.error(e);
process.exit(1);
}
console.log('File uploaded to mega!');
await File.create({
name: path.basename(file),
hash,
password,
account: availableAccount,
type: 'backup'
});
availableAccount.usedStorage += size;
await availableAccount.save();
fs.unlinkSync(rarFile);
db.close();
});
commander.command('search [regex]')
.description('Looks for files matching the regex')
.action(async (regex) => {
regex = regex || '.';
let results = await File.find({name: new RegExp(regex)}).populate('account');
let tbl = new Table({head: ['Hash', 'Type', 'Account', 'Name', 'Password']});
console.log(results.length + ' result(s).');
for (let result of results) {
tbl.push([result.hash, result.type, result.account.email, result.name, result.password]);
}
console.log(tbl.toString());
db.close();
});
commander
.command('publish <hash>')
.description('Publishes a file from a backup account to the first available sharing account')
.action(async (hash) => {
if (await File.findOne({hash, type: 'sharing'})) {
console.error('This file is already on a sharing account!');
process.exit(1);
}
let file = await File.findOne({hash, type: 'backup'}).populate('account');
if (!file) {
console.error('Hash not found on any backup accounts!');
process.exit(1);
}
console.log(`Downloading from backup account ${file.account.email}...`);
try {
var rarFile = await mega.download(file.account.email, file.account.password, hash);
} catch (e) {
console.error('Could not download file from backup account!');
console.error(e);
process.exit(1);
}
console.log('Extracting file...');
try {
var dir = await rar.unrar(rarFile, file.password);
} catch (e) {
console.error('Could not extract file!');
console.error(e);
process.exit(1);
}
fs.unlinkSync(rarFile);
const filePath = path.join(dir, file.name);
const password = uid.sync(32);
console.log(`Creating new passworded rar archive (PASSWD: ${password})...`);
try {
rarFile = await rar.rar(filePath, password);
} catch (e) {
console.error('Failed to rar archive!');
console.error(e);
process.exit(1);
}
rmrf.sync(dir);
const size = fs.statSync(rarFile).size;
const availableAccount = await Account.findOne({type: 'sharing', usedStorage: {$lt: MAX_STORAGE - size}});
if (!availableAccount) {
fs.unlinkSync(rarFile);
console.error('Could not find a sharing account with enough space. Please add a new sharing account.');
process.exit(1);
}
console.log(`Uploading to mega (${availableAccount.email})...`);
try {
await mega.upload(availableAccount.email, availableAccount.password, rarFile, hash + '.rar');
} catch (e) {
fs.unlinkSync(rarFile);
console.error('Could not upload file to mega!');
console.error(e);
process.exit(1);
}
console.log('File uploaded to mega!');
await File.create({
name: file.name,
hash,
password,
account: availableAccount,
type: 'sharing'
});
availableAccount.usedStorage += size;
await availableAccount.save();
try {
var fileUrl = await mega.getLink(availableAccount.email, availableAccount.password, hash);
console.log('You can now download this file using the following URL: ' + fileUrl);
} catch (e) {
console.error('Could not retreive link.');
console.error(e);
}
fs.unlinkSync(rarFile);
db.close();
});
commander.command('account <email> <password>')
.description('Adds a new account to the database')
.option('-t, --type [type]', 'The account type (backup or sharing) [backup]', 'backup')
.action(async (email, password, options) => {
if (!Account.ACCOUNT_TYPES.includes(options.type)) {
console.error('--type should either be backup or sharing');
process.exit(1);
}
try {
var usedStorage = await mega.getUsedStorage(email, password);
} catch (e) {
console.error('Could not add account!');
console.error(e);
process.exit(1);
}
await Account.create({
email,
password,
usedStorage,
type: options.type
});
console.log(`Added ${email} as a ${options.type} account.`);
db.close();
});
commander.command('refresh [email]')
.description('Updates used torage of all accounts or just a single one')
.action(async (email) => {
let accounts;
if (email) {
accounts = [await Account.findOne({email})];
} else {
accounts = await Account.find();
}
for (let account of accounts) {
if (!account) continue;
try {
var usedStorage = await mega.getUsedStorage(account.email, account.password);
} catch (e) {
console.error(`Could not refresh ${account.email}!`);
}
account.usedStorage = usedStorage;
await account.save();
console.log(`Refreshed ${account.email}`);
}
db.close();
});
commander.command('rm <hash>')
.description('Removes a file from the cluster')
.option('-t, --type [type]', 'The account type to remove the file from (backup or sharing) [sharing]', 'sharing')
.option('-f, --force', 'Remove the file from the database even if it is not removed from mega')
.action(async (hash, options) => {
if (!Account.ACCOUNT_TYPES.includes(options.type)) {
console.error('--type should either be backup or sharing');
process.exit(1);
}
let file = await File.findOne({hash, type: options.type}).populate('account');
if (!file) {
console.error('Hash not found!');
process.exit(1);
}
try {
await mega.rm(file.account.email, file.account.password, hash + '.rar');
} catch (e) {
if (!options.force) {
console.error(`Could not remove file from ${file.account.email}! If it is not on that account anymore, call this command with -f`);
console.error(e);
process.exit(1);
}
}
await file.remove();
console.log(`${hash} successfully removed.`);
db.close();
});
commander.command('*')
.description('output usage information')
.action(() => {
commander.help();
});
// Treats unhandled errors in async code as regular errors.
process.on('unhandledRejection', (err) => {
console.error(err);
process.exit(1);
});
commander.parse(process.argv);
if (!commander.args.length) commander.help();