-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
120 lines (106 loc) · 3.74 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
const cp = require('child_process');
const fs = require('fs');
const http = require('http');
const request = require('request');
const stream = require('stream');
const { App } = require('@slack/bolt');
const { WebClient } = require('@slack/web-api');
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET
});
const clientRead = new WebClient(process.env.SLACK_OAUTH_TOKEN);
const client = new WebClient(process.env.SLACK_BOT_TOKEN);
const cloudconvert = new (require('cloudconvert'))(process.env.CLOUD_CONVERT_KEY);
const documentExts = [
"doc", "docm", "docx", "md", "odt",
"pages", "rtf", "tex",
"eps", "pps", "ppt", "pptm", "pptx",
"key", "csv", "numbers",
"xls", "xlsm", "xlsx",
];
app.event('app_mention', async({event, context}) => {
console.log("Event app_mention");
// console.log(event);
// console.log(context);
processMessage(event.files, event.channel, event.ts, event.user);
if (!event.files && event.thread_ts) {
console.log("Retriving a message");
const res = await clientRead.channels.history({
channel: event.channel,
count: 1,
latest: event.thread_ts,
inclusive: true
});
// console.log(res);
processMessage(res.messages[0].files, event.channel, event.thread_ts, event.user);
};
});
(async () => {
await app.start(process.env.PORT || 3000);
})();
const processMessage = async (files, channel, ts, user) => {
if (files) client.chat.postEphemeral({
user,
channel,
text: "PDF化・圧縮中",
});
for (var i = 0; files && i <files.length; ++i) {
const url = files[i].url_private_download;
const ext = url.split('.').pop();
if (ext == "pdf") {
recievePDF(url, channel, ts);
} else {
recieveDocument(url, channel, ts);
}
}
};
const recievePDF = async (url, channel, ts) => {
compressed = compressPDF(request.get({
url,
headers: {Authorization: "Bearer " + process.env.SLACK_BOT_TOKEN}
}));
await upload(compressed, channel, ts, "compressed-" + url.split('/').pop());
};
const recieveDocument = async (url, channel, ts) => {
const ext = url.split(".").pop();
console.log("Convert " + ext + " to pdf");
converted = request.get({
url,
headers: {Authorization: "Bearer " + process.env.SLACK_BOT_TOKEN}
}).pipe(new stream.PassThrough)
.pipe(cloudconvert.convert({
inputformat: ext,
outputformat: "pdf"
})).pipe(new stream.PassThrough());
await upload(converted, channel, ts, url.split("/").pop() + ".pdf");
};
const upload = async (input, channel, ts, filename) => {
await client.files.upload({
filename,
channels: channel,
thread_ts: ts,
file: input,
});
console.log('File uploaded');
};
const compressPDF = (input) => {
const ws = new stream.PassThrough();
const filePrefix = '/tmp/slack-pdf-' + Math.random().toString(16).substr(2, 10);
const inputFile = filePrefix + '.pdf';
const outputFile = filePrefix + '.min.pdf';
const inputFileStream = fs.createWriteStream(inputFile);
input.on('end', () => {
inputFileStream.close();
console.log("Compressing PDF");
cp.exec('gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.6 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile="' + outputFile + '" "' + inputFile + '"', () => {
console.log("Write compressed PDF");
fs.createReadStream(outputFile).pipe(ws);
});
}).pipe(inputFileStream);
return ws.on('end', () => {
fs.unlink(inputFile, () => {});
fs.unlink(outputFile, () => {});
});
};
module.exports = {compressPDF};