2023-09-07 16:59:05 -05:00
|
|
|
import { Conf } from '@/config.ts';
|
|
|
|
import { IpfsHash, S3Client } from '@/deps.ts';
|
|
|
|
|
|
|
|
import type { Uploader } from './types.ts';
|
|
|
|
|
2023-09-08 15:01:30 -05:00
|
|
|
/**
|
|
|
|
* S3-compatible uploader for AWS, Wasabi, DigitalOcean Spaces, and more.
|
|
|
|
* Files are named by their IPFS CID and exposed at `/ipfs/<cid>`, letting it
|
|
|
|
* take advantage of IPFS features while not really using IPFS.
|
|
|
|
*/
|
|
|
|
const s3Uploader: Uploader = {
|
2024-01-23 15:53:29 -06:00
|
|
|
async upload(file, _signal) {
|
2023-09-08 15:01:30 -05:00
|
|
|
const cid = await IpfsHash.of(file.stream()) as string;
|
2023-09-07 16:59:05 -05:00
|
|
|
|
2024-01-23 15:53:29 -06:00
|
|
|
// FIXME: Can't cancel S3 requests: https://github.com/bradenmacdonald/deno-s3-lite-client/issues/24
|
2023-09-11 14:41:30 -05:00
|
|
|
await client().putObject(`ipfs/${cid}`, file.stream(), {
|
2023-09-08 15:01:30 -05:00
|
|
|
metadata: {
|
|
|
|
'Content-Type': file.type,
|
|
|
|
'x-amz-acl': 'public-read',
|
|
|
|
},
|
|
|
|
});
|
2023-09-07 16:59:05 -05:00
|
|
|
|
2023-09-08 15:01:30 -05:00
|
|
|
return {
|
|
|
|
cid,
|
|
|
|
};
|
|
|
|
},
|
2024-01-23 15:53:29 -06:00
|
|
|
async delete(cid, _signal) {
|
|
|
|
// FIXME: Can't cancel S3 requests: https://github.com/bradenmacdonald/deno-s3-lite-client/issues/24
|
2023-09-11 14:41:30 -05:00
|
|
|
await client().deleteObject(`ipfs/${cid}`);
|
2023-09-08 15:01:30 -05:00
|
|
|
},
|
2023-09-07 16:59:05 -05:00
|
|
|
};
|
|
|
|
|
2023-09-11 14:41:30 -05:00
|
|
|
/** Build S3 client from config. */
|
|
|
|
function client() {
|
|
|
|
return new S3Client({ ...Conf.s3 });
|
|
|
|
}
|
|
|
|
|
2023-09-07 16:59:05 -05:00
|
|
|
export { s3Uploader };
|