forked from SINTEF-9012/Caracal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
813 lines (681 loc) · 21.5 KB
/
server.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
var express = require('express'),
compression = require('compression'),
morgan = require('morgan'),
formidable = require('formidable'),
mime = require('mime'),
fs = require('fs'),
path = require('path'),
url = require('url'),
http = require('follow-redirects').http,
https = require('follow-redirects').https,
crypto = require('crypto'),
temp = require('temp'),
gm = require('gm'),
async = require('async'),
Nedb = require('nedb'),
ffmpeg = require('fluent-ffmpeg'),
cors = require('cors');
var config = {
// Listening HTTP Port
port: process.env.HTTP_PORT || 8075,
// HTTP Cache value for the stored files
cache: process.env.CACHE || "max-age=290304000, public",
// The user agent used by the HTTP client to fetch distant files
"User-Agent": process.env.UA || "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0",
// A password required to delete files.
// /!\ Please change it. /!\
"deletions-key": process.env.DELETIONS_KEY || "caracal18",
// An authentication association table (object) used for basic HTTP authentifications
// keys are the host-names, values are the passwords
auths: process.env.AUTHS ? JSON.parse(process.env.AUTHS) : {},
// Number of allowed concurrent GraphicsMagick processes
concurrency: process.env.CONCURRENCY ? parseInt(process.env.CONCURRENCY) : 8,
// Number of allowed concurrent FFmpeg processes
// Is CONCURRENCY divided by 4 by default
videoConcurrency: process.env.VIDEO_CONCURRENCY ? parseInt(process.env.VIDEO_CONCURRENCY) : (
process.env.CONCURRENCY ? Math.min(Math.round(parseInt(process.env.CONCURRENCY)/4)) : 2
),
// Location of the storage folder, server side
datapath: process.env.DATAPATH || './data',
// Array of allowed resizing sizes
// So the user cannot ask every resized image pixel by pixel
// If a requested size is not found, the closest size from this array is used.
// Define it to '*' to allow every size (not recommended)
// The default configuration contains many sizes, you might want to use fewer sizes
allowedSizes: process.env.ALLOWED_SIZES ? JSON.parse(process.env.ALLOWED_SIZES) :
[ 32, 64, 128, 256, 1024, 2048, 4096, 8192, 16384,
50, 100, 200, 400, 500, 600, 800, 1000, 1050, 1200, 1600,
120, 160, 240, 320, 480, 576, 640, 768, 854, 960, 1050, 1080,
1152, 1280, 1440, 1536, 1716, 1920, 2160, 2560, 3200,
3840, 3996, 4320, 4800, 5120, 6400, 6144, 7680, 12288,
],
// Array of allowed resizing video size. This MUST be an array
allowedVideoSizes: process.env.ALLOWED_VIDEO_SIZES ? JSON.parse(process.env.ALLOWED_VIDEO_SIZES) :
[ 144, 240, 360, 480, 720, 1080, 3840],
// List of allowed domains for CORS
allowedDomains: process.env.ALLOWED_DOMAINS ? JSON.parse(process.env.ALLOWED_DOMAINS) : '*',
// Dependencie used to generate ids
idsGeneration: process.env.IDS_GENERATION ? process.env.IDS_GENERATION : 'sillyid',
};
// Generation of ids
// The ids must never start by http: or https:
// They should never have path characters
// Accepted characters
// a-z A-Z 0-9 _-
// No accepted (and many others)
// : / \ . < > ( ) [ ]
// GenerateId has the sha256 checksum as optional argument
var generateId = (hash) => hash;
const requiresAdmin = (req, res, next) => {
if (req.user._json._profile.app_metadata.roles[0] === 'admin') {
next();
} else {
res.status(401).json({message: 'Please log in to post pictures.'})
}
};
switch (config.idsGeneration) {
case 'hash':
// Just return the hash
break;
case 'shortid':
var shortid = require('shortid');
generateId = () => shortid.generate();
break;
case 'human-readable':
var humanreadableid = require('human-readable-ids').hri;
generateId = () => humanreadableid.random();
break;
case 'bronze':
var bronze = require('bronze');
var bronzeInstance = new bronze();
generateId = () => bronzeInstance.generate();
break;
case 'sillyid':
default:
var sillyid = require('sillyid');
var sillyidInstance = new sillyid();
generateId = () => sillyidInstance.generate();
}
var datapath = config.datapath;
if (datapath.slice(-1) !== '/') {
datapath += '/';
}
var uploadDatapath = datapath+"uploads";
// Create the uploads folder if necessary
if (!fs.existsSync(uploadDatapath)){
fs.mkdirSync(uploadDatapath);
}
// Open the databases
var filesDb = new Nedb({filename: datapath+'files.db', autoload:true}),
picturesSizeDb = new Nedb({filename: datapath+'picturesSizes.db', autoload:true}),
idsDb = new Nedb({filename: datapath+'ids.db', autoload: true});
// Create the indexes, sparse allows multiple documents with an undefined field
filesDb.ensureIndex({ fieldName: 'url', unique: true, sparse: true });
filesDb.ensureIndex({ fieldName: 'mtime', unique: false, sparse: true });
idsDb.ensureIndex({ fieldName: 'id', unique: true, sparse: true });
idsDb.ensureIndex({ fieldName: 'hash', unique: true, sparse: true });
var gmWorker = async.queue((task, callback) => {
task(callback);
}, config.concurrency);
var ffmpegWorker = async.queue((task, callback) => {
task(callback);
}, config.videoConcurrency);
var app = express();
app.use(session({
store: new RedisStore({
host: redis,
port: 6379,
client,
ttl: 7 * 24 * 60 * 60, // expiration from redis in seconds
}),
secret: secrets.session,
resave: false,
rolling: true,
saveUninitialized: false,
}));
app.use(compression());
app.use(morgan('short'));
if (Array.isArray(config.allowedDomains)) {
app.use(cors({
origin: function(origin, callback){
var originIsWhitelisted = config.allowedDomains.indexOf(origin) !== -1;
callback(null, originIsWhitelisted);
}
}));
}
else {
app.use(cors());
}
app.use(express.static(__dirname + '/public'));
app.use(express.static(path.resolve(uploadDatapath)));
app.get('/files', (req, res) => {
filesDb.find({}).sort({mtime: -1}).exec((err, docs) => {
for (var i = 0, l = docs.length; i < l; ++i) {
delete docs[i]._id;
}
res.send(docs);
});
});
app.get(/^\/([a-zA-Z0-9_\-]+)$/, (req, res) => {
var id = req.params[0];
convertIdToHashAndPath(id, (infos) => {
res.sendFile(infos.fullPath, {root: __dirname});
}, () => {
res.status(404).send("File not found.");
});
});
app.get('/paginateFiles/:page', (req, res) => {
var pageSize = req.query.hasOwnProperty('pageSize') ?
Math.max(2, (parseInt(req.query.pageSize) || 0)) : 10,
page = Math.max(-1, (parseInt(req.params.page) || 0));
var req = filesDb.find({});
if (page >= 0) {
req.sort({mtime: 1});
req.skip(pageSize * page);
req.limit(pageSize);
} else {
req.sort({mtime: -1});
req.limit(pageSize);
}
req.exec((err, docs) => {
for (var i = 0, l = docs.length; i < l; ++i) {
delete docs[i]._id;
}
var req = filesDb.count({}, (err, count) => {
res.json({
files: docs,
count: count
});
});
});
});
app.get(/^\/resize\/(deform\/)?(\d+)\/(\d+)\/([a-fA-F0-9]{40,64}\.[a-zA-Z0-9]+)$/, (req, res) => {
var path = req.params[3],
width = parseInt(req.params[1]),
height = parseInt(req.params[2]),
deform = !!req.params[0];
sendResizedImage(path, width, height, deform, res);
});
app.get(/^\/resize\/(deform\/)?(\d+)\/(\d+)\/([a-zA-Z0-9_\-]+)$/, (req, res) => {
var id = req.params[3],
width = parseInt(req.params[1]),
height = parseInt(req.params[2]),
deform = !!req.params[0];
convertIdToHashAndPath(id, (infos) => {
sendResizedImage(infos.path, width, height, deform, res);
}, () => {
res.status(404).send("File not found.");
});
});
app.get(/^\/thumbnail\/([a-fA-F0-9]{40,64}\.[a-zA-Z0-9]+)$/, (req, res) => {
var path = req.params[0];
sendThumbnail(path, res);
});
app.get(/^\/thumbnail\/([a-zA-Z0-9_\-]+)$/, (req, res) => {
var id = req.params[0];
convertIdToHashAndPath(id, (infos) => {
sendThumbnail(infos.path, res);
}, () => {
res.status(404).send("File not found.");
});
});
app.post('/upload', requiresAdmin, (req, res) => {
var form = new formidable.IncomingForm();
form.uploadDir = uploadDatapath;
form.keepExtensions = true;
form.hash = 'sha256';
form.parse(req, (err, fields, files) => {
if (err || !files) {
console.log("File upload error", err);
res.status(500).send("Sorry, file upload error");
return;
}
for (var key in files) {
var f = files[key];
var extension = mime.extension(f.name ? mime.lookup(f.name) : f.type);
var path = uploadDatapath+"/"+f.hash+"."+extension;
fs.exists(path, (exists) => {
if (exists) {
fs.unlink(f.path, () => {});
filesDb.findOne({hash: f.hash, extension}, (err, doc) => {
if (doc) {
doc.status = 'exists';
delete doc._id;
res.send(doc);
} else {
res.status(500).send("File's database record doesn't exist.");
}
});
} else {
fs.rename(f.path, path, () => {
convertHashToId(f.hash, extension, (beautifulId) => {
var documentInfos = {
name: f.name,
id: beautifulId,
size: f.size,
hash: f.hash,
extension: extension,
type: f.type,
mtime: f.lastModifiedDate,
};
filesDb.insert(documentInfos, () => {
documentInfos.status = 'ok';
res.send(documentInfos);
});
});
});
}
});
return;
};
})
});
app.get(/^\/remove\/([a-fA-F0-9]{40,64}\.[a-zA-Z0-9]+)$/, requiresAdmin, (req, res) => {
var path = req.params[0];
removeFile(path, req, res);
});
app.get(/^\/remove\/([a-zA-Z0-9_\-]+)$/, requiresAdmin, (req, res) => {
var id = req.params[0];
convertIdToHashAndPath(id, (infos) => {
removeFile(infos.path, req, res);
}, () => {
res.status(404).send("File not found.");
});
});
function removeFile(path, req, res) {
var deletionsKey = config['deletions-key'];
if (deletionsKey && deletionsKey !== req.query.key) {
res.status(403).send('Missing or wrong deletions key');
return;
}
var uploadPath = uploadDatapath+"/"+path;
fs.unlink(uploadPath, () => {});
var hashAndExtension = path.split('.');
var hash = hashAndExtension[0],
extension = hashAndExtension[1];
picturesSizeDb.find({path: path}, (err, docs) => {
if (err) {
res.status(500).send(err);
return;
}
docs.forEach((doc) => {
fs.unlink(doc.unlink, () => {});
});
picturesSizeDb.remove({path: path});
idsDb.remove({hash});
filesDb.remove({hash: hash, extension:extension}, {}, (err, numRemoved) => {
if (err) {
res.status(500).send(err);
return;
}
res.send({numRemoved:numRemoved});
});
});
};
app.get(/^\/details\/([a-fA-F0-9]{40,64}\.[a-zA-Z0-9]+)$/, (req, res) => {
var path = req.params[0];
var hashAndExtension = path.split('.');
var hash = hashAndExtension[0],
extension = hashAndExtension[1];
fileDetails(hash, extension, res);
});
app.get(/^\/details\/([a-zA-Z0-9_\-]+)$/, (req, res) => {
var id = req.params[0];
convertIdToHashAndPath(id, (infos) => {
fileDetails(infos.hash, infos.extension, res);
}, () => {
res.status(404).send("File not found.");
});
});
function fileDetails(hash, extension, res) {
filesDb.findOne({hash, extension}, (err, doc) => {
if (doc) {
delete doc._id;
res.send(doc);
} else {
res.status(404).send("File not found, sorry");
}
});
};
function createThumbnail(path, uploadPath, thumbnailPath, res) {
gmWorker.push((callback) => {
gm(uploadPath).autoOrient().thumb(128,128,thumbnailPath, 90, callback);
}, (err) => {
if (err) {
res.redirect('/broken_thumbnail.png');
console.log(err);
return;
}
picturesSizeDb.insert({unlink: thumbnailPath, path:path});
res.header('Cache-Control', config.cache);
res.sendFile(thumbnailPath, {root: __dirname});
});
}
function sendThumbnail(path, res) {
var thumbnailPath = uploadDatapath+"/thumbnail-"+path,
uploadPath = uploadDatapath+"/"+path;
var isVideo = /^video\//.test(mime.lookup(path));
if (isVideo) {
thumbnailPath += '.png';
}
fs.exists(thumbnailPath, (exists) => {
if (exists) {
res.header('Cache-Control', config.cache);
res.sendFile(thumbnailPath, {root: __dirname});
} else {
fs.exists(uploadPath, (exists) => {
if (!exists) {
res.status(404).send("File not found, sorry");
return;
}
if (isVideo) {
ffmpegWorker.push((callback) => {
ffmpeg(uploadPath).on('error', (err,stdout,stderr) => {
console.log(err);
res.redirect('/broken_thumbnail.png');
callback();
}).on('end', () => {
var ffmpegPath = uploadDatapath+'/ffmpeg-1-'+path+'.png'
picturesSizeDb.insert({unlink: ffmpegPath, path:path});
callback();
createThumbnail(path, ffmpegPath, thumbnailPath+'.png', res);
}).takeScreenshots({
count: 1,
timemarks: ['0.1'],
filename: 'ffmpeg-%i-%f'
}, uploadDatapath);
});
// If it's not a video, imagemagick will do the job
// We don't check the filetype, the error callback is triggered if
// imagemagick can't create a thumbnail
} else {
createThumbnail(path, uploadPath, thumbnailPath, res);
}
});
}
});
}
function closestInArray(array, goal) {
return array.reduce((prev, curr) =>
Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
}
function sendResizedImage(path, width, height, deform, res) {
// If only a set of sizes is allowed, use the closest sizes
if (Array.isArray(config.allowedSizes)) {
width = closestInArray(config.allowedSizes, width);
height = closestInArray(config.allowedSizes, height);
}
var resizedPath = "/"+width+"x"+height+(deform ? "-deform-" : "-" ) + path,
fullResizedPath = uploadDatapath+resizedPath,
uploadPath = uploadDatapath+"/"+path;
fs.exists(fullResizedPath, (exists) => {
if (exists) {
res.header('Cache-Control', config.cache);
res.sendFile(fullResizedPath, {root: __dirname});
} else {
fs.exists(uploadPath, (exists) => {
if (!exists) {
res.status(404).send("File not found, sorry");
return;
}
gmWorker.push((callback) => {
gm(uploadPath).autoOrient().resize(width,height, deform ? '!>' : '>')
.noProfile().write(fullResizedPath, callback);
}, (err) => {
if (err) {
console.log(err);
res.redirect('/broken_thumbnail.png');
return;
}
picturesSizeDb.insert({unlink: fullResizedPath, path:path});
res.header('Cache-Control', config.cache);
res.sendFile(fullResizedPath, {root: __dirname});
});
});
}
});
}
function sendConvertedVideo(path, format, size, res) {
size = closestInArray(config.allowedVideoSizes, size);
var convertedPath = "/ffmpeg-"+path+"-"+size+"."+format,
fullConvertedPath = uploadDatapath+"/"+convertedPath,
uploadPath = uploadDatapath+"/"+path;
fs.exists(fullConvertedPath, (exists) => {
if (exists) {
res.header('Cache-Control', config.cache);
res.sendFile(fullConvertedPath, {root: __dirname});
} else {
fs.exists(uploadPath, (exists) => {
if (!exists) {
res.status(404).send("File not found, sorry");
return;
}
console.log("The file "+path+" will be converted to "+format);
ffmpegWorker.push((callback) => {
var proc = ffmpeg(uploadPath);
if (format === 'mp4') {
proc.format('mp4')
.videoCodec('libx264')
.audioCodec('aac');
} else if (format === 'webm') {
proc.format('webm')
.videoCodec('libvpx')
//.videoBitrate('1024k')
.audioCodec('libvorbis');
}
proc.size('?x'+size)
/*.fps(30)
.audioChannels(2)
.audioFrequency(44100)
.audioBitrate('192k')*/
.on('end', () => {
console.log("The file "+path+" has been converted succesfully.");
picturesSizeDb.insert({unlink: fullConvertedPath, path:path});
res.header('Cache-Control', config.cache);
res.sendFile(fullConvertedPath, {root: __dirname});
callback();
})
.on('error', (err) => {
console.log('An error happened: '+err.message);
res.status(500).send(err.message);
fs.exists(fullConvertedPath, (exists) => {
if (exists) {
fs.unlink(fullConvertedPath, () => {});
}
});
callback();
})
.output(fullConvertedPath)
//.output(res, {end: true}) // the streaming doesn't work with mp4
// (it does with flv and .preset('flashvideo') )
.run();
});
})
}
})
}
function fetchDistantFile(u2, res, callback, reserror) {
// Outlook doesn't like the http://
// It's maybe the same for some other softwares
u2 = u2.replace(/^http(s?):\/([^\/])/,'http$1://$2');
filesDb.find({url:u2}, (err, docs) => {
if (docs.length) {
var file = docs[0];
var path = uploadDatapath+'/'+file.hash+'.'+file.extension;
if (res) {
res.sendFile(path, {root: __dirname});
}
if (callback) {
delete file._id;
callback(path, file.hash, file.extension, file);
}
} else {
var u = url.parse(u2);
if (!u.auth && config.auths[u.hostname]) {
u.auth = config.auths[u.hostname];
}
u.headers = {
'User-Agent': config['User-Agent'],
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'fr,en-US;q=0.8,en;q=0.6'
};
(u.protocol === 'https:' ? https : http).get(u, (httpres) => {
var type = httpres.headers['content-type'];
if (res) {
res.status(httpres.statusCode);
res.type(type);
res.header('Cache-Control', config.cache);
httpres.pipe(res);
}
var extension = mime.extension(type);
var temppath = temp.path({suffix: '.'+extension, prefix: 'temp-', dir: uploadDatapath});
var file = fs.createWriteStream(temppath);
var hash = crypto.createHash('sha256');
var size = 0;
httpres.on('data', (data) => {
hash.update(data);
size += data.length;
});
httpres.pipe(file);
httpres.on('end', () => {
hash = hash.digest('hex');
var path = uploadDatapath+'/'+hash+'.'+extension;
fs.exists(path, (exists) => {
if (exists) {
// Just remove the temporary file, we don't need it
fs.unlink(temppath, () => {});
} else {
fs.rename(temppath, path, () => {});
}
});
var name = u2.match(/[^\/]*$/)[0];
if (!name) {
name = "untitled";
}
convertHashToId(hash, extension, (beautifulId) => {
var fileDetails = {
url: u2,
name: name,
id: beautifulId,
size: size,
hash: hash,
extension: extension,
type: type,
mtime: new Date(),
};
filesDb.insert(fileDetails, () => {
if (callback) {
callback(path, hash, extension, fileDetails);
}
});
});
});
}).on('error', (e) => {
if (res) {
res.status(404).send(e.message);
} else if (reserror) {
reserror.status(404).send(e.message);
}
});
}
});
}
// Generate an unique ID, checking existing ids in the database
// Having a collision (a new id that is already used)
// should be very unlikely.
function generateUniqueId(hash, callback, nbIters) {
// We give up after a number of iterations
if (!nbIters || nbIters <= 0) {
callback(hash);
return;
}
// Generate an id using the selected algorithm
var id = generateId(hash);
// Look for collisions
idsDb.findOne({id}, (err, doc) => {
// If we find a collision
if (doc) {
console.log("/!\\ ID generation collision detected.");
generateUniqueId(hash, callback, nbIters-1);
} else {
callback(id);
}
});
}
// Takes a hash as input and returns an id.
// This modifies the database
function convertHashToId(hash, extension, callback) {
idsDb.findOne({hash}, (err, doc) => {
if (doc) {
callback(doc.id);
} else {
generateUniqueId(hash, (id) => {
idsDb.insert({hash, id, extension});
callback(id);
}, 5);
}
});
}
// Takes an id as input, and returns a hash, the extension, and the associated path
function convertIdToHashAndPath(id, callbackSuccess, callbackError) {
idsDb.findOne({id}, (err, doc) => {
if (doc) {
doc.path = doc.hash+"."+doc.extension;
doc.fullPath = uploadDatapath+"/"+doc.path;
callbackSuccess(doc);
} else {
callbackError(err);
}
});
}
app.get(/^\/https?:\/\/?.+$/, (req, res) => {
fetchDistantFile(req.url.slice(1), res);
});
app.get(/^\/fetch\/https?:\/\/?.+$/, (req, res) => {
fetchDistantFile(req.url.slice(7), false, (filepath, hash, extension, fileDetails) => {
fileDetails.status = "ok";
res.send(fileDetails);
}, res);
});
app.get(/^\/thumbnail\/https?:\/\/?.+$/, (req, res) => {
var path = req.url.slice(11);
fetchDistantFile(path, false, (filepath, hash, extension) => {
sendThumbnail(hash+"."+extension, res);
}, res);
});
app.get(/^\/resize\/(deform\/)?(\d+)\/(\d+)\/https?:\/\/?.+$/, (req, res) => {
var path = req.url.match(/^\/resize\/(deform\/)?\d+\/\d+\/(https?:\/\/?.+)$/)[2],
width = parseInt(req.params[1]),
height = parseInt(req.params[2]),
deform = !!req.params[0];
fetchDistantFile(path, false, (filepath, hash, extension) => {
sendResizedImage(hash+"."+extension, width, height, deform, res);
}, res);
});
app.get(/^\/convert\/(mp4|webm)\/(\d+)\/([a-fA-F0-9]{40,64}\.[a-zA-Z0-9]+)$/, (req, res) => {
var path = req.params[2],
format = req.params[0];
size = parseInt(req.params[1]);
sendConvertedVideo(path, format, size, res);
});
app.get(/^\/convert\/(mp4|webm)\/(\d+)\/([a-zA-Z0-9_\-]+)$/, (req, res) => {
var id = req.params[2],
format = req.params[0],
size = parseInt(req.params[1]);
convertIdToHashAndPath(id, (infos) => {
sendConvertedVideo(infos.path, format, size, res);
}, () => {
res.status(404).send("File not found.");
});
});
app.get(/^\/convert\/(mp4|webm)\/(\d+)\/(https?:\/\/?.+)$/, (req, res) => {
var path = req.params[2],
format = req.params[0],
size = parseInt(req.params[1]);
fetchDistantFile(path, false, (filepath, hash, extension) => {
sendConvertedVideo(hash+"."+extension, format, size, res);
}, res);
});
var server = app.listen(config.port, () => {
console.log("Server started on http://localhost:"+config.port+"/");
});