-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
73 lines (62 loc) · 1.87 KB
/
server.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
const express = require("express");
const basicAuth = require("express-basic-auth");
const fileUpload = require("express-fileupload");
const tempy = require("tempy");
const { exec } = require("child_process");
const app = express();
app.use(fileUpload());
const password = process.argv[2];
if (!password) {
console.warn('You must specify a password used for basic auth. For example node server.js your-password-here');
process.exit();
}
app.use(
basicAuth({
users: { ios: password },
unauthorizedResponse: getUnauthorizedResponse
})
);
app.post("/upload", function(req, res) {
if (!req.files) return res.status(400).send("No files were uploaded.");
const dataFile = req.files.dataFile;
tmpFile = tempy.file();
dataFile.mv(tmpFile, function(err) {
if (err) return res.status(500).send(err);
res.send();
});
exec(
`xclip -selection clipboard -i ${tmpFile} -t ${dataFile.mimetype}`,
(err, stdout, stderr) => {
if (err) {
console.log(err);
return;
}
}
);
});
app.get("/get-clipboard", (req, res) => {
exec(`xclip -selection clipboard -o -t TARGETS`, (err, stdout, stderr) => {
const imageTarget = /image\/.*/g.exec(stdout);
if (imageTarget) {
imageContentType = imageTarget[0];
tmpFile = tempy.file();
exec(
`xclip -selection clipboard -t ${imageContentType} -o > ${tmpFile}`,
(err, stdout, stderr) => {
res.contentType(imageContentType);
res.sendFile(tmpFile);
}
);
} else {
exec(`xclip -selection clipboard -o -t TEXT`, (err, stdout, stderr) => {
res.send(stdout);
});
}
});
});
function getUnauthorizedResponse(req) {
return req.auth
? "Credentials " + req.auth.user + ":" + req.auth.password + " rejected"
: "No credentials provided";
}
app.listen(8001, () => console.log(`Server is running on port 8001...`));