Skip to content

Commit

Permalink
backend: Fixed recording deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
CSantosM committed Jul 30, 2024
1 parent 21a4a7a commit f6e9b2b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
4 changes: 2 additions & 2 deletions openvidu-call-back/src/services/recording.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ export class RecordingService {
if (!recordingPath) throw internalError(`Error extracting path from recording ${egressId}`);

this.logger.info(`Deleting recording from S3 ${recordingPath}`);
// await this.s3Service.deleteObject(recordingPath);
await Promise.all([this.s3Service.deleteObject(metadataPath!), this.s3Service.deleteFolder(recordingPath)]);

await Promise.all([this.s3Service.deleteObject(metadataPath!), this.s3Service.deleteObject(recordingPath)]);

if (!isRequestedByAdmin) {
const signalOptions: SendDataOptions = {
Expand Down
71 changes: 41 additions & 30 deletions openvidu-call-back/src/services/s3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ export class S3Service {
}
}

/**
* Deletes an object from an S3 bucket.
*
* @param name - The name of the object to delete.
* @param bucket - The name of the S3 bucket (optional, defaults to CALL_S3_BUCKET).
* @returns A promise that resolves to the result of the delete operation.
* @throws Throws an error if there was an error deleting the object.
*/
async deleteObject(name: string, bucket: string = CALL_S3_BUCKET): Promise<DeleteObjectCommandOutput> {
try {
this.logger.info(`Deleting object in S3: ${name}`);
Expand All @@ -115,36 +123,39 @@ export class S3Service {
}
}

async deleteFolder(folderName: string, bucket: string = CALL_S3_BUCKET) {
try {
const listParams = {
Bucket: bucket,
Prefix: folderName.endsWith('/') ? folderName : `${folderName}/`
};
// Get all objects in the folder
const listedObjects: ListObjectsV2CommandOutput = await this.run(new ListObjectsV2Command(listParams));
const deleteParams = {
Bucket: bucket,
Delete: {
Objects: listedObjects?.Contents?.map(({ Key }) => ({ Key }))
}
};

// Skip if no objects found
if (!deleteParams.Delete.Objects || deleteParams.Delete.Objects.length === 0) return;

this.logger.info(`Deleting objects in S3: ${deleteParams.Delete.Objects}`);
await this.run(new DeleteObjectsCommand(deleteParams));

if (listedObjects.IsTruncated) {
this.logger.verbose(`Folder ${folderName} is truncated, deleting next batch`);
await this.deleteFolder(bucket, folderName);
}
} catch (error) {
this.logger.error(`Error deleting folder in S3: ${error}`);
throw internalError(error);
}
}
// async deleteFolder(folderName: string, bucket: string = CALL_S3_BUCKET) {
// try {
// const listParams = {
// Bucket: bucket,
// Prefix: folderName.endsWith('/') ? folderName : `${folderName}/`
// };
// // Get all objects in the folder
// const listedObjects: ListObjectsV2CommandOutput = await this.run(new ListObjectsV2Command(listParams));
// const deleteParams = {
// Bucket: bucket,
// Delete: {
// Objects: listedObjects?.Contents?.map(({ Key }) => ({ Key }))
// }
// };

// // Skip if no objects found
// if (!deleteParams.Delete.Objects || deleteParams.Delete.Objects.length === 0){
// this.logger.error(`No objects found in folder ${folderName}. Nothing to delete`);
// return;
// }

// this.logger.info(`Deleting objects in S3: ${deleteParams.Delete.Objects}`);
// await this.run(new DeleteObjectsCommand(deleteParams));

// if (listedObjects.IsTruncated) {
// this.logger.verbose(`Folder ${folderName} is truncated, deleting next batch`);
// await this.deleteFolder(bucket, folderName);
// }
// } catch (error) {
// this.logger.error(`Error deleting folder in S3: ${error}`);
// throw internalError(error);
// }
// }

/**
* Lists objects in an S3 bucket.
Expand Down

0 comments on commit f6e9b2b

Please sign in to comment.