From 7ada849a6a217a462ab8c0205d40eb276320230c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 29 Apr 2024 15:54:01 -0500 Subject: [PATCH] s3: support pathStyle --- deno.json | 3 +++ src/upload.ts | 3 +-- src/uploaders/ipfs.ts | 6 ++++-- src/uploaders/s3.ts | 31 ++++++++++++++++++++----------- src/uploaders/types.ts | 10 ++++++++-- 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/deno.json b/deno.json index c5681e3..d241262 100644 --- a/deno.json +++ b/deno.json @@ -18,7 +18,10 @@ "@/": "./src/", "@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.15.0", "@std/cli": "jsr:@std/cli@^0.223.0", + "@std/crypto": "jsr:@std/crypto@^0.224.0", + "@std/encoding": "jsr:@std/encoding@^0.224.0", "@std/json": "jsr:@std/json@^0.223.0", + "@std/media-types": "jsr:@std/media-types@^0.224.0", "@std/streams": "jsr:@std/streams@^0.223.0", "hono": "https://deno.land/x/hono@v3.10.1/mod.ts", "hono/middleware": "https://deno.land/x/hono@v3.10.1/middleware.ts", diff --git a/src/upload.ts b/src/upload.ts index 5c16501..632dbab 100644 --- a/src/upload.ts +++ b/src/upload.ts @@ -16,8 +16,7 @@ async function uploadFile(file: File, meta: FileMeta, signal?: AbortSignal) { throw new Error('File size is too large.'); } - const { cid } = await uploader.upload(file, signal); - const url = new URL(`/ipfs/${cid}`, Conf.mediaDomain).toString(); + const { url } = await uploader.upload(file, { signal }); return insertUnattachedMedia({ pubkey, diff --git a/src/uploaders/ipfs.ts b/src/uploaders/ipfs.ts index 6e0e0e7..21619b5 100644 --- a/src/uploaders/ipfs.ts +++ b/src/uploaders/ipfs.ts @@ -30,10 +30,12 @@ const ipfsUploader: Uploader = { signal: opts?.signal, }); - const { Hash } = ipfsAddResponseSchema.parse(await response.json()); + const { Hash: cid } = ipfsAddResponseSchema.parse(await response.json()); return { - cid: Hash, + id: cid, + cid, + url: new URL(`/ipfs/${cid}`, Conf.mediaDomain).toString(), }; }, async delete(cid, opts) { diff --git a/src/uploaders/s3.ts b/src/uploaders/s3.ts index 378b279..29f3043 100644 --- a/src/uploaders/s3.ts +++ b/src/uploaders/s3.ts @@ -1,30 +1,39 @@ +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 { IpfsHash, S3Client } from '@/deps.ts'; +import { S3Client } from '@/deps.ts'; import type { Uploader } from './types.ts'; -/** - * S3-compatible uploader for AWS, Wasabi, DigitalOcean Spaces, and more. - * Files are named by their IPFS CID and exposed at `/ipfs/`, letting it - * take advantage of IPFS features while not really using IPFS. - */ +/** S3-compatible uploader for AWS, Wasabi, DigitalOcean Spaces, and more. */ const s3Uploader: Uploader = { async upload(file) { - const cid = await IpfsHash.of(file.stream()) as string; + const sha256 = encodeHex(await crypto.subtle.digest('SHA-256', file.stream())); + const ext = extensionsByType(file.type)?.[0] ?? 'bin'; + const filename = `${sha256}.${ext}`; - await client().putObject(`ipfs/${cid}`, file.stream(), { + await client().putObject(filename, file.stream(), { metadata: { 'Content-Type': file.type, 'x-amz-acl': 'public-read', }, }); + const { pathStyle, bucket } = Conf.s3; + const path = (pathStyle && bucket) ? join(bucket, filename) : filename; + return { - cid, + id: filename, + sha256, + url: new URL(path, Conf.mediaDomain).toString(), }; }, - async delete(cid) { - await client().deleteObject(`ipfs/${cid}`); + async delete(id) { + await client().deleteObject(id); }, }; diff --git a/src/uploaders/types.ts b/src/uploaders/types.ts index 8898048..c514ad1 100644 --- a/src/uploaders/types.ts +++ b/src/uploaders/types.ts @@ -8,8 +8,14 @@ interface Uploader { /** Return value from the uploader after uploading a file. */ interface UploadResult { - /** IPFS CID for the file. */ - cid: string; + /** File ID specific to the uploader, so it can later be referenced or deleted. */ + id: string; + /** URL where the file can be accessed. */ + url: string; + /** SHA-256 hash of the file. */ + sha256?: string; + /** IPFS CID of the file. */ + cid?: string; } export type { Uploader };