Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ export class UploadCleanupSchedulerService {
for (const upload of uploads) {
try {
const files = await this.fileRepository.find({ _id: upload._uploadedFileId });
await this.storageService.deleteFolder(upload.id);

await Promise.all(
await Promise.allSettled(
files.map(async (file) => {
try {
await Upload.updateOne({ _uploadedFileId: file._id }, { $set: { _uploadedFileId: '' } });

// Delete file from storage and db
try {
await this.storageService.deleteFile(file.path);
await this.fileRepository.delete({ _id: file._id });
} catch (error) {}

Expand Down
50 changes: 50 additions & 0 deletions libs/services/src/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
GetObjectCommand,
DeleteObjectCommand,
ListBucketsCommand,
ListObjectsV2Command,
DeleteObjectsCommand,
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { FileNotExistError, Defaults } from '@impler/shared';
Expand All @@ -28,6 +30,7 @@ export abstract class StorageService {
abstract getFileStream(key: string): Promise<Readable>;
abstract writeStream(key: string, stream: Readable, contentType: string): Promise<void>;
abstract deleteFile(key: string): Promise<void>;
abstract deleteFolder(key: string): Promise<void>;
abstract isConnected(): boolean;
abstract getSignedUrl(key: string): Promise<string>;
}
Expand Down Expand Up @@ -64,6 +67,46 @@ export class S3StorageService implements StorageService {
this.isS3Connected = false;
});
}
async deleteFolder(prefix: string): Promise<void> {
try {
// Ensure prefix ends with '/' if it's not empty
const folderPrefix = prefix && !prefix.endsWith('/') ? `${prefix}/` : prefix;

let continuationToken: string | undefined;

do {
// List objects with the given prefix
const listCommand = new ListObjectsV2Command({
Bucket: process.env.S3_BUCKET_NAME,
Prefix: folderPrefix,
ContinuationToken: continuationToken,
});

const listResponse = await this.s3.send(listCommand);

if (listResponse.Contents && listResponse.Contents.length > 0) {
// Prepare objects for deletion (max 1000 objects per delete request)
const objectsToDelete = listResponse.Contents.map((obj) => ({
Key: obj.Key,
}));

// Delete the objects
const deleteCommand = new DeleteObjectsCommand({
Bucket: process.env.S3_BUCKET_NAME,
Delete: {
Objects: objectsToDelete,
Quiet: true, // Don't return details about deleted objects
},
});

await this.s3.send(deleteCommand);
}

// Check if there are more objects to delete
continuationToken = listResponse.NextContinuationToken;
} while (continuationToken);
} catch (error) {}
}

async uploadFile(key: string, file: Buffer | string | Readable, contentType: string): Promise<IStorageResponse> {
const command = new PutObjectCommand({
Expand Down Expand Up @@ -275,6 +318,13 @@ export class AzureStorageService implements StorageService {
await blockBlobClient.delete();
}

async deleteFolder(prefix: string): Promise<void> {
for await (const blob of this.containerClient.listBlobsFlat({ prefix })) {
const blockBlobClient = this.containerClient.getBlockBlobClient(blob.name);
await blockBlobClient.deleteIfExists();
}
}

isConnected(): boolean {
return this.isAzureConnected;
}
Expand Down
Loading