/
githubmirror
/
trpc
Обзор
Документация
Войти
/
githubmirror
/
trpc
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
examples/next-formdata/src/utils/writeFileToDisk.ts
30 строк
794 B
Alex / KATT
chore(server): rewrite `resolveHTTPResponse` with Fetch (#5684)
30 апр 2024, 22:54
Не верифицирован
30 апр 2024, 22:54
01bb2ea
Код
Авторство
О чём код?
import fs from 'node:fs'; import path from 'node:path'; import { Readable } from 'node:stream'; export async function writeFileToDisk(file: File) { const rootDir = __dirname + '/../../../../..'; const nonce = Date.now(); const fileDir = path.resolve(`${rootDir}/public/uploads/${nonce}`); if (!fs.existsSync(fileDir)) { fs.mkdirSync(fileDir, { recursive: true }); } console.log('Writing', file.name, 'to', fileDir); const fd = fs.createWriteStream(path.resolve(`${fileDir}/${file.name}`)); const fileStream = Readable.fromWeb( // @ts-expect-error - unsure why this is not working file.stream(), ); for await (const chunk of fileStream) { fd.write(chunk); } fd.end(); return { url: `/uploads/${nonce}/${file.name}`, name: file.name, }; }