/// <summary>
        /// Creates the container if it does not yet exist in the storage account
        /// </summary>
        /// <param name="container"></param>
        /// <returns></returns>
        public async Task <RepositoryResult> EnsureContainerCreated(string container)
        {
            await _blobClient.GetBlobContainerClient(container)
            .CreateIfNotExistsAsync();

            return(RepositoryResult.Success());
        }
Beispiel #2
0
        /// <summary>
        /// Checks if the file exists
        /// </summary>
        /// <param name="fileDate"></param>
        /// <param name="container"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public Task <RepositoryResult <bool> > CheckIfFileExistsAsync(DateTime fileDate, string container, string fileName)
        {
            var fileExists = _savedFiles
                             .Any(f => f.Container == container &&
                                  f.FileName == fileName);

            return(Task.FromResult(RepositoryResult <bool> .Success(fileExists)));
        }
            public void ReturnsSuccessOnSuccess()
            {
                var result = RepositoryResult <string> .Success("Hello");

                Assert.True(result.IsSuccess);
                Assert.Null(result.ErrorMessage);
                Assert.Equal("Hello", result.Value);
            }
Beispiel #4
0
        /// <summary>
        /// Deletes the file
        /// </summary>
        /// <param name="fileDate"></param>
        /// <param name="container"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public Task <RepositoryResult> DeleteFileAsync(DateTime fileDate, string container, string fileName)
        {
            var file = _savedFiles.FirstOrDefault(f => f.Container == container &&
                                                  f.FileName == fileName);

            if (file != null)
            {
                _savedFiles.Remove(file);
            }
            return(Task.FromResult(RepositoryResult.Success()));
        }
Beispiel #5
0
 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> 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 <bool> > InternalCheckIfFileExistsAsync(BlobClient blobReference)
        {
            try
            {
                var blobExists = await blobReference.ExistsAsync();

                return(RepositoryResult <bool> .Success(blobExists));
            }
            catch (Exception e)
            {
                return(RepositoryResult <bool> .Fail(e.ToString()));
            }
        }
        /// <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());
            }
        }
        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="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());
            }
        }
Beispiel #11
0
        /// <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")));
        }
Beispiel #12
0
        /// <summary>
        /// Caches a file to memory
        /// </summary>
        /// <param name="fileId"></param>
        /// <param name="container"></param>
        /// <param name="fileName"></param>
        /// <param name="fileStream"></param>
        /// <returns></returns>
        public async Task <RepositoryResult> SaveFileAsync(Guid fileId, string container, string fileName, Stream fileStream)
        {
            // Copying it internally because the original stream is likely to be disposed
            var copiedMemoryStream = new MemoryStream();
            await fileStream.CopyToAsync(copiedMemoryStream);

            copiedMemoryStream.Position = 0;

            _savedFiles.Add(new InMemorySavedFile
            {
                FileId     = fileId,
                Container  = container,
                FileName   = fileName,
                FileStream = copiedMemoryStream
            });

            return(RepositoryResult.Success());
        }
Beispiel #13
0
        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())));
            }
        }
Beispiel #14
0
        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()));
            }
        }
Beispiel #16
0
        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()));
            }
        }
Beispiel #17
0
            public void ReturnsSuccessPrimitiveType()
            {
                var result = RepositoryResult <int, object> .Success(5);

                Assert.Equal(5, result.Value);
            }