Uploader: make second argument an options object

This commit is contained in:
Alex Gleason 2024-04-29 15:32:18 -05:00
parent 25db277a9f
commit c786e1bc55
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 12 additions and 14 deletions

View File

@ -7,11 +7,11 @@ import type { Uploader } from './types.ts';
/** Meta-uploader determined from configuration. */
const configUploader: Uploader = {
upload(file, signal) {
return uploader().upload(file, signal);
upload(file, opts) {
return uploader().upload(file, opts);
},
delete(cid, signal) {
return uploader().delete(cid, signal);
delete(cid, opts) {
return uploader().delete(cid, opts);
},
};

View File

@ -18,7 +18,7 @@ const ipfsAddResponseSchema = z.object({
* and upload the file using the REST API.
*/
const ipfsUploader: Uploader = {
async upload(file, signal) {
async upload(file, opts) {
const url = new URL('/api/v0/add', Conf.ipfs.apiUrl);
const formData = new FormData();
@ -27,7 +27,7 @@ const ipfsUploader: Uploader = {
const response = await fetchWorker(url, {
method: 'POST',
body: formData,
signal,
signal: opts?.signal,
});
const { Hash } = ipfsAddResponseSchema.parse(await response.json());
@ -36,7 +36,7 @@ const ipfsUploader: Uploader = {
cid: Hash,
};
},
async delete(cid, signal) {
async delete(cid, opts) {
const url = new URL('/api/v0/pin/rm', Conf.ipfs.apiUrl);
const query = new URLSearchParams();
@ -46,7 +46,7 @@ const ipfsUploader: Uploader = {
await fetchWorker(url, {
method: 'POST',
signal,
signal: opts?.signal,
});
},
};

View File

@ -9,10 +9,9 @@ import type { Uploader } from './types.ts';
* take advantage of IPFS features while not really using IPFS.
*/
const s3Uploader: Uploader = {
async upload(file, _signal) {
async upload(file) {
const cid = await IpfsHash.of(file.stream()) as string;
// FIXME: Can't cancel S3 requests: https://github.com/bradenmacdonald/deno-s3-lite-client/issues/24
await client().putObject(`ipfs/${cid}`, file.stream(), {
metadata: {
'Content-Type': file.type,
@ -24,8 +23,7 @@ const s3Uploader: Uploader = {
cid,
};
},
async delete(cid, _signal) {
// FIXME: Can't cancel S3 requests: https://github.com/bradenmacdonald/deno-s3-lite-client/issues/24
async delete(cid) {
await client().deleteObject(`ipfs/${cid}`);
},
};

View File

@ -1,9 +1,9 @@
/** Modular uploader interface, to support uploading to different backends. */
interface Uploader {
/** Upload the file to the backend. */
upload(file: File, signal?: AbortSignal): Promise<UploadResult>;
upload(file: File, opts?: { signal?: AbortSignal }): Promise<UploadResult>;
/** Delete the file from the backend. */
delete(cid: string, signal?: AbortSignal): Promise<void>;
delete(cid: string, opts?: { signal?: AbortSignal }): Promise<void>;
}
/** Return value from the uploader after uploading a file. */