mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
36 lines
1 KiB
TypeScript
36 lines
1 KiB
TypeScript
import { join } from 'node:path';
|
|
|
|
import { crypto } from '@std/crypto';
|
|
import { encodeHex } from '@std/encoding/hex';
|
|
import { extensionsByType } from '@std/media-types';
|
|
|
|
import { Conf } from '@/config.ts';
|
|
|
|
import type { Uploader } from './types.ts';
|
|
|
|
/** Local filesystem uploader. */
|
|
const localUploader: Uploader = {
|
|
async upload(file) {
|
|
const sha256 = encodeHex(await crypto.subtle.digest('SHA-256', file.stream()));
|
|
const ext = extensionsByType(file.type)?.[0] ?? 'bin';
|
|
const filename = `${sha256}.${ext}`;
|
|
|
|
await Deno.mkdir(Conf.uploadsDir, { recursive: true });
|
|
await Deno.writeFile(join(Conf.uploadsDir, filename), file.stream());
|
|
|
|
const { mediaDomain } = Conf;
|
|
const url = new URL(mediaDomain);
|
|
const path = url.pathname === '/' ? filename : join(url.pathname, filename);
|
|
|
|
return {
|
|
id: filename,
|
|
sha256,
|
|
url: new URL(path, url).toString(),
|
|
};
|
|
},
|
|
async delete(id) {
|
|
await Deno.remove(join(Conf.uploadsDir, id));
|
|
},
|
|
};
|
|
|
|
export { localUploader };
|