-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch-external-images.ts
229 lines (217 loc) · 7.04 KB
/
fetch-external-images.ts
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import fs from 'fs';
import { readFile } from 'fs/promises';
import path from 'path';
import axios from 'axios';
import sharp from 'sharp';
import lodash from 'lodash';
import { getStamps } from 'git-date-extractor';
import getFiles from './utils/get-files';
import normalizeData from './utils/normalize-data';
function componentToHex(c: number) {
const hex = c.toString(16);
return hex.length === 1 ? '0' + hex : hex;
}
type RGBToHexOptions = { r: number; g: number; b: number };
function rgbToHex({ r, g, b }: RGBToHexOptions) {
return '#' + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function renameKeys(content: Record<string, unknown>) {
return normalizeData({
obj: content,
keysMap: { href: 'url', heading: 'name', subtitle: 'subheading' },
});
}
const downloadImage = (url: string, imagePath: string) =>
axios({
url,
responseType: 'stream',
}).then(
(response) =>
new Promise((resolve, reject) => {
response.data
.pipe(fs.createWriteStream(imagePath))
.on('finish', () => resolve(imagePath))
.on('error', (e: Error) => reject(e));
}),
);
function checkFileExists(file: string) {
return fs.promises
.access(file, fs.constants.F_OK)
.then(() => true)
.catch(() => false);
}
type ResizeOptions = { input: string; output: string };
async function resize({ input, output }: ResizeOptions) {
try {
const buffer = await sharp(input)
.toFormat('png')
.resize({
width: 4686,
height: 2636,
fit: sharp.fit.contain,
background: { r: 255, g: 255, b: 255, alpha: 0 },
})
.toBuffer();
await sharp(buffer).toFile(output ?? input);
} catch (e) {
console.error(e);
}
}
type UpdateContentOptions = {
folder: string;
imageFolder: string;
imageKey?: string;
rename?: boolean;
};
async function updateContent({
folder,
imageFolder,
imageKey = 'image',
rename = true,
}: UpdateContentOptions) {
// get list of urls to crawl from content files
for await (const filename of getFiles(folder)) {
const data = fs.readFileSync(filename);
const content = JSON.parse(data.toString());
const slug = path.parse(filename).name;
console.log(slug);
if (content.offers) {
for (const categoryKey in content.offers) {
const category = content.offers[categoryKey];
console.log(' - ', categoryKey);
for (const offerKey in category) {
const offer = category[offerKey];
console.log(' - ', offerKey);
const imageUrl = offer.image;
const offerFolder = `${imageFolder}/offers/${slug}/`.replace(
/\/\//,
'/',
);
const imagePath = `${offerFolder}${offerKey}.png`;
const folderExists = await checkFileExists(offerFolder);
if (!folderExists) {
fs.mkdirSync(offerFolder);
}
const fileExists = await checkFileExists(imagePath);
if (imageUrl && !fileExists) {
if (imageUrl.startsWith('http')) {
await downloadImage(imageUrl, imagePath);
} else {
await resize({ input: `public/${imageUrl}`, output: imagePath });
}
const { dominant } = await sharp(imagePath).stats();
offer.color = rgbToHex(dominant);
}
}
}
}
const imageUrl = (lodash.get(content, imageKey) ?? '')
.replace(/^\/\//, 'https://')
.replace(/^https:\/\/daim.dev\//, '');
const bannerUrl = lodash.get(content, 'banner') ?? '';
const imagePath = `${imageFolder}${slug}.png`;
const bannerPath = `${imageFolder}/banners/${slug}.png`;
const images = [
{ imageUrl, imagePath, type: 'main' },
{ imageUrl: bannerUrl, imagePath: bannerPath, type: 'banner' },
];
for (const image of images) {
const fileExists = await checkFileExists(image.imagePath);
if (image.imageUrl && !fileExists) {
if (image.imageUrl.startsWith('http')) {
await downloadImage(image.imageUrl, image.imagePath);
await resize({ input: image.imagePath, output: image.imagePath });
} else {
const input = `public/${image.imageUrl}`.replace('//', '/');
await resize({ input, output: image.imagePath });
}
if (image.type === 'main' && !content.color) {
const { dominant } = await sharp(image.imagePath).stats();
content.color = rgbToHex(dominant);
}
}
}
const timestamps = JSON.parse(
(await readFile('timestamps.json')).toString(),
);
const contentFilePath = `${folder}${slug}.json`;
// Remove last full stop from subheading
content.subheading = content.subheading?.replace(/\.$/, '');
content.createdAt = timestamps[contentFilePath].created;
// content.updatedAt = timestamps[contentFilePath].modified;
content.updatedAt = undefined;
if (rename) {
const renamed = renameKeys(content);
const stringContent = JSON.stringify(renamed, null, 2) + '\n';
fs.writeFileSync(filename, stringContent);
}
}
}
(async () => {
await getStamps({ onlyIn: 'content', outputToFile: true });
const contents = [
{
slug: 'tools',
folder: 'content/tools/',
imageFolder: 'public/img/tools/',
},
{
slug: 'alternatives',
folder: 'content/alternatives/',
imageFolder: 'public/img/alternatives/',
},
{
slug: 'team',
folder: 'content/team/',
imageFolder: 'public/img/team/',
imageKey: 'basics.picture',
rename: false,
},
{
slug: 'services',
folder: 'content/services/',
imageFolder: 'public/img/services/',
},
{
slug: 'portfolio',
folder: 'content/projects/',
imageFolder: 'public/img/portfolio/',
},
];
for (const content of contents) {
console.log(content.slug);
await updateContent(content);
}
const images = [
'img/blog/best-front-end-framework.jpg',
'img/blog/docker.jpg',
'img/blog/git-workflow.png',
'img/blog/nuxt.png',
'img/blog/paperplane.jpg',
'img/blog/windows.jpg',
'img/blog.jpg',
'img/portfolio/scuber.jpg',
'https://begriffs.com/images/reorder-list.png',
'https://wearepixel.com.au/static/93dc2daa73fab468fc19e7d053047220/47498/selecting_a_digital_agency.jpg',
'https://tailwind.build/tailwind/static/img/components.png?v=1',
// 'https://harlanzw.com/__og_image__/og.png',
];
// get list of urls to crawl from content files
for await (const image of images) {
const slug = path.parse(image).name;
console.log(slug);
const imagePath = `public/img/blog/${slug}.png`;
const fileExists = await checkFileExists(imagePath);
if (!fileExists) {
if (image.startsWith('http')) {
await downloadImage(image, imagePath);
await resize({ input: imagePath, output: imagePath });
} else {
await resize({ input: `public/${image}`, output: imagePath });
}
}
// const { dominant } = await sharp(imagePath).stats();
// const hex = rgbToHex(dominant);
// console.log(slug, hex);
}
})();