-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (138 loc) · 4.68 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
const express = require("express");
const ffmpeg = require("fluent-ffmpeg");
const bodyParser = require("body-parser");
const fs = require("fs");
const fileUpload = require("express-fileupload");
const RSS = require("rss-generator");
const Parser = require("rss-parser");
const randomstring = require("randomstring");
const path = require('path');
const rimraf = require("rimraf");
const app = express();
const PORT = 8080;
const home = `${__dirname}/public`;
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}));
// parse application/json
app.use(bodyParser.json());
//support parsing of application/x-www-form-urlencoded post data
app.use(
fileUpload({
useTempFiles: true,
tempFileDir: "/tmp/",
})
);
app.use("/", express.static(__dirname + "/public"));
app.set("view engine", "pug");
ffmpeg.setFfmpegPath("/usr/bin/ffmpeg");
ffmpeg.setFfprobePath("/usr/bin/ffprobe");
ffmpeg.setFlvtoolPath("/usr/bin/ffplay");
function regenerateFeed(req, feeddata) {
const feed = new RSS(feeddata.meta);
for (let i = 0; i < feeddata.items.length; i++) {
feed.item(feeddata.items[i]);
}
const xml = feed.xml();
fs.writeFileSync(`${home}/${req.query.id}/rss.xml`, xml);
}
function addItem(fileName, req) {
let folderName = `${home}/${req.query.id}`;
let feeddata = JSON.parse(fs.readFileSync(folderName + "/feed.json").toString());
feeddata.items.push({
title: `${req.body.Title}`,
description: `${req.body.Title}`,
author: `${req.body.Title}`,
enclosure: {url: `${req.protocol}://${req.hostname}:${PORT}/${req.query.id}/${fileName}`}
});
fs.writeFileSync(folderName + "/feed.json", JSON.stringify(feeddata));
regenerateFeed(req, feeddata);
}
function remove (file) {
fs.unlink(file, function (err) {
if (err) throw err;
});
}
function move (file, to){
file.mv(to, function (err) {
if (err) return res.sendStatus(500)
console.log("File Uploaded successfully");
});
}
function convertToMP3(file, req) {
// replace file extension with mp3
let fileName = `${file.name.split(".").slice(0, -1).join(".")}.mp3`;
// save video file to tmp directory
move(file, "tmp/" + file.name)
// convert to mp3
ffmpeg("tmp/" + file.name)
.noVideo()
.audioCodec("libmp3lame")
.saveToFile(`${home}/${req.query.id}/${fileName}`)
.on("end", function (stdout, stderr) {
console.log("Finished");
addItem(fileName, req);
// remove video file
remove("tmp/" + file.name);
})
.on("error", function (err) {
console.log("an error happened: " + err.message);
// remove video file
remove("tmp/" + file.name);
});
}
// generate homepage from view
app.get("/", function (req, res) {
if (req.query.id == null) {
var rndFolder = randomstring.generate({
length: 10,
charset: "alphanumeric"
});
return res.redirect("/?id="+rndFolder);
}
res.render("index", {
id: req.query.id,
rsspath: `${req.protocol}://${req.hostname}:${PORT}/${req.query.id}/rss.xml`
});
});
app.post("/create", (req, res) => {
//TODO Hier string generieren und redirect auf zweite HTML-Seite (add,remove)
const folderName = `${home}/${req.query.id}`;
if (!fs.existsSync(folderName)) {
fs.mkdirSync(folderName);
console.log(req.query.id + "created");
let feeddata = {
meta: {
title: `${req.body.Title}`,
description: `${req.body.Title}`,
docs: `${req.body.Title}`,
feed_url: `${req.protocol}://${req.hostname}:${PORT}/${req.query.id}/rss.xml`,
site_url: `${req.protocol}://${req.hostname}:${PORT}/${req.query.id}/`
},
items: [ ]
};
fs.writeFileSync(folderName + "/feed.json", JSON.stringify(feeddata));
regenerateFeed(req, feeddata);
}
return res.redirect("/?id=" + req.query.id);
});
app.post("/addItem", (req, res) => {
let file = req.files.file;
let extn = path.extname(file.name);
if (extn == ""){
return res.sendStatus(500);
}
convertToMP3(file, req);
return res.redirect("/?id=" + req.query.id);
});
app.post("/removePlaylist", (req, res) => {
rimraf(`${home}/${req.query.id}`, (err) => {
if(err) return res.sendStatus(500);
console.log(req.query.id + "deleted");
})
return res.redirect("/");
});
app.post("/removeItem", (req, res) => {
remove(`${home}/${req.query.id}/${req.body.Item}`);
return res.redirect("/?id=" + req.query.id);
});
app.listen(PORT);