-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
136 lines (129 loc) · 5.05 KB
/
app.js
File metadata and controls
136 lines (129 loc) · 5.05 KB
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
/**
* @api {get} /api/convert Convert a file
* @apiName Convert
* @apiGroup Convert
*
* @apiParam {String} from Format to convert from.
* @apiParam {String} to Format to convert to.
* @apiParam {String} url URL for the file to convert.
*
* @apiSuccess {String} url URL for the file to convert.
* @apiSuccess {String} from Format to convert from.
* @apiSuccess {String} to Format to convert to.
* @apiSuccess {String} output The output of the conversion.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "url": "http://example.com/my.pdf",
* "from": "pdf",
* "to": "html",
* "output": "<!DOCTYPE html><html><head></head><body><h1>Example PDF</h1></body></html>"
* }
*
* @apiError InvalidURL Please specify a valid URL
* @apiError InvalidFromFormat Please specify a format to convert from
* @apiError InvalidToFormat Please specify a format to convert to
* @apiError InternalServerError Oops, we couldn't convert that PDF, please try again
* @apiError UnsupportedFromFormat Sorry, we don't currently support `from`
* @apiError UnsupportedToFormat Sorry, we don't currently support `to`
*
* @apiErrorExample Error-Response:
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "error": "InvalidFromFormat",
* "message": "Please specify a format to convert from"
* }
*/
var express = require("express");
var pdf = require("pdftohtmljs");
var request = require("request");
var fs = require("fs");
var sha1 = require("sha1");
var app = express();
app.use('/', express.static(__dirname + '/static'));
app.use('/docs', express.static(__dirname + '/static/docs'));
app.get("/api/*", function(req, res) {
console.log("Got request for /api to convert " + req.query.url + " from " + req.query.from + " to " + req.query.to);
// req.query contains URL parameters
if (!req.query.url) {
res.status(400).json({
code: 400,
error: "InvalidURL",
message: "Please specify a valid URL"
});
}
else if (!req.query.from) {
res.status(400).json({
code: 400,
error: "InvalidFromFormat",
message: "Please specify a format to convert from"
});
}
else if (!req.query.to) {
res.status(400).json({
code: 400,
error: "InvalidToFormat",
message: "Please specify a format to convert to"
});
} else {
if (req.query.from.toLowerCase() == "pdf") {
if (req.query.to.toLowerCase() == "html") {
var id = sha1(req.query.url);
try {
stats = fs.lstatSync('files/' + id + ".html");
fs.readFile("files/" + id + ".html", "utf-8", function(err, data) {
res.status(200).json({
url: req.query.url,
html: data,
from: req.query.from.toLowerCase(),
to: req.query.to.toLowerCase()
});
});
} catch (e) {
var getPDF = request(req.query.url).pipe(fs.createWriteStream("files/" + id + ".pdf"));
getPDF.on("finish", function() {
var converter = new pdf("files/" + id + ".pdf", "files/" + id + ".html");
converter.success(function() {
fs.readFile("files/" + id + ".html", "utf-8", function(err, data) {
res.status(200).json({
url: req.query.url,
html: data,
from: req.query.from.toLowerCase(),
to: req.query.to.toLowerCase()
});
});
});
converter.error(function(err) {
res.status(500).json({
code: 500,
error: "InternalServerError",
message: "Oops, we couldn't convert that PDF, please try again"
});
console.log("Error with PDF converter: " + err);
});
converter.convert();
});
}
} else {
res.status(400).json({
code: 400,
error: "UnsupportedToFormat",
message: "Sorry, we don't currently support " + req.query.to
});
}
} else {
res.status(400).json({
code: 400,
error: "UnsupportedFromFormat",
message: "Sorry, we don't currently support " + req.query.from
});
}
}
});
var server = app.listen(3002, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Morpheus listening at http://%s:%s', host, port);
});