Beispiel #1
0
        private async Task DoUploadFileBlock(FileBlockInfo fileBlockInfo, Stream fileContent)
        {
            var filename = GetFileBlockName(fileBlockInfo);
            var filepath = Path.Combine(_fileMetadata.Location, filename);

            try
            {
                using (var fileStream = File.Create(filepath))
                {
                    await fileContent.CopyToAsync(fileStream);

                    await fileStream.FlushAsync();
                }
            }
            catch (DirectoryNotFoundException)
            {
                // retry by first attempting to create the directory
                Directory.CreateDirectory(_fileMetadata.Location);

                using (var fileStream = File.Create(filepath))
                {
                    await fileContent.CopyToAsync(fileStream);

                    await fileStream.FlushAsync();
                }
            }
        }
Beispiel #2
0
        public async Task UploadFileBlockAsync(FileBlockInfo fileBlockInfo, Stream fileContent)
        {
            await Initialize(fileBlockInfo);

            while (_initialized == 0)
            {
                Thread.Sleep(50);
            }

            await DoUploadFileBlockAsync(fileBlockInfo, fileContent);
        }
Beispiel #3
0
        private async Task DoUploadFileBlockAsync(FileBlockInfo fileBlockInfo, Stream fileContent)
        {
            if (_blocksCount == 1)
            {
                await _blockBlobReference.UploadFromStreamAsync(fileContent);

                return;
            }

            var blockId = ToBase64(fileBlockInfo.SequenceNum);

            await _blockBlobReference.PutBlockAsync(blockId, fileContent, null);
        }
        public async Task AddFileBlockAsync(IServiceProvider serviceProvider, FileBlockInfo fileBlockInfo, Stream stream, bool sendToAzure)
        {
            _uploaders.TryGetValue(fileBlockInfo.FileId, out var uploader);

            if (uploader == null)
            {
                await AddOrInitializeUploadAsync(serviceProvider, fileBlockInfo, stream, sendToAzure);

                return;
            }

            await uploader.UploadFileBlockAsync(fileBlockInfo, stream);
        }
Beispiel #5
0
        private async Task Initialize(FileBlockInfo fileBlockInfo)
        {
            if (0 == Interlocked.Exchange(ref _initializing, 1))
            {
                try
                {
                    _fileMetadata.Id            = Guid.NewGuid();
                    _fileMetadata.FileName      = fileBlockInfo.FileName;
                    _fileMetadata.FileSize      = fileBlockInfo.FileSize;
                    _fileMetadata.Location      = _fileMetadata.Id.ToString();
                    _fileMetadata.CreateDateUtc = DateTime.UtcNow;
                    _fileMetadata.Store         = FileStore.Azure;

                    _blocksCount = fileBlockInfo.TotalBlocksCount;

                    _blockBlobReference = await _azureAccountManager.GetBlobReferenceAsync(_fileMetadata);
                }
                finally
                {
                    Interlocked.Exchange(ref _initialized, 1);
                }
            }
        }
Beispiel #6
0
        private void Initialize(FileBlockInfo fileBlockInfo)
        {
            if (0 == Interlocked.Exchange(ref _initializing, 1))
            {
                try
                {
                    _fileMetadata.Id            = Guid.NewGuid();
                    _fileMetadata.FileName      = fileBlockInfo.FileName;
                    _fileMetadata.FileSize      = fileBlockInfo.FileSize;
                    _fileMetadata.Location      = Path.Combine(_storageDirectory, _fileMetadata.Id.ToString());
                    _fileMetadata.CreateDateUtc = DateTime.UtcNow;
                    _fileMetadata.Store         = FileStore.FileSystem;

                    if (!Directory.Exists(_fileMetadata.Location))
                    {
                        Directory.CreateDirectory(_fileMetadata.Location);
                    }
                }
                finally
                {
                    Interlocked.Exchange(ref _initialized, 1);
                }
            }
        }
Beispiel #7
0
 private string GetFileBlockName(FileBlockInfo fileBlockInfo)
 {
     return($"{fileBlockInfo.SequenceNum.ToString().PadLeft(15, '0')}{FileBlockExtension}");
 }
        public async Task AddOrInitializeUploadAsync(IServiceProvider serviceProvider, FileBlockInfo fileBlockInfo, Stream stream, bool sendToAzure)
        {
            var uploader = _uploaders.GetOrAdd(fileBlockInfo.FileId, key =>
            {
                if (sendToAzure)
                {
                    return(new AzureFileUploader(_configuration, _azureAccountManager));
                }

                return(new FileUploader(_configuration));
            });

            await uploader.UploadFileBlockAsync(fileBlockInfo, stream);
        }