Example #1
0
        public async Task<StorageFile> UploadArtifactFromStreamAsync(StorageFile file, CancellationToken cancellationToken = new CancellationToken())
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (file.Stream == null)
            {
                throw new ArgumentNullException("file", "Stream value should be defined.");
            }

            if (string.IsNullOrEmpty(file.UserId))
            {
                throw new ArgumentNullException("file", "UserId value should be defined.");
            }

            // Adds file to storage
            return await AddFileToStorageFromStreamAsync(file, true, cancellationToken);
        }
Example #2
0
        public async Task<StorageFile> DownloadFileToStreamAsync(StorageFile file, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (string.IsNullOrEmpty(file.Id))
            {
                throw new ArgumentNullException("file", "Id value should be defined.");
            }

            if (file.Stream == null)
            {
                throw new ArgumentNullException("file", "Stream value should be defined.");
            }

            // Receive table entity
            FileEntity storageFile = await _fileRepository.GetAsync(file.Id);

            // Get blob reference
            CloudBlockBlob blob = _blobClient.GetContainerReference(storageFile.Id).GetBlockBlobReference(BlobName);

            // Download blob to stream
            await blob.DownloadToStreamAsync(file.Stream, cancellationToken);

            return new StorageFile
            {
                ContentType = storageFile.ContentType,
                Created = storageFile.Created,
                FileName = storageFile.Name,
                Id = storageFile.Id,
                Length = storageFile.Length,
                Modified = storageFile.Modified,
                UserId = storageFile.UserId,
                Uri = blob.Uri
            };
        }
Example #3
0
        public async Task<StorageFile> UploadFileFromStreamAsync(StorageFile file, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (file.Stream == null)
            {
                throw new ArgumentNullException("file", "Stream value should be defined.");
            }

            if (string.IsNullOrEmpty(file.UserId))
            {
                throw new ArgumentNullException("file", "UserId value should be defined.");
            }

            // Checks user storage space
            await CheckUserSpaceAsync(file);

            // Adds file to storage
            return await AddFileToStorageFromStreamAsync(file, false, cancellationToken);
        }
Example #4
0
        public void ExecuteUploadStepTest()
        {
            //Arrange
            var fileStream = new MemoryStream();

            _fileWrapper.Setup(m => m.OpenRead(LocalFileUri)).Returns(fileStream);
            _fileSystem.Setup(m => m.UploadArtifactFromStreamAsync(It.Is<StorageFile>(s => s.UserId == UserId &&
                                                                                            s.ContentType == ContentType &&
                                                                                            s.Stream == fileStream),
                                                                    It.IsAny<CancellationToken>()))
                            .Returns(() =>
                                {
                                    var tcs = new TaskCompletionSource<StorageFile>();
                                    var storageFile = new StorageFile() {Hash = FileHash};
                                    tcs.SetResult(storageFile);
                                    return tcs.Task;
                                });

            //Act
            _pipelineStep.Execute(_tokenSource.Object);

            //Assert
            _pipelineMediator.Verify(m => m.Send(It.Is<UploadStepData>(d => d.FileHash == FileHash &&
                                                                            d.EncoderState == EncoderState.Completed), _pipelineStep),
                                     Times.Once());
        }
Example #5
0
 public Task DeleteFileAsync(StorageFile file, CancellationToken cancellationToken = new CancellationToken())
 {
     throw new NotImplementedException();
 }
Example #6
0
 public Task<StorageFile> GetFilePropertiesAsync(StorageFile file, CancellationToken cancellationToken = new CancellationToken())
 {
     throw new NotImplementedException();
 }
Example #7
0
 public Task<StorageFile> DownloadFileToStreamAsync(StorageFile file, CancellationToken cancellationToken = new CancellationToken())
 {
     throw new NotImplementedException();
 }
Example #8
0
 public Task<StorageFile> UploadArtifactFromStreamAsync(StorageFile file, CancellationToken cancellationToken = new CancellationToken())
 {
     throw new NotImplementedException();
 }
Example #9
0
 public Task<StorageFile> AddFileByHashAsync(StorageFile file, CancellationToken cancellationToken = new CancellationToken())
 {
     throw new NotImplementedException();
 }
Example #10
0
        /// <summary>
        ///     Uploads file to storage from stream.
        /// </summary>
        /// <param name="file">Storage file.</param>
        /// <param name="isArtifact">Defines whether file is artifact.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>Result file.</returns>
        private async Task<StorageFile> AddFileToStorageFromStreamAsync(StorageFile file, bool isArtifact, CancellationToken cancellationToken)
        {
            // Result storage file
            var storageFile = new FileEntity
            {
                ContentType = file.ContentType,
                Created = DateTime.UtcNow,
                Modified = DateTime.UtcNow,
                Length = file.Length,
                Name = file.FileName,
                UserId = file.UserId,
                IsArtifact = isArtifact
            };

            // Create table entity
            storageFile = await _fileRepository.AddAsync(storageFile);

            // Create container
            CloudBlobContainer container = _blobClient.GetContainerReference(storageFile.Id);
            await TryCreateContainer(container, cancellationToken);
            await container.SetPermissionsAsync(new BlobContainerPermissions
            {
                PublicAccess = BlobContainerPublicAccessType.Blob
            }, cancellationToken);

            // Create blob
            CloudBlockBlob blob = container.GetBlockBlobReference(BlobName);
            if (await blob.ExistsAsync(cancellationToken))
            {
                throw new ConflictException(string.Format("File with id {0} is already exists in Blob storage", storageFile.Id));
            }

            // Upload data
            blob.UploadFromStream(file.Stream);

            // Set properties
            await blob.FetchAttributesAsync(cancellationToken);
            blob.Properties.ContentType = file.ContentType;
            blob.Properties.CacheControl = CacheControl;
            await blob.SetPropertiesAsync(cancellationToken);

            return new StorageFile
            {
                ContentType = storageFile.ContentType,
                Created = storageFile.Created,
                FileName = storageFile.Name,
                Id = storageFile.Id,
                Length = storageFile.Length,
                Modified = storageFile.Modified,
                UserId = storageFile.UserId,
                Uri = blob.Uri
            };
        }
Example #11
0
        /// <summary>
        ///     Checks user storage space.
        /// </summary>
        /// <param name="file">Storage file.</param>
        private async Task CheckUserSpaceAsync(StorageFile file)
        {
            // Checks user storage space
            List<FileEntity> storageSpace = await _fileRepository.ToListAsync(p => p.UserId == file.UserId);
            long currentSpace = storageSpace.Where(p => !p.IsArtifact).Sum(p => p.Length);

            UserEntity userProfile = await _userRepository.GetAsync(file.UserId);

            if (currentSpace + file.Length > userProfile.MaximumStorageSpace)
            {
                throw new EntityTooLargeException();
            }
        }
Example #12
0
        public async Task DeleteFileAsync(StorageFile file, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (string.IsNullOrEmpty(file.Id))
            {
                throw new ArgumentNullException("file", "Id value should be defined.");
            }

            if (string.IsNullOrEmpty(file.UserId))
            {
                throw new ArgumentNullException("file", "UserId value should be defined.");
            }

            // Receive table entity
            FileEntity storageFile = await _fileRepository.GetAsync(file.Id);
            if (storageFile == null)
            {
                throw new NotFoundException(string.Format("File {0} could not be found in storage", file.Id));
            }

            // Delete storage file & storage space entities
            await _fileRepository.DeleteAsync(storageFile);

            // Remove file from blob
            CloudBlobContainer container = _blobClient.GetContainerReference(storageFile.Id);
            await container.DeleteIfExistsAsync(cancellationToken);
        }
Example #13
0
        public async Task<StorageFile> GetFilePropertiesSlimAsync(StorageFile file, CancellationToken cancellationToken = new CancellationToken())
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (string.IsNullOrEmpty(file.Id))
            {
                throw new ArgumentNullException("file", "Id value should be defined.");
            }

            // Receive table entity
            FileEntity storageFile = await _fileRepository.GetAsync(file.Id);

            return new StorageFile
            {
                ContentType = storageFile.ContentType,
                Created = storageFile.Created,
                FileName = storageFile.Name,
                Id = storageFile.Id,
                Length = storageFile.Length,
                Modified = storageFile.Modified,
                UserId = storageFile.UserId,
                Uri = null
            };
        }
Example #14
0
        public async Task<StorageFile> GetFilePropertiesAsync(StorageFile file, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (string.IsNullOrEmpty(file.Id))
            {
                throw new ArgumentNullException("file", "Id value should be defined.");
            }

            // Receive table entity
            FileEntity storageFile = await _fileRepository.GetAsync(file.Id);
            if (storageFile == null)
            {
                throw new NotFoundException(string.Format("File {0} could not be found in storage", file.Id));
            }

            // Get blob reference
            CloudBlockBlob blob = _blobClient.GetContainerReference(storageFile.Id).GetBlockBlobReference(BlobName);

            await BlobIsExists(blob, storageFile.Id);

            return new StorageFile
            {
                ContentType = storageFile.ContentType,
                Created = storageFile.Created,
                FileName = storageFile.Name,
                Id = storageFile.Id,
                Length = storageFile.Length,
                Modified = storageFile.Modified,
                UserId = storageFile.UserId,
                Uri = blob.Uri
            };
        }