feat: implement file existence check and improve file upload handling
This commit is contained in:
@@ -2,6 +2,7 @@ import { StorageProvider } from '@/lib/storage/storage.interface';
|
||||
import { TypedResult, wrap } from '@/utils/types/results';
|
||||
import {
|
||||
DeleteObjectCommand,
|
||||
HeadObjectCommand,
|
||||
ObjectCannedACL,
|
||||
PutObjectCommand,
|
||||
S3Client,
|
||||
@@ -36,6 +37,9 @@ export class S3StorageAdapter implements StorageProvider {
|
||||
readonly put: (
|
||||
...args: Parameters<StorageProvider['put']>
|
||||
) => Promise<TypedResult<string>>;
|
||||
readonly exists: (
|
||||
...args: Parameters<StorageProvider['exists']>
|
||||
) => Promise<boolean>;
|
||||
readonly delete: (
|
||||
...args: Parameters<StorageProvider['delete']>
|
||||
) => Promise<TypedResult<void>>;
|
||||
@@ -58,6 +62,7 @@ export class S3StorageAdapter implements StorageProvider {
|
||||
this.get = wrap(this._get.bind(this));
|
||||
this.put = wrap(this._put.bind(this));
|
||||
this.delete = wrap(this._delete.bind(this));
|
||||
this.exists = this._exists.bind(this);
|
||||
}
|
||||
|
||||
private async _get(key: string): Promise<string> {
|
||||
@@ -75,6 +80,20 @@ export class S3StorageAdapter implements StorageProvider {
|
||||
return await getSignedUrl(this.s3Client, command, { expiresIn: 3600 });
|
||||
}
|
||||
|
||||
private async _exists(key: string): Promise<boolean> {
|
||||
try {
|
||||
await this.s3Client.send(
|
||||
new HeadObjectCommand({
|
||||
Bucket: this.bucketName,
|
||||
Key: key,
|
||||
})
|
||||
);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async _delete(key: string): Promise<void> {
|
||||
await this.s3Client.send(
|
||||
new DeleteObjectCommand({
|
||||
|
||||
Reference in New Issue
Block a user