This repository has been archived by the owner on Mar 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
120 lines (113 loc) · 3.79 KB
/
app.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
const dotenv = require('dotenv');
const express = require('express');
const app = express();
const api = require('./ApiRequest');
const actions = require('./utils/actions');
const bodyParser = require('body-parser');
const requester = require('request')
const overlay = require('./utils/overlay');
const imageURL = 'https://emojis-as-a-service.herokuapp.com/images/'
dotenv.config({
silent: true
});
var path = require('path');
var formidable = require('formidable');
var fs = require('fs');
const ROOT = "./public_html";
app.use(function (req, res, next) {
console.log(req.method + " request for " + req.url);
next();
});
app.use(bodyParser.json());
function processAPIData(res, data, filename) {
if (data.status_code === undefined) {
res.send(data);
} else if (data.status_code === 4 && data.frames) {
actions.getAllEmojis(data.frames[0], () => {
overlay(data.frames[0], filename, () => {
res.json({
'url': imageURL + filename
});
});
})
} else if (data.status_code) {
setTimeout(() => {
api.get(data.id)
.then((newData) => {
processAPIData(res, newData, filename)
})
.catch((error) => {
res.send(error);
});
}, 500);
} else {
// this is purely a safety net
}
}
app.post('/upload', function (req, res) {
// create an incoming form object
var form = new formidable.IncomingForm();
// specify that we want to allow the user to upload multiple files in a single request
form.multiples = true;
// store all uploads in the /uploads directory
form.uploadDir = path.join(__dirname, '/uploads');
// every time a file has been uploaded successfully,
// rename it to it's orignal name
form.on('file', function (field, file) {
fs.rename(file.path, file.path + path.extname(file.name), function (err) {
if (err) return res.send(err);
file.path = file.path + path.extname(file.name);
file.name = path.basename(file.path);
api.post(imageURL + file.name).then((data) => {
processAPIData(res, data, file.name);
}).catch((error) => {
res.send(error);
});
});
});
// log any errors that occur
form.on('error', function (err) {
console.log('An error has occured: \n' + err);
});
// once all the files have been uploaded, send a response to the client
form.on('end', function () {
// res.end('success');
});
// parse the incoming request containing the form data
form.parse(req);
});
app.post('/slack', function (req, res) {
// console.log(req.body.image_url);
// path.extname(req.body.image_url)
var name = Date.now() + path.extname(req.body.image_url);
name = name.split('?')[0];
requester(req.body.image_url, {encoding: 'binary'}, function(error, response, body) {
if (error) {
console.log(error);
return;
}
fs.writeFile('./uploads/' + name, body, 'binary', function (err) {
if (err) {
console.log(err);
return;
}
api.post(imageURL+ name).then((data) => {
// console.log(name);
processAPIData(res, data, name);
}).catch((error) => {
console.log(error);
res.send(error);
});
});
// name = name.slice(2);
});
});
app.use('/images', express.static('./uploads'));
app.use(express.static(ROOT)); //handle all static requests
app.all("*", function (req, res) {
res.sendStatus(404);
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Express server listening on port ${port}`);
})