|
1 | | -import { execFile } from "node:child_process"; |
2 | 1 | import fs from "node:fs/promises"; |
3 | 2 | import path from "node:path"; |
4 | | -import { promisify } from "node:util"; |
5 | 3 | import { isInboundPathAllowed } from "openclaw/plugin-sdk/media-runtime"; |
6 | 4 | import { saveMediaBuffer } from "openclaw/plugin-sdk/media-store"; |
7 | | -import { buildRandomTempFilePath } from "openclaw/plugin-sdk/temp-path"; |
| 5 | +import { loadWebMedia } from "openclaw/plugin-sdk/web-media"; |
8 | 6 | import type { IMessageAttachment } from "./types.js"; |
9 | 7 |
|
10 | | -const execFileAsync = promisify(execFile); |
11 | | - |
12 | | -const HEIC_CONVERSION_TIMEOUT_MS = 15_000; |
13 | | -const HEIC_CONVERSION_MAX_BUFFER_BYTES = 64 * 1024; |
14 | | - |
15 | 8 | export type StagedIMessageAttachment = { |
16 | 9 | path: string; |
17 | 10 | contentType?: string; |
@@ -73,43 +66,6 @@ async function resolveAllowedCanonicalAttachmentPath(params: { |
73 | 66 | return canonicalPath; |
74 | 67 | } |
75 | 68 |
|
76 | | -async function convertHeicToJpegWithSips(sourcePath: string, maxBytes: number): Promise<Buffer> { |
77 | | - const tempPath = buildRandomTempFilePath({ |
78 | | - prefix: "openclaw-imessage", |
79 | | - extension: "jpg", |
80 | | - }); |
81 | | - try { |
82 | | - await execFileAsync( |
83 | | - "sips", |
84 | | - [ |
85 | | - "-s", |
86 | | - "format", |
87 | | - "jpeg", |
88 | | - "-s", |
89 | | - "formatOptions", |
90 | | - "90", |
91 | | - "-Z", |
92 | | - "4096", |
93 | | - sourcePath, |
94 | | - "--out", |
95 | | - tempPath, |
96 | | - ], |
97 | | - { |
98 | | - timeout: HEIC_CONVERSION_TIMEOUT_MS, |
99 | | - maxBuffer: HEIC_CONVERSION_MAX_BUFFER_BYTES, |
100 | | - killSignal: "SIGKILL", |
101 | | - }, |
102 | | - ); |
103 | | - const stat = await fs.stat(tempPath); |
104 | | - if (stat.size > maxBytes) { |
105 | | - throw new Error(`converted media exceeds ${Math.round(maxBytes / (1024 * 1024))}MB limit`); |
106 | | - } |
107 | | - return await fs.readFile(tempPath); |
108 | | - } finally { |
109 | | - await fs.rm(tempPath, { force: true }).catch(() => {}); |
110 | | - } |
111 | | -} |
112 | | - |
113 | 69 | async function readAttachmentBuffer(params: { |
114 | 70 | attachmentPath: string; |
115 | 71 | mimeType?: string | null; |
@@ -142,11 +98,20 @@ async function readAttachmentBuffer(params: { |
142 | 98 |
|
143 | 99 | if (isHeicAttachment(params.attachmentPath, params.mimeType)) { |
144 | 100 | try { |
145 | | - const convert = params.deps.convertHeicToJpeg ?? convertHeicToJpegWithSips; |
| 101 | + const convert = params.deps.convertHeicToJpeg; |
| 102 | + const converted = convert |
| 103 | + ? { |
| 104 | + buffer: await convert(canonicalPath, params.maxBytes), |
| 105 | + fileName: jpegFilenameForAttachment(params.attachmentPath), |
| 106 | + } |
| 107 | + : await loadWebMedia(canonicalPath, { |
| 108 | + maxBytes: params.maxBytes, |
| 109 | + localRoots: [path.dirname(canonicalPath)], |
| 110 | + }); |
146 | 111 | return { |
147 | | - buffer: await convert(canonicalPath, params.maxBytes), |
| 112 | + buffer: converted.buffer, |
148 | 113 | contentType: "image/jpeg", |
149 | | - originalFilename: jpegFilenameForAttachment(params.attachmentPath), |
| 114 | + originalFilename: converted.fileName ?? jpegFilenameForAttachment(params.attachmentPath), |
150 | 115 | }; |
151 | 116 | } catch (err) { |
152 | 117 | params.deps.logVerbose?.( |
|
0 commit comments