Skip to content

Commit

Permalink
feat: modernize
Browse files Browse the repository at this point in the history
  • Loading branch information
zaida04 committed Nov 7, 2023
1 parent 685a9bb commit 8a4da5f
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/multer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import type { StorageEngine } from "multer";
import type { Request } from "express";
import type { CloudflareCDNUploadResponse } from "./typings";

type CallbackFunction = (error: Error | null, info?: Partial<Express.Multer.File>) => void;

class CloudflareStorage implements StorageEngine {
private destURL: string;
public constructor(private accountID: string, private accountToken: string) {
Expand All @@ -12,40 +14,49 @@ class CloudflareStorage implements StorageEngine {
this.destURL = `https://api.cloudflare.com/client/v4/accounts/${this.accountID}/images/v1`;
}

public _handleFile(_req: Request, file: Express.Multer.File, callback: (error: Error | null, info?: Partial<Express.Multer.File>) => void): void {
public _handleFile(_req: Request, file: Express.Multer.File, callback: CallbackFunction): void {
const body = new FormData();
body.append("file", file.stream, file.originalname);

void fetch(this.destURL, {
this._uploadFile(body, callback);

Check failure on line 21 in src/multer.ts

View workflow job for this annotation

GitHub Actions / lint

Promises must be handled appropriately or explicitly marked as ignored with the `void` operator
}

private async _uploadFile(body: FormData, callback: CallbackFunction) {
const request = await fetch(this.destURL, {
method: "POST",
headers: {
Authorization: `Bearer ${this.accountToken}`,
...body.getHeaders()
},
body
}).then((response) => {
void (response.json() as Promise<CloudflareCDNUploadResponse>).then((data) => {
if (response.ok)
return callback(null, {
path: data.result.variants[0],
filename: data.result.filename,
destination: data.result.id
});
return callback(new Error("There was an error in uploading an asset to Cloudflare Images."));
})

Check failure on line 32 in src/multer.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;`

const response: CloudflareCDNUploadResponse = await request.json();
if (request.ok) {
return callback(null, {
path: response.result.variants[0],
filename: response.result.filename,
destination: response.result.id
});
});
}

return callback(new Error("There was an error in uploading an asset to Cloudflare Images."));
}

public _removeFile(_req: Request, file: Express.Multer.File, callback: (error: Error | null) => void): void {

Check failure on line 46 in src/multer.ts

View workflow job for this annotation

GitHub Actions / lint

Member _removeFile should be declared before all private instance method definitions
void fetch(`${this.destURL}/${file.destination}`, {
this._deleteFile(file.destination, callback);

Check failure on line 47 in src/multer.ts

View workflow job for this annotation

GitHub Actions / lint

Promises must be handled appropriately or explicitly marked as ignored with the `void` operator
}

private async _deleteFile(filedestination: string, callback: CallbackFunction) {
const request = await fetch(`${this.destURL}/${filedestination}`, {
method: "DELETE",
headers: {
Authorization: `Bearer ${this.accountToken}`
}
}).then((response) => {
if (response.ok) return callback(null);
return callback(new Error("There was an error in deleting the asset from Cloudflare Images."));
});

if (request.ok) return callback(null);
return callback(new Error("There was an error in deleting the asset from Cloudflare Images."));
}
}

Expand Down

0 comments on commit 8a4da5f

Please sign in to comment.