mirror of
https://github.com/directus/directus.git
synced 2026-01-13 18:18:17 -05:00
Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch> Co-authored-by: ian <licitdev@gmail.com>
42 lines
804 B
TypeScript
42 lines
804 B
TypeScript
import { createHash } from 'node:crypto';
|
|
import fs from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
|
|
async function createTmpDirectory() {
|
|
const path = await fs.mkdtemp(join(tmpdir(), 'directus-'));
|
|
|
|
async function cleanup() {
|
|
return await fs.rmdir(path);
|
|
}
|
|
|
|
return {
|
|
path,
|
|
cleanup,
|
|
};
|
|
}
|
|
|
|
export async function createTmpFile() {
|
|
const dir = await createTmpDirectory();
|
|
const filename = createHash('sha1').update(new Date().toString()).digest('hex').substring(0, 8);
|
|
const path = join(dir.path, filename);
|
|
|
|
try {
|
|
const fd = await fs.open(path, 'wx');
|
|
await fd.close();
|
|
} catch (err) {
|
|
await dir.cleanup();
|
|
throw err;
|
|
}
|
|
|
|
async function cleanup() {
|
|
await fs.unlink(path);
|
|
await dir.cleanup();
|
|
}
|
|
|
|
return {
|
|
path,
|
|
cleanup,
|
|
};
|
|
}
|