@openpeepshq/core 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/media/index.js +77 -20
  2. package/package.json +3 -3
@@ -215,26 +215,82 @@ const escapeXml = (value) => value
215
215
  .replace(/>/g, '>')
216
216
  .replace(/"/g, '"');
217
217
  /**
218
- * Render the first page of a PDF to a WebP thumbnail via sharp. The prebuilt
219
- * sharp binary bundles poppler, so PDF decoding is available without any extra
220
- * system packages on Linux/musl (Alpine) and glibc.
218
+ * Rasterize page 1 of a PDF to PNG with poppler's pdftoppm. sharp's prebuilt
219
+ * binaries ship without a PDF loader, so this is the actual rendering engine;
220
+ * font selection goes through fontconfig (font-noto et al. in the image).
221
221
  */
222
- const createPdfPreview = async (inputPath) => {
222
+ const renderPdfWithPoppler = (inputPath, originalName) => new Promise((resolve, reject) => {
223
+ const prefix = join(tmpdir(), `pdf-${randomString(16)}`);
224
+ const proc = spawn('pdftoppm', [
225
+ '-png',
226
+ '-singlefile',
227
+ '-r',
228
+ '150',
229
+ '-f',
230
+ '1',
231
+ '-l',
232
+ '1',
233
+ inputPath,
234
+ prefix,
235
+ ], { stdio: ['ignore', 'ignore', 'pipe'] });
236
+ let stderr = '';
237
+ proc.stderr?.setEncoding('utf8');
238
+ proc.stderr?.on('data', (chunk) => {
239
+ stderr += chunk;
240
+ });
241
+ proc.on('error', (err) => {
242
+ reject(err.code === 'ENOENT' ? new Error('pdftoppm binary not found') : err);
243
+ });
244
+ proc.on('close', (code) => {
245
+ if (code === 0) {
246
+ resolve(`${prefix}.png`);
247
+ }
248
+ else {
249
+ reject(new Error(`pdftoppm failed for ${originalName} (exit ${code}): ${stderr}`));
250
+ }
251
+ });
252
+ });
253
+ /**
254
+ * Render the first page of a PDF to a WebP thumbnail: try sharp (in case a
255
+ * future build gains a PDF loader), then pdftoppm, then let the caller fall
256
+ * back to the document card preview.
257
+ */
258
+ const createPdfPreview = async (inputPath, originalName) => {
223
259
  const outputPath = join(tmpdir(), `preview-${randomString(16)}.webp`);
224
- await sharp(inputPath, { density: 150, pages: 1 })
225
- .resize(previewMaxWidth)
226
- .webp({ quality: 80 })
227
- .toFile(outputPath);
228
- // sharp/poppler may produce a blank or corrupt thumbnail without erroring
229
- // when fonts are missing (text renders as tofu rectangles) or the PDF page is
230
- // genuinely empty. Validate the output so callers fall back to the document
231
- // card preview, which shows the filename instead of garbled content.
232
- const { size } = await fs.stat(outputPath);
233
- if (size < 100) {
234
- throw new Error('PDF preview rendered to empty image');
260
+ let rendered = false;
261
+ try {
262
+ await sharp(inputPath, { density: 150, pages: 1 })
263
+ .resize(previewMaxWidth)
264
+ .webp({ quality: 80 })
265
+ .toFile(outputPath);
266
+ rendered = true;
267
+ }
268
+ catch {
269
+ // sharp has no PDF loader here; render the page with poppler instead.
235
270
  }
271
+ if (!rendered) {
272
+ const pngPath = await renderPdfWithPoppler(inputPath, originalName);
273
+ try {
274
+ await sharp(pngPath)
275
+ .resize(previewMaxWidth)
276
+ .webp({ quality: 80 })
277
+ .toFile(outputPath);
278
+ rendered = true;
279
+ }
280
+ finally {
281
+ await fs.rm(pngPath, { force: true }).catch(() => undefined);
282
+ }
283
+ }
284
+ if (!rendered) {
285
+ throw new Error(`PDF preview failed for ${originalName}`);
286
+ }
287
+ // A broken render may still produce a file: reject a blank (uniform,
288
+ // near-white) page so callers fall back to the document card preview.
289
+ // Solid-color pages are legitimate content and must pass.
236
290
  const stats = await sharp(outputPath).stats();
237
- if (stats.channels.every((c) => c.stdev < 2)) {
291
+ const uniform = stats.channels.every((c) => c.stdev < 2);
292
+ const nearWhite = stats.channels.every((c) => c.mean > 250);
293
+ if (uniform && nearWhite) {
238
294
  throw new Error('PDF preview rendered to blank image');
239
295
  }
240
296
  return { path: outputPath, mimetype: 'image/webp' };
@@ -317,17 +373,18 @@ const createFilePreview = async (inputPath, mimetype, originalName) => {
317
373
  originalName.toLowerCase().endsWith('.pdf');
318
374
  if (isPdf) {
319
375
  try {
320
- return await createPdfPreview(inputPath);
376
+ return await createPdfPreview(inputPath, originalName);
321
377
  }
322
- catch {
323
- // sharp PDF support may be missing; fall back to card preview below.
378
+ catch (error) {
379
+ const message = error instanceof Error ? error.message : String(error);
380
+ log.warn(`PDF preview failed for ${originalName}: ${message}`);
324
381
  }
325
382
  }
326
383
  if (isOfficeDocument(originalName)) {
327
384
  let pdfPath;
328
385
  try {
329
386
  pdfPath = await convertOfficeToPdf(inputPath, originalName);
330
- return await createPdfPreview(pdfPath);
387
+ return await createPdfPreview(pdfPath, originalName);
331
388
  }
332
389
  catch (error) {
333
390
  const message = error instanceof Error ? error.message : String(error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpeepshq/core",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "type": "module",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "dependencies": {
@@ -37,10 +37,10 @@
37
37
  "type-fest": "^4.31.0",
38
38
  "uuidv7": "^1.0.2",
39
39
  "web-push": "^3.6.7",
40
- "@openpeepshq/fetch-client": "^0.2.1",
40
+ "@openpeepshq/common": "0.4.0",
41
41
  "@openpeepshq/i18n": "^0.3.1",
42
42
  "@openpeepshq/geocoder": "^0.1.2",
43
- "@openpeepshq/common": "0.4.0"
43
+ "@openpeepshq/fetch-client": "^0.2.1"
44
44
  },
45
45
  "main": "dist/index.js",
46
46
  "types": "dist/index.d.ts",