public Task <FileRepositoryOperationResult <bool> > RemoveFile(string filePath, dynamic metadata) { _logger.LogInformation($"Removing resource from path {filePath}"); if (string.IsNullOrWhiteSpace(filePath)) { throw new ArgumentNullException(nameof(filePath)); } try { filePath = NormalizeFilePath(filePath); if (!File.Exists(filePath)) { return(Task.FromResult(FileRepositoryOperationResult <bool> .Error("File not found"))); } File.Delete(filePath); return(Task.FromResult(FileRepositoryOperationResult <bool> .Success(true))); } catch (Exception ex) { _logger.LogError($"{ex.GetType().Name}: {ex.Message}"); return(Task.FromResult(FileRepositoryOperationResult <bool> .Error(ex.Message))); } }
public async Task <FileRepositoryOperationResult <bool> > PersistFile(string filePath, Stream fileContent, dynamic metadata) { _logger.LogInformation($"Persisting resource to path {filePath}"); if (string.IsNullOrWhiteSpace(filePath)) { throw new ArgumentNullException(nameof(filePath)); } if (fileContent == null) { throw new ArgumentNullException(nameof(fileContent)); } try { filePath = NormalizeFilePath(filePath); Directory.CreateDirectory(Path.GetDirectoryName(filePath)); await using var fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write); fileContent.Seek(0, SeekOrigin.Begin); await fileContent.CopyToAsync(fileStream); await fileStream.FlushAsync(); } catch (Exception ex) { _logger.LogError($"{ex.GetType().Name}: {ex.Message}"); return(FileRepositoryOperationResult <bool> .Error(ex.Message)); } return(FileRepositoryOperationResult <bool> .Success(true)); }
public async Task <FileRepositoryOperationResult <bool> > RemoveFile(string filePath, dynamic metadata) { _logger.LogInformation($"Removing resource from path {filePath}"); filePath = NormalizeFilePath(filePath); using var client = _amazonS3ClientFactory.GetClient(); var request = new DeleteObjectRequest { BucketName = _awsS3Settings.BucketName, Key = filePath }; var response = await client.DeleteObjectAsync(request); var success = response.HttpStatusCode == System.Net.HttpStatusCode.OK; return(FileRepositoryOperationResult <bool> .Success(success)); }
public async Task <FileRepositoryOperationResult <bool> > PersistFile(string filePath, Stream fileContent, dynamic metadata) { _logger.LogInformation($"Persisting resource to path {filePath}"); filePath = NormalizeFilePath(filePath); try { using var transferUtility = _amazonS3ClientFactory.GetTransferUtility(); await transferUtility.UploadAsync(fileContent, _awsS3Settings.BucketName, filePath); return(FileRepositoryOperationResult <bool> .Success(true)); } catch (Exception ex) { _logger.LogError($"{ex.GetType().Name}: {ex.Message}"); return(FileRepositoryOperationResult <bool> .Error(ex.Message)); } }