@openpeepshq/core 0.2.2 → 0.2.3

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.
@@ -110,7 +110,7 @@ export const transcodeIncomingMedia = async (type, inputPath, originalName, sour
110
110
  export const createPreview = async (inputPath, mimetype, originalName) => {
111
111
  switch (mimetype.split('/')[0]) {
112
112
  case 'image':
113
- return createImagePreview(inputPath);
113
+ return createImagePreview(inputPath, mimetype, originalName);
114
114
  case 'audio':
115
115
  return createAudioPreview(inputPath);
116
116
  case 'video':
@@ -122,10 +122,78 @@ export const createPreview = async (inputPath, mimetype, originalName) => {
122
122
  return createFilePreview(inputPath, mimetype, originalName);
123
123
  }
124
124
  };
125
- const createImagePreview = async (inputPath) => {
125
+ /**
126
+ * HEVC-coded HEIF variants. sharp's prebuilt libvips cannot decode these
127
+ * (AVIF, which shares the container, is handled natively).
128
+ */
129
+ const HEIF_MIMETYPES = new Set([
130
+ 'image/heic',
131
+ 'image/heif',
132
+ 'image/heic-sequence',
133
+ 'image/heif-sequence',
134
+ ]);
135
+ const isHeifFamily = (mimetype, originalName) => HEIF_MIMETYPES.has(mimetype) || /\.hei[cf]$/i.test(originalName);
136
+ /**
137
+ * Decode a HEIC/HEIF image to PNG with ImageMagick. Prefers the ImageMagick 7
138
+ * `magick` binary and falls back to the ImageMagick 6 `convert` (Debian/Ubuntu
139
+ * ship only the latter). `-auto-orient` bakes EXIF rotation into the PNG so
140
+ * the later sharp step cannot lose orientation with the pixel data.
141
+ */
142
+ const decodeWithImageMagick = async (inputPath) => {
143
+ const outputPath = tempPathFor('decode.png');
144
+ const args = [inputPath, '-auto-orient', outputPath];
145
+ await new Promise((resolve, reject) => {
146
+ const run = (bin) => {
147
+ const proc = spawn(bin, args, { stdio: ['ignore', 'ignore', 'pipe'] });
148
+ let stderr = '';
149
+ proc.stderr?.setEncoding('utf8');
150
+ proc.stderr?.on('data', (chunk) => {
151
+ stderr += chunk;
152
+ });
153
+ proc.on('error', (err) => {
154
+ if (err.code === 'ENOENT' && bin === 'magick') {
155
+ run('convert');
156
+ return;
157
+ }
158
+ reject(err.code === 'ENOENT'
159
+ ? new Error('ImageMagick binary not found')
160
+ : err);
161
+ });
162
+ proc.on('close', (code) => {
163
+ if (code === 0) {
164
+ resolve();
165
+ }
166
+ else {
167
+ reject(new Error(`ImageMagick ${bin} failed (exit ${code}): ${stderr}`));
168
+ }
169
+ });
170
+ };
171
+ run('magick');
172
+ });
173
+ return outputPath;
174
+ };
175
+ /**
176
+ * Render an image to a WebP thumbnail via sharp. sharp's prebuilt libvips
177
+ * bundles libheif for AVIF but ships without the HEVC codecs iPhone photos
178
+ * use, so HEVC-coded HEIC inputs are first decoded with ImageMagick (when
179
+ * installed with its heic delegate; see decodeWithImageMagick). Without it the
180
+ * sharp call below rejects them with MediaContentError, preserving the
181
+ * reject-uploads-we-cannot-process behaviour.
182
+ */
183
+ const createImagePreview = async (inputPath, mimetype, originalName) => {
126
184
  const outputPath = join(tmpdir(), `preview-${randomString(16)}.webp`);
185
+ let decodePath = inputPath;
186
+ if (isHeifFamily(mimetype, originalName)) {
187
+ try {
188
+ decodePath = await decodeWithImageMagick(inputPath);
189
+ }
190
+ catch (error) {
191
+ const message = error instanceof Error ? error.message : String(error);
192
+ log.warn(`ImageMagick HEIC decode skipped for ${originalName}: ${message}`);
193
+ }
194
+ }
127
195
  try {
128
- await sharp(inputPath)
196
+ await sharp(decodePath)
129
197
  .rotate()
130
198
  .resize(previewMaxWidth)
131
199
  .webp({ quality: 80 })
@@ -134,6 +202,11 @@ const createImagePreview = async (inputPath) => {
134
202
  catch {
135
203
  throw new MediaContentError("This file isn't a supported image.");
136
204
  }
205
+ finally {
206
+ if (decodePath !== inputPath) {
207
+ await fs.rm(decodePath, { force: true }).catch(() => undefined);
208
+ }
209
+ }
137
210
  return { path: outputPath, mimetype: 'image/webp' };
138
211
  };
139
212
  const escapeXml = (value) => value
@@ -325,7 +398,16 @@ const renderWaveform = (inputPath, outputPath, color) => new Promise((resolve, r
325
398
  });
326
399
  const createAudioPreview = async (inputPath) => {
327
400
  const outputPath = join(tmpdir(), `preview-${randomString(16)}.png`);
328
- const color = waveformColor(getTheme(await communityConfig()).primaryHex);
401
+ // The waveform color is cosmetic: an unreadable community config must not
402
+ // block preview generation, so fall back to the default theme primary.
403
+ let primaryHex = '#15678a';
404
+ try {
405
+ primaryHex = getTheme(await communityConfig()).primaryHex;
406
+ }
407
+ catch {
408
+ // keep default
409
+ }
410
+ const color = waveformColor(primaryHex);
329
411
  try {
330
412
  await renderWaveform(inputPath, outputPath, color);
331
413
  return { path: outputPath, mimetype: 'image/png' };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpeepshq/core",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
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/common": "0.4.0",
41
- "@openpeepshq/geocoder": "^0.1.2",
42
40
  "@openpeepshq/fetch-client": "^0.2.1",
43
- "@openpeepshq/i18n": "^0.2.1"
41
+ "@openpeepshq/i18n": "^0.3.1",
42
+ "@openpeepshq/geocoder": "^0.1.2",
43
+ "@openpeepshq/common": "0.4.0"
44
44
  },
45
45
  "main": "dist/index.js",
46
46
  "types": "dist/index.d.ts",