repl.it
@rshetty/

BeatKeeper

Nodejs

No description

fork
loading
Files
  • index.js
  • public
  • package-lock.json
  • package.json
  • post.md
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
var express = require('express');
var app = express();
var server = require('http').Server(app);
var formidable = require('formidable');
var fs = require('fs');
var path = require('path');
var rimraf = require('rimraf');
var cors = require('cors');
var ytdl = require('ytdl-core');


app.use(cors());
app.use(express.urlencoded( {extended: true} ))
// Use correct file directory
app.use('/', express.static('/home/runner/public'));

app.use('/process', function(req, res, next) {
	var uploadsDir = '/home/runner/public/process/uploads';
	fs.readdir(uploadsDir, function(err, files) {
		files.forEach(function(file, index) {
			fs.stat(path.join(uploadsDir, file), function(err, stat) {
				var endTime, now;
				if (err) {
					return console.error(err);
				}
				now = new Date().getTime();
				endTime = new Date(stat.ctime).getTime() + 3600000;
				if (now > endTime) {
					return rimraf(path.join(uploadsDir, file), function(err) {
						if (err) {
							return console.error(err);
						}
						console.log('successfully deleted');
					});
				}
			});
		});
	});
	next();
});

app.get('/', function(req, res) {

	res.sendFile('/home/runner/public/index.html');

}).listen(9000, function() {
	console.log(`Listening`);
});

app.post('/fileupload', function(req, res) {

	var form = new formidable.IncomingForm();
	form.parse(req, function(err, fields, files) {
		var oldpath = files.filetoupload.path;
		newpath = '/home/runner/public/process/uploads/' + files.filetoupload.name;
		fs.rename(oldpath, newpath, function(err) {
			if (err) throw err;
			res.writeHead(200, {"Content-Type": "text/html"});
			res.write(`<p>Successfully uploaded your file! Redirecting...</p>`);
			var reqUrl = '"/process?filepath=" + "' + files.filetoupload.name + '"';
			var writeScript = `<script>
  									setTimeout(function () {
     									window.location = ` + reqUrl + `;
  									}, 2000)
									</script>`;
			res.write(writeScript);
			res.end();
		});
	});
});

app.get('/process', function(req, res) {
	res.sendFile('/home/runner/public/process/index.html');
});

app.post('/demo', function(req, res) {
	res.redirect('/process/?filepath=Queen-Another-One-Bites-The-Dust.mp3')
});

app.use('/download', (req,res, next) => {
	var url, ID;
		url = req.body.youtubepath;
  ID = '';
  url = url.replace(/(>|<)/gi,'').split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);
  if(url[2] !== undefined) {
    ID = url[2].split(/[^0-9a-z_\-]/i);
    ID = ID[0];
  }
  else {
    ID = url;
  }
	var ytFile;
	tags = ytdl.getBasicInfo(ID, function(err, tags) {
		ytFile = tags.media;
		res.header(
			{
				'Content-Type': 'audio/mpeg',
				'Content-Disposition': 'attachment; filename="' + ytFile.artist + ' - ' + ytFile.song + '.mp3"'
			}
		);
	});
	
	aud = ytdl(ID, {
			quality: "highest",
			format: 'mp3'
			})
	console.log();
	aud.pipe(res);
	next();
});

app.post("/download", function(req, res) {
	res.set({
  'Content-Type': 'text/plain',
  'Location': '/'
});
});
node v10.16.0