private Task <RepositoryResult> DeleteFileAsync(string absolutePath) { try { File.Delete(absolutePath); return(Task.FromResult(RepositoryResult.Success())); } catch { return(Task.FromResult(RepositoryResult.Fail())); } }
private async Task <RepositoryResult <bool> > InternalCheckIfFileExistsAsync(BlobClient blobReference) { try { var blobExists = await blobReference.ExistsAsync(); return(RepositoryResult <bool> .Success(blobExists)); } catch (Exception e) { return(RepositoryResult <bool> .Fail(e.ToString())); } }
private async Task <RepositoryResult> UploadBlobReference(BlobClient blobReference, Stream fileStream) { try { await blobReference.UploadAsync(fileStream); return(RepositoryResult.Success()); } catch (Exception e) { return(RepositoryResult.Fail(e.ToString())); } }
private async Task <RepositoryResult <SasDownloadLink> > GetSasDownloadLinkInternalAsync(string filePath, string container, int validForMinutes, string friendlyFileName) { var sasUploadLink = await GeSasLinkInternalAsync(filePath, container, validForMinutes, BlobSasPermissions.Read, friendlyFileName); if (!sasUploadLink.IsSuccess) { return(RepositoryResult <SasDownloadLink> .Fail(sasUploadLink.ErrorMessage)); } return(RepositoryResult <SasDownloadLink> .Success(new SasDownloadLink { DownloadLink = sasUploadLink.Value.link, ValidUntil = sasUploadLink.Value.validUntil })); }
/// <summary> /// Deletes the file /// </summary> /// <param name="fileDate"></param> /// <param name="container"></param> /// <param name="fileName"></param> /// <returns></returns> public async Task <RepositoryResult> DeleteFileAsync(DateTime fileDate, string container, string fileName) { var blobReference = GetTimeStampedBlobReference(fileDate, container, fileName); try { await blobReference.DeleteAsync(); return(RepositoryResult.Success()); } catch { return(RepositoryResult.Fail()); } }
/// <summary> /// Deletes the file /// </summary> /// <param name="fileId"></param> /// <param name="container"></param> /// <param name="fileName"></param> /// <returns></returns> public async Task <RepositoryResult> DeleteFileAsync(Guid fileId, string container, string fileName) { var blobReference = GetBlobReference(fileId, container, fileName); try { await blobReference.DeleteAsync(); return(RepositoryResult.Success()); } catch { return(RepositoryResult.Fail()); } }
/// <summary> /// Returns a cached file /// </summary> /// <param name="container"></param> /// <param name="fileName"></param> /// <returns></returns> public Task <RepositoryResult <Stream> > GetFileAsync(string container, string fileName) { var file = _savedFiles .Find(f => f.Container == container && f.FileName == fileName); if (file != null) { // We're copying and returning a new stream, since the caller // might dispose the returned value, but we need to ensure that // we can always again return a given file return(Task.FromResult(RepositoryResult <Stream> .Success(file.FileStream.Copy()))); } return(Task.FromResult(RepositoryResult <Stream> .Fail("File not found"))); }
private Task <RepositoryResult <bool> > InternalCheckIfFileExistsAsync(Guid?fileId, string container, string fileName) { var fileSavePath = GetFilePath(fileId, container, fileName); try { if (!File.Exists(fileSavePath)) { return(Task.FromResult(RepositoryResult <bool> .Success(false))); } return(Task.FromResult(RepositoryResult <bool> .Success(true))); } catch (Exception e) { return(Task.FromResult(RepositoryResult <bool> .Fail(e.ToString()))); } }
private Task <RepositoryResult <Stream> > InternalGetFileAsync(Guid?fileId, string container, string fileName) { var fileSavePath = GetFilePath(fileId, container, fileName); try { if (!File.Exists(fileSavePath)) { return(Task.FromResult(RepositoryResult <Stream> .Fail("The file does not exist"))); } var fileStream = File.Open(fileSavePath, FileMode.Open, FileAccess.Read, FileShare.Read); return(Task.FromResult(RepositoryResult <Stream> .Success(fileStream))); } catch (Exception e) { return(Task.FromResult(RepositoryResult <Stream> .Fail(e.ToString()))); } }
private async Task <RepositoryResult <Stream> > InternalGetFileAsync(Guid?fileId, string container, string fileName) { var blobReference = fileId == null ? GetBlobReference(container, fileName) : GetBlobReference((Guid)fileId, container, fileName); try { var memoryStream = new MemoryStream(); var blobResponse = await blobReference.DownloadAsync(); await blobResponse.Value.Content.CopyToAsync(memoryStream); memoryStream.Position = 0; return(RepositoryResult <Stream> .Success(memoryStream)); } catch (Exception e) { return(RepositoryResult <Stream> .Fail(e.ToString())); } }
private async Task <RepositoryResult> SaveFileToDiskAsync(string absoluteFilePath, Stream fileStream) { try { var path = Path.GetDirectoryName(absoluteFilePath); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } using (var fs = File.Create(absoluteFilePath)) { await fileStream.CopyToAsync(fs).ConfigureAwait(false); } return(RepositoryResult.Success()); } catch (Exception e) { return(RepositoryResult.Fail(e.ToString())); } }
public void ReturnsDefaultForNonNullableTypeOnFail() { var resultInt = RepositoryResult <int, string> .Fail("Error data"); Assert.Equal(default, resultInt.Value);
public void ReturnsErrorPrimitiveType() { var result = RepositoryResult <int, int> .Fail(5); Assert.Equal(5, result.Error); }