Skip to content

Commit

Permalink
remove baseurl on edits
Browse files Browse the repository at this point in the history
  • Loading branch information
robertotcestari committed Aug 4, 2024
1 parent 4602f62 commit faa4ca2
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/lib/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,31 @@ export class S3 {
}

async deleteImage(imagePath: string): Promise<void> {
// remove the base url
imagePath = this.removeBaseUrl(imagePath);

const params = {
Bucket: process.env.AWS_BUCKET_NAME ?? '',
Key: imagePath,
};

// remove the base url

try {
await this.client.send(new DeleteObjectCommand(params));
} catch (e: any) {
e.message = `S3 delete error: ${e.message}`;
throw e;
}
}

removeBaseUrl(imageUrl: string): string {
try {
new URL(imageUrl); // Check if it's a valid URL
const url = new URL(imageUrl);
return url.pathname.substring(1); // Remove leading '/' from the pathname
} catch (error) {
return imageUrl; // Return the original if not a valid URL
}
}
}

0 comments on commit faa4ca2

Please sign in to comment.