using Amazon.S3; using Amazon.S3.Model; using Amazon.S3.Transfer; using Ardalis.Result; using Microsoft.Extensions.Logging; using MimeMapping; using Musify.Application.Contracts.Infrastructure; using Musify.Domain.Entities; using Musify.Infrastructure.Configuration; namespace Musify.Infrastructure.Storage { public class StorageHandler(IDatabase database, IAmazonS3 amazonS3, InfrastructureStorageConfiguration storageClientConfiguration, ILogger logger) : IStorageHandler { public async Task> GetFileAsync(string bucketName, string keyName, CancellationToken cancellationToken) { var request = new GetObjectRequest { BucketName = bucketName, Key = keyName, }; logger.LogDebug("Getting file from S3. Bucket: {BucketName}, Key: {KeyName}", bucketName, keyName); try { var response = await amazonS3.GetObjectAsync(request, cancellationToken); logger.LogDebug("Successfully got file from S3 with bucket name {BucketName} and key name {KeyName}", bucketName, keyName); return Result.Success(response.ResponseStream); } catch (Exception exception) { logger.LogWarning(exception, "Failed to get file from S3. Bucket: {BucketName}, Key: {KeyName}", bucketName, keyName); return Result.NotFound($"File not found in S3 with bucket name {bucketName} and key name {keyName}"); } } public async Task GetUrlAsync(string bucketName, string keyName, TimeSpan expirationTime, CancellationToken cancellationToken) { var request = new GetPreSignedUrlRequest { BucketName = bucketName, Key = keyName, Expires = DateTime.UtcNow + expirationTime, Protocol = storageClientConfiguration.UseHttp ? Protocol.HTTP : Protocol.HTTPS }; logger.LogDebug("Getting pre-signed URL from S3. Bucket: {BucketName}, Key: {KeyName}, Expires: {Expires}", bucketName, keyName, request.Expires); var url = await amazonS3.GetPreSignedURLAsync(request); return url; } public async Task RemoveFileAsync(string bucketName, string keyName, CancellationToken cancellationToken) { var request = new DeleteObjectRequest { BucketName = bucketName, Key = keyName, }; logger.LogDebug("Removing file from S3. Bucket: {BucketName}, Key: {KeyName}", bucketName, keyName); var response = await amazonS3.DeleteObjectAsync(request, cancellationToken); return Result.NoContent(); } public async Task TransferFilesAsync(string sourceDirectory, string bucketName, string route, CancellationToken cancellationToken) { var trasnsferUtility = new TransferUtility(amazonS3); var files = Directory.GetFiles(sourceDirectory, "*"); var successCount = 0; var failedCount = 0; foreach (var file in files) { string fileName = Path.GetFileName(file); string filePath = Path.Combine(sourceDirectory, file); var keyName = Path.Combine(route, fileName); var upload = new Upload { Id = Guid.NewGuid(), BucketName = bucketName, KeyName = keyName, ContentType = "Application/Octet-Stream" }; await database.Uploads.AddAsync(upload, cancellationToken); try { await trasnsferUtility.UploadAsync(filePath, bucketName, keyName, cancellationToken); upload.State = UploadState.Successful; successCount++; logger.LogDebug("Transferred file to S3. Bucket: {BucketName}, KeyName: {KeyName}", bucketName, keyName); } catch (Exception exception) { failedCount++; upload.State = UploadState.Failed; logger.LogError(exception, "Failed to transfer file to S3. Bucket: {BucketName}, keyName: {keyName}", bucketName, keyName); } } await database.SaveChangesAsync(cancellationToken); logger.LogInformation("TransferFiles completed. Bucket: {BucketName}, Route: {Route}, Success: {SuccessCount}, Failed: {FailedCount}", bucketName, route, successCount, failedCount); return Result.Success(); } public async Task> UploadFileAsync(string filePath, string bucketName, string keyName, CancellationToken cancellationToken) { try { using var fileStream = File.OpenRead(filePath); var contentType = MimeUtility.GetMimeMapping(filePath); return await UploadFileAsync(fileStream, contentType, bucketName, keyName, cancellationToken); } catch (Exception exception) { logger.LogError(exception, "Failed to upload file to S3 with bucket name {BucketName} and key name {KeyName}", bucketName, keyName); return Result.Error($"Failed to upload file to S3 with bucket name {bucketName} and key name {keyName}"); } } public async Task> UploadFileAsync(Stream sourceStream, string contentType, string bucketName, string keyName, CancellationToken cancellationToken) { var request = new PutObjectRequest { BucketName = bucketName, ContentType = contentType, InputStream = sourceStream, Key = keyName }; var upload = new Upload { Id = Guid.NewGuid(), BucketName = bucketName, KeyName = keyName, ContentType = contentType }; await database.Uploads.AddAsync(upload, cancellationToken); try { var response = await amazonS3.PutObjectAsync(request, cancellationToken); logger.LogDebug("Uploaded file to S3. Bucket: {BucketName}, Key: {KeyName}", bucketName, keyName); } catch (Exception exception) { logger.LogError(exception, "Failed to upload file to S3 with bucket name {BucketName} and key name {KeyName}", bucketName, keyName); upload.State = UploadState.Failed; return Result.Error($"Failed to upload file to S3 with bucket name {bucketName} and key name {keyName}"); } upload.State = UploadState.Successful; await database.SaveChangesAsync(cancellationToken); logger.LogDebug("Saved upload record to database. Bucket: {BucketName}, Key: {KeyName}", bucketName, keyName); return Result.Success(keyName); } public async Task CopyFileAsync(string sourceBucketName, string sourceKeyName, string destinationBucketName, string destinationKeyName, CancellationToken cancellationToken) { var request = new CopyObjectRequest { SourceBucket = sourceBucketName, SourceKey = sourceKeyName, DestinationBucket = destinationBucketName, DestinationKey = destinationKeyName }; try { var response = await amazonS3.CopyObjectAsync(request, cancellationToken); logger.LogInformation("Successfully copied file from S3 with bucket name {SourceBucketName} and key name {SourceKeyName} to bucket name {DestinationBucketName} and key name {DestinationKeyName}", sourceBucketName, sourceKeyName, destinationBucketName, destinationKeyName); return Result.Success(); } catch (Exception exception) { logger.LogError(exception, "Failed to copy file from S3 with bucket name {SourceBucketName} and key name {SourceKeyName} to bucket name {DestinationBucketName} and key name {DestinationKeyName}", sourceBucketName, sourceKeyName, destinationBucketName, destinationKeyName); return Result.Error($"Failed to copy file from S3 with bucket name {sourceBucketName} and key name {sourceKeyName} to bucket name {destinationBucketName} and key name {destinationKeyName}"); } } } }