-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathshared-utils.ts
More file actions
265 lines (235 loc) · 7.54 KB
/
Copy pathshared-utils.ts
File metadata and controls
265 lines (235 loc) · 7.54 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* Script utilities for publishing content from GitHub issues.
* Parsing logic lives in src/lib/parse-issue.ts (shared with the web app).
* This file adds script-specific functions: image downloading and processing.
*/
import fs from "node:fs";
import path from "node:path";
import https from "node:https";
import sharp from "sharp";
// Re-export all parsing functions from the shared module
export {
parseMetadata,
parseSection,
parseList,
extractImages,
extractImagesBySections,
formatMarkdown,
slugify,
} from "../src/lib/parse-issue";
import {
extractImagesBySections,
formatMarkdown,
type ParsedImage,
} from "../src/lib/parse-issue";
const ALLOWED_HOSTS = [
"github.com",
"user-images.githubusercontent.com",
"raw.githubusercontent.com",
"objects.githubusercontent.com",
"avatars.githubusercontent.com",
"i0.wp.com",
];
const MAX_REDIRECTS = 5;
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB for images
function contentTypeToExt(contentType: string): string {
if (contentType.includes("svg")) return ".svg";
if (contentType.includes("webp")) return ".webp";
if (contentType.includes("gif")) return ".gif";
if (contentType.includes("png")) return ".png";
if (contentType.includes("jpeg") || contentType.includes("jpg")) return ".jpg";
return ".png";
}
/** Convert an SVG file to PNG in-place, returns the new filepath */
async function convertSvgToPng(svgPath: string): Promise<string> {
const pngPath = svgPath.replace(/\.svg$/i, ".png");
await sharp(svgPath, { density: 144 }).png().toFile(pngPath);
fs.unlinkSync(svgPath);
return pngPath;
}
/** Download an image from a URL to a local file. Returns the detected content-type. */
export async function downloadImage(
url: string,
filepath: string,
redirectCount = 0,
): Promise<string> {
if (redirectCount > MAX_REDIRECTS) {
throw new Error(`Too many redirects (>${MAX_REDIRECTS}) for ${url}`);
}
const parsed = new URL(url);
const isAllowed =
ALLOWED_HOSTS.some(
(h) => parsed.hostname === h || parsed.hostname.endsWith(`.${h}`),
) ||
// GitHub stores issue-uploaded images on S3
(parsed.hostname.startsWith("github-production-") &&
parsed.hostname.endsWith(".s3.amazonaws.com"));
if (!isAllowed) {
throw new Error(`Blocked download from untrusted host: ${parsed.hostname}`);
}
return new Promise((resolve, reject) => {
https
.get(url, (response) => {
if (
response.statusCode &&
response.statusCode >= 300 &&
response.statusCode < 400 &&
response.headers.location
) {
return downloadImage(
response.headers.location,
filepath,
redirectCount + 1,
)
.then((ct) => resolve(ct))
.catch(reject);
}
const contentType = response.headers["content-type"] || "";
if (
!contentType.startsWith("image/") &&
!contentType.startsWith("application/octet-stream")
) {
response.resume(); // drain the response
return reject(
new Error(`Unexpected content-type "${contentType}" for ${url}`),
);
}
let downloaded = 0;
const file = fs.createWriteStream(filepath);
response.on("data", (chunk: Buffer) => {
downloaded += chunk.length;
if (downloaded > MAX_FILE_SIZE) {
response.destroy();
file.close();
fs.unlink(filepath, () => {});
reject(
new Error(
`File exceeds ${MAX_FILE_SIZE / 1024 / 1024}MB limit: ${url}`,
),
);
}
});
response.pipe(file);
file.on("finish", () => {
file.close();
resolve(contentType);
});
})
.on("error", (err) => {
fs.unlink(filepath, () => {});
reject(err);
});
});
}
/** Download and process images (banner, logo, and description images) */
export async function processImages(
issueBody: string,
descriptionMarkdown: string,
slug: string,
contentType: string,
) {
const { bannerImages, logoImages, descriptionImages } =
extractImagesBySections(issueBody);
const allImages = [...descriptionImages];
if (
allImages.length === 0 &&
bannerImages.length === 0 &&
logoImages.length === 0
) {
return {
banner: "",
logo: "",
updatedMarkdown: formatMarkdown(descriptionMarkdown),
};
}
const imagesDir = path.join(
process.cwd(),
"public",
"content-images",
contentType,
slug,
);
fs.mkdirSync(imagesDir, { recursive: true });
let updatedMarkdown = formatMarkdown(descriptionMarkdown);
let banner = "";
let logo = "";
const totalImages =
bannerImages.length + logoImages.length + allImages.length;
if (totalImages > 0) {
console.log(`📸 Downloading ${totalImages} images...`);
}
const usedFilenames = new Set<string>();
async function downloadWithFilename(
img: ParsedImage,
dir: string,
publicPrefix: string,
defaultBase: string,
defaultExt: string,
) {
const ext =
img.url.match(/\.(png|jpg|jpeg|gif|svg|webp)/i)?.[0] || defaultExt;
let baseFilename = img.alt
? img.alt.toLowerCase().replace(/[^a-z0-9]+/g, "-")
: defaultBase;
let filename = `${baseFilename}${ext}`;
let counter = 1;
while (usedFilenames.has(filename)) {
filename = `${baseFilename}-${counter}${ext}`;
counter++;
}
usedFilenames.add(filename);
const filepath = path.join(dir, filename);
const publicPath = `${publicPrefix}/${filename}`;
await downloadImage(img.url, filepath);
return { filename, publicPath };
}
// Process banner image — saved as {slug}/banner.png (converted from SVG if needed)
if (bannerImages.length > 0) {
const img = bannerImages[0];
const urlExt = img.url.match(/\.(png|jpg|jpeg|gif|svg|webp)/i)?.[0];
const tempPath = path.join(imagesDir, `banner_tmp`);
const detectedType = await downloadImage(img.url, tempPath);
let ext = urlExt || contentTypeToExt(detectedType);
let finalPath = path.join(imagesDir, `banner${ext}`);
fs.renameSync(tempPath, finalPath);
if (ext === ".svg") {
finalPath = await convertSvgToPng(finalPath);
ext = ".png";
}
banner = `/content-images/${contentType}/${slug}/banner${ext}`;
console.log(` ✓ banner${ext} (banner)`);
}
// Process logo image — saved as {slug}/logo.png (converted from SVG if needed)
if (logoImages.length > 0) {
const img = logoImages[0];
const urlExt = img.url.match(/\.(png|jpg|jpeg|gif|svg|webp)/i)?.[0];
const tempPath = path.join(imagesDir, `logo_tmp`);
const detectedType = await downloadImage(img.url, tempPath);
let ext = urlExt || contentTypeToExt(detectedType);
let finalPath = path.join(imagesDir, `logo${ext}`);
fs.renameSync(tempPath, finalPath);
if (ext === ".svg") {
finalPath = await convertSvgToPng(finalPath);
ext = ".png";
}
logo = `/content-images/${contentType}/${slug}/logo${ext}`;
console.log(` ✓ logo${ext} (logo)`);
}
// Process description images
for (let i = 0; i < allImages.length; i++) {
const img = allImages[i];
const { filename, publicPath } = await downloadWithFilename(
img,
imagesDir,
`/content-images/${contentType}/${slug}`,
`image-${i + 1}`,
".png",
);
console.log(` ✓ ${filename}`);
updatedMarkdown = updatedMarkdown.replace(
img.markdown,
``,
);
}
return { banner, logo, updatedMarkdown };
}