refactor: unify download utils and temp directories#132
refactor: unify download utils and temp directories#132
Conversation
- Consolidate three temp directories (/tmp/dmwork-upload, /tmp/dmwork-media,
/tmp/dmwork-files) into /tmp/dmwork-temp/{upload,media,files}
- Extract common stream download logic into src/temp-utils.ts with
streamDownloadToFile() supporting backpressure, size limits, and HEAD pre-check
- Add throttled cleanup (cleanupTempDir) — skips directory scan if called
within 10 minutes of last run
- Existing function signatures and behavior unchanged:
- downloadToTempFile returns {tempPath, contentType}
- downloadMediaToLocal returns string|undefined (graceful degradation)
- downloadToTemp throws on error
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Jerry-Xin
left a comment
There was a problem hiding this comment.
Good refactoring — consolidating three duplicate download+cleanup patterns into a shared utility reduces maintenance surface and makes behavior consistent. A few things worth addressing:
-
Dropped
signalparameter (channel.ts):downloadToTempFilestill acceptssignal?: AbortSignalbut the new implementation ignores it entirely. Callers relying on abort signal for cancellation will find it non-functional. Either wire it throughStreamDownloadOptionsor remove the parameter to avoid a misleading API. -
HEAD response unchecked (
temp-utils.ts:75): The HEAD pre-check does not validatehead.ok— a 404/500 HEAD response yieldscontentLength = 0and silently passes the size gate. Consider only applying the size check whenhead.okis true, so a broken HEAD falls through to the GET request naturally. -
Fragile error message routing (
inbound.tsdownloadMediaToLocal): The catch block routes log levels by checkingmsg.includes("HTTP ")andmsg.includes("File too large"). IfstreamDownloadToFileerror message wording changes, these branches break silently. Consider using typed error subclasses or an error code field for reliable branching.
Overall this is a solid DRY improvement — the shared streamDownloadToFile with backpressure and throttled cleanup is well-structured. 👍
yujiawei
left a comment
There was a problem hiding this comment.
Coda review ✅ — temp-utils.ts 抽取合理,streamDownloadToFile 统一三处下载逻辑,cleanup 节流设计好。CI 204 测试全过。等 TestBot 验证后合并。
|
Thanks for confirming the overall approach looks good! Before merging, could you clarify whether the three items from the review are being tracked?
None of these are blocking if you've considered them and have a reason to defer — just want to make sure they don't slip through. 🙂 |
Summary
/tmp/dmwork-upload,/tmp/dmwork-media,/tmp/dmwork-files) 合并为/tmp/dmwork-temp/{upload,media,files}src/temp-utils.ts的streamDownloadToFile(),支持 backpressure、size limit、HEAD 预检cleanupTempDir()10 分钟内不重复扫同一目录变更文件
src/temp-utils.tscleanupTempDir、streamDownloadToFilesrc/temp-utils.test.tssrc/channel.tsUPLOAD_TEMP_DIR、downloadToTempFile实现、cleanupOldUploadTempFiles,改用 temp-utilssrc/inbound.tsMEDIA_TEMP_DIR、TEMP_DIR、cleanupMediaTempFiles、cleanupTempFiles,三个下载函数改为调用streamDownloadToFile的 wrappersrc/inbound.test.ts/tmp/dmwork-media/→/tmp/dmwork-temp/media/Test plan
npm test— 204 tests passed (8 test files)/tmp/dmwork-temp/media//tmp/dmwork-temp/upload//tmp/dmwork-temp/files/🤖 Generated with Claude Code