This repository has been archived by the owner on Dec 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
120 lines (111 loc) · 3.05 KB
/
util.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 fs = require("fs");
const { execSync } = require("child_process");
const path = require("path");
/*
ファイルのダウンロード判定
https://www.sambaiz.net/article/131/ を参考にした
*/
async function sleep(time) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, time);
});
}
async function waitDownloadComplete(
path,
waitTimeSpan = 1000,
timeout = 60 * 1000
) {
return new Promise(async (resolve, reject) => {
let totalWaitTime = 0;
while (true) {
try {
const completed = await isDownloadComplete(path);
if (completed) {
resolve();
return;
} else {
if (totalWaitTime >= timeout) {
reject("timeout");
return;
}
totalWaitTime += waitTimeSpan;
}
await sleep(waitTimeSpan);
} catch (e) {
reject(e);
return;
}
}
});
}
async function isDownloadComplete(path) {
return new Promise((resolve, reject) => {
fs.readdir(path, (e, files) => {
if (e) {
reject(e);
} else {
if (files.length === 0) {
resolve(false);
return;
}
for (let file of files) {
if (/.*\.crdownload$/.test(file)) {
resolve(false);
return;
}
}
resolve(true);
}
});
});
}
function searchFiles(targetPath, ext) {
return new Promise((resolve, reject) => {
fs.readdir(targetPath, (err, files) => {
if (err) throw err;
const matcher = new RegExp(`^[^~].+\.${ext}$`);
files = files.filter((file) => matcher.exec(file) != null);
resolve(files);
});
});
}
async function unarchive(targetPath) {
let zip = await searchFiles(targetPath, "zip");
if (zip.length > 0) {
execSync(`unzip -o ${path.join(targetPath, zip[0])} -d ${targetPath}`);
return true;
} else {
return false;
}
}
function cleanUp(targetPath) {
if (targetPath && fs.existsSync(targetPath)) {
fs.readdir(targetPath, (err, files) => {
if (err) throw err;
files.forEach((file) => {
fs.unlinkSync(path.join(targetPath, file));
});
fs.rmdirSync(targetPath);
});
}
}
async function saveScreenShot(page = _page) {
// S3に保存
const jpgBuf = await page.screenshot({ fullPage: true, type: "jpeg" });
const s3 = new AWS.S3();
const now = new Date();
now.setHours(now.getHours() + 9);
const nowStr = "" + now.getFullYear() + "-" + (now.getMonth() + 1 + "").padStart(2, "0") + "-" + (now.getDate() + "").padStart(2, "0") + " " + (now.getHours() + "").padStart(2, "0") + ":" + (now.getMinutes() + "").padStart(2, "0") + ":" + (now.getSeconds() + "").padStart(2, "0");
const fileName = nowStr.replace(/[\-:]/g, "_").replace(/\s/g, "__");
let s3Param = {
Bucket: "rpaka-screenshots",
Key: null,
Body: null,
};
s3Param.Key = fileName + ".jpg";
s3Param.Body = jpgBuf;
await s3.putObject(s3Param).promise();
}
module.exports = { waitDownloadComplete, searchFiles, unarchive, cleanUp, saveScreenShot };