Skip to content

Commit 0e22c43

Browse files
refactor(media): streamline temporary file handling and remove unused imports
1 parent f1337d8 commit 0e22c43

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

src/worker/workers/media_processor_worker.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@
88

99
import path from 'path';
1010
import fs from 'fs-extra';
11-
import { fileURLToPath } from 'url';
1211
import log from '../../lib/logger.js';
13-
import config from '../../../config.js';
12+
import { tmpDir } from '../../lib/tempManager.js';
1413
import { fetch, parseArgsToFetchOptions, getHeader } from '../../addon/bridge.js';
1514

16-
const __filename = fileURLToPath(import.meta.url);
17-
const __dirname = path.dirname(__filename);
18-
1915
export default async function ({ argsArray }) {
2016
const { url, options } = parseArgsToFetchOptions(argsArray);
2117
try {
@@ -37,10 +33,6 @@ export default async function ({ argsArray }) {
3733
}
3834
const isDirectDownload = contentType.startsWith('image/') || contentType.startsWith('audio/') || contentType.startsWith('video/') || contentType.startsWith('application/pdf') || contentType.startsWith('application/zip') || contentType.startsWith('application/octet-stream');
3935
if (isDirectDownload) {
40-
const tempDir = config.paths.tempDir;
41-
if (!fs.existsSync(tempDir)) {
42-
fs.mkdirSync(tempDir, { recursive: true });
43-
}
4436
let extension = '';
4537
try {
4638
const urlPath = new URL(url).pathname;
@@ -54,9 +46,8 @@ export default async function ({ argsArray }) {
5446
extension = ctParts[1].split(';')[0];
5547
}
5648
}
57-
const tempFileName = `${Date.now()}-${Math.random().toString(36).substring(2, 9)}.${extension || 'tmp'}`;
58-
const tempFilePath = path.join(tempDir, tempFileName);
59-
fs.writeFileSync(tempFilePath, bodyBuffer);
49+
const tempFilePath = tmpDir.createTempFile(extension, 'fetch-');
50+
await fs.writeFile(tempFilePath, bodyBuffer);
6051
return {
6152
type: 'local_file',
6253
path: tempFilePath

0 commit comments

Comments
 (0)