-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-scaling.js
172 lines (149 loc) · 4.82 KB
/
image-scaling.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
const sharp = require("sharp");
const FormData = require("form-data");
const fs = require("node:fs/promises");
const path = require("node:path");
const { constants, createReadStream } = require("node:fs");
async function benchmark() {
const examplesPath = path.join(__dirname, "image-examples");
let exampleImages;
if (await fs.stat(examplesPath, constants.R_OK).catch(() => false)) {
exampleImages = await getExamples(examplesPath);
} else {
exampleImages = await generateExamples(examplesPath);
}
console.log("Uploading assets to storyblok.");
const uploadPromises = [];
for (const image of exampleImages) {
uploadPromises.push(uploadStoryblokImage(image));
await new Promise((r) => setTimeout(r, 1000));
}
console.log("Finished storyblok upload.");
const storyblokImages = await Promise.all(uploadPromises);
for (const image of storyblokImages) {
for (const width of testWidths) {
const start = Date.now();
const res = await fetch(
image.url + `/${width}x0/filters:format(webp):quality(75)`,
);
await res.arrayBuffer();
console.log(Date.now() - start, image);
}
}
}
async function generateExamples(examplesPath) {
console.log("Generating examples.");
await fs.mkdir(examplesPath);
const imagesPath = path.join(__dirname, "images");
const exampleImages = [];
for (const fileName of await fs.readdir(imagesPath)) {
console.log(" " + fileName);
const filePath = path.join(imagesPath, fileName);
const baseName = path.parse(fileName).name;
for (const width of sourceImageWidths) {
async function resize(format, opts) {
const prePath = path.join(
examplesPath,
`${baseName}-${width}.${format}`,
);
let image = sharp(filePath).resize(width);
image = image[format](opts);
image = await image.toFile(prePath);
const genPath = path.join(
examplesPath,
`${baseName}-${width}x${image.height}.${format}`,
);
await fs.rename(prePath, genPath);
exampleImages.push({
width,
height: image.height,
format,
path: genPath,
});
}
await Promise.all([
resize("webp"),
resize("png", { quality: 80 }),
resize("jpeg"),
]);
}
}
console.log("Finished generating examples.");
return exampleImages;
}
async function getExamples(examplesPath) {
const fileNames = await fs.readdir(examplesPath);
return fileNames.map((n) => {
const sizeString = n.slice(n.lastIndexOf("-") + 1, n.lastIndexOf("."));
const [width, height] = sizeString.split("x");
return {
width: Number.parseInt(width),
height: Number.parseInt(height),
format: path.parse(n).ext.slice(1),
path: path.join(examplesPath, n),
};
});
}
async function uploadStoryblokImage(image) {
const headers = {
["content-type"]: "application/json",
authorization: sbAuthToken,
};
const getUploadUrlResponse = await fetch(
`https://mapi.storyblok.com/v1/spaces/${spaceId}/assets/`,
{
method: "POST",
headers,
credentials: "include",
body: JSON.stringify({
filename: path.basename(image.path),
size: image.width + "x" + image.height,
}),
},
);
const form = new FormData();
const uploadUrlResponse = await getUploadUrlResponse.json();
// apply all fields from the signed response object to the second request
for (let key in uploadUrlResponse.fields) {
form.append(key, uploadUrlResponse.fields[key]);
}
// also append the file read stream
form.append("file", createReadStream(image.path));
// submit your form
const { id } = await new Promise((resolve, reject) =>
form.submit(uploadUrlResponse.post_url, async (error) => {
if (error) {
reject(error);
}
const finaliseResponse = await fetch(
`https://mapi.storyblok.com/v1/spaces/${spaceId}/assets/${uploadUrlResponse.id}/finish_upload`,
{ headers },
);
if (!finaliseResponse.ok) {
console.error(finaliseResponse.status, await finaliseResponse.text());
reject(new Error("Upload failed."));
} else {
resolve(await finaliseResponse.json());
}
}),
);
const getAssetResponse = await fetch(
`https://mapi.storyblok.com/v1/spaces/${spaceId}/assets/${id}`,
{ headers },
);
const asset = await getAssetResponse.json();
return { url: asset.filename.replace("s3.amazonaws.com/", ""), ...image };
}
const providers = [];
const sourceImageWidths = [1080, 2048, 3840];
const testWidths = [640, 750, 828, 1080, 1200, 1560];
const spaceId = 247220;
const sbAuthToken = "HVr29ALQgmvg33sKcrT3kQtt-209491-1PKVJoSWzRZesG_QGHZ2";
benchmark().then(
() => {
console.log("Finished benchmarking.");
},
(error) => {
console.error(error);
process.exit(1);
},
);