-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREST.js
113 lines (101 loc) · 2.73 KB
/
REST.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
/*
* The REST API for SaveSortMovie
*/
// Fetch dependencies
var express = require("express");
var md5 = require("blueimp-md5");
var getDuration = require("get-video-duration");
var serveStatic = require("serve-static");
var fileUpload = require('express-fileupload');
var multer = require("multer");
// Set up Multer
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, __dirname + '/video_store');
},
filename: function (req, file, cb) {
cb(null, Date.now() + "-" + file.originalname);
}
});
var upload = multer({storage:storage });
// Set up application and model
var app;
var videoModel;
var init = function init(v) {
app = express();
videoModel = v;
// Set up static file serving
app.use(serveStatic(__dirname + '/client'));
app.use(serveStatic(__dirname + '/video_store'));
}
// Mainloop
var watch = function watch() {
app.get("/get_video", upload.array(), function getVideo(req, res) {
// Get video from ID
var rId = req.query.id;
videoModel.find({id: rId},
function getVideoCallback(err, videos) {
if (err) {
console.log(err)
res.sendStatus(503);
}
if (videos.length == 0) res.sendStatus(404);
else res.send(videos[0]);
console.log(videos[0]);
});
});
app.post("/upload_video",
upload.single("vid"),
function addVideo(req, res) {
// Save video to database
var videoData = req.body;
videoData.id = md5(req.file.filename);
videoData.actors = [];
videoData.tags = videoData.tags.split(" ");
videoData.filepath = req.file.filename;
getDuration(__dirname + "/video_store/" + videoData.filepath).then(function (duration){
videoData["length"] = duration;
videoModel
.create(videoData,
function addVideoCallback(err,addedVideo) {
if(err) {
console.log(err);
res.sendStatus(503);
}
console.log(addedVideo);
res.sendStatus(200);
}
);
});
});
app.get("/search_videos",
upload.array(),
function searchVideo(req, res) {
videoModel.find({},
function svCallback(err,vids) {
res.status(200);
for(var i = 0; i != vids.length; i++) {
var id = vids[i].id;
var title = vids[i].title;
var actors = vids[i].actors;
var length = vids[i].length;
vids[i] = {};
vids[i] = {
id: id,
title: title,
actors: actors,
length: length
};
}
console.log(vids);
res.end(JSON.stringify(vids));
}
);
});
app.get("/", upload.array(), function(req, res) {
res.sendFile('client/view.html', {root: __dirname });
});
// Start listening
app.listen(6700);
}
module.exports = {init: init, watch: watch};