-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
173 lines (152 loc) · 4.57 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const sharp = require("sharp");
const fs = require("fs").promises;
const path = require("path");
const folderPathInput = "./input";
const folderPathOutput = "./output";
async function getFileNames() {
try {
const files = await fs.readdir(folderPathInput);
const fileStats = await Promise.allSettled(
files.map(async (file) => {
const stats = await fs.lstat(`${folderPathInput}/${file}`);
return stats.isFile();
})
);
return files.filter(
(file, index) =>
fileStats[index].status === "fulfilled" && fileStats[index].value
);
} catch (err) {
console.error("Error reading directory:", err);
return [];
}
}
async function getFilesizeInBytes(filename) {
try {
var stats = await fs.stat(filename);
} catch (err) {
console.error("Error reading file:", err);
return;
}
return stats.size;
}
const BYTE_SIZE = {
B: 1,
KB: 1000,
MB: 1000000,
GB: 1000000000,
};
function getKeyByValue(object, value) {
return Object.keys(object).find((key) => object[key] === value);
}
function getFileSize(fileSize, byteSize) {
const key = getKeyByValue(BYTE_SIZE, byteSize);
if (!fileSize) return "unknown";
if (!key) return "unknown";
return `${(fileSize / byteSize).toFixed(2)} ${key}`;
}
async function processFile(image, options) {
try {
const processed = image.resize(options).webp(options);
return processed;
} catch (err) {
console.error("Error reading file:", err);
}
}
async function run({
logging = true,
performanceInfo = false,
byteSize = BYTE_SIZE.MB,
quality = 75,
}) {
try {
let start, end;
if (performanceInfo) {
start = performance.now();
}
const fileNames = await getFileNames();
const amount = fileNames.length;
console.log(`Processing ${amount} files...`);
const fileStatsPromises = fileNames.map(async (fileName) => {
let fileSize = undefined;
if (logging) {
try {
fileSize = await getFilesizeInBytes(`${folderPathInput}/${fileName}`);
} catch (err) {
console.error("Error reading file:", err);
}
}
if (logging) {
console.log("--------------------------------------------------");
console.log(`Converting file: ${fileName}`);
console.log(`[INPUT] file size: ${getFileSize(fileSize, byteSize)}`);
}
const inputImagePath = `${folderPathInput}/${fileName}`;
const fileFormatOutput = ".webp";
const fileNameOutput = fileName.split(".")[0];
const outputImagePath = path.join(
folderPathOutput,
fileNameOutput + "-compressed" + fileFormatOutput
);
// Define the compression and resizing options
// 0 - 6 (effort, higher is slower but better)
const options = {
quality: quality,
width: 1920,
height: 1080,
fit: "inside",
withoutEnlargement: true,
effort: 6,
fastShrinkOnLoad: true,
};
const image = sharp(inputImagePath);
try {
const processed = await processFile(image, options);
try {
const info = await processed.toFile(outputImagePath);
if (logging) {
if (fileSize) {
let sign = '+';
if (fileSize - info.size > 0) {
sign = '-'
}
console.log(
`[OUTPUT] file size: ${getFileSize(
info.size,
byteSize
)} [${sign}${getFileSize(fileSize - info.size, byteSize)}] [${sign}${(
((fileSize - info.size) / fileSize) *
100
).toFixed(2)}%]`
);
}
console.log(
`[OUTPUT] file info: [${info.format}] ${info.width}x${info.height}`
);
}
} catch (err) {
console.error("Error writing file:", err);
}
} catch (err) {
console.error("Error processing file:", err);
}
});
await Promise.allSettled(fileStatsPromises);
if (performanceInfo) {
end = performance.now();
console.log(`Time elapsed: ${(end - start).toFixed(2)} ms`);
console.log(
`Avg time per file: ${((end - start) / amount).toFixed(2)} ms`
);
}
} catch (err) {
console.error("Error reading directory:", err);
}
}
const args = {
logging: true,
performanceInfo: true,
byteSize: BYTE_SIZE.KB,
quality: 75,
};
run(args);