Example #1
0
        public async Task <Stream> Open(string relativePath)
        {
            if (!await _fileStorageProvider.Exists(relativePath))
            {
                throw new HopexException("file not exists.");
            }

            return(await _fileStorageProvider.Open(relativePath));
        }
Example #2
0
        private void CreateFile(INFile file)
        {
            long pos = 0;

            if (file.Size > _minResumeUploadFileSize)
            {
                //опросить сервер о состоянии этого файла
                pos = _fileArchiveApi.GetFilePosition(file.Id);
                if (pos > file.Size)
                {
                    throw new Exception(string.Format("File with id {0} is corrupted", file.Id));
                }
            }

            // учтем в прогрессе уже загруженные данные
            _uploaded += pos;

            //отправим тело на сервер
            using (var fs = _storageProvider.Open(file.Id))
            {
                if (file.Size != fs.Length)
                {
                    throw new Exception(string.Format("Local file size is incorrect: {0}", file.Id));
                }

                const int MAX_ATTEMPT_COUNT = 5;
                int       attemptCount      = 0;
                bool      succeed           = false;
                do
                {
                    UploadData(fs, file.Id, pos);
                    try
                    {
                        _fileArchiveApi.PutFileInArchive(file.Dto.Body);
                        succeed = true;
                    }
                    catch (Exception e)
                    {
                        _logger.Error("при загрузке файла произошла ошибка", e);
                        pos       = 0;
                        _uploaded = 0;
                    }
                    attemptCount++;
                } while (!succeed && attemptCount < MAX_ATTEMPT_COUNT);

                if (!succeed)
                {
                    throw new PilotException(string.Format("Unable to upload file {0}", file.Id));
                }
            }
        }