public string Download(INFile inFile) { var filePath = GetTempPath(inFile.Modified.ToFileTimeUtc() + "_" + inFile.Id); if (File.Exists(filePath)) { File.Delete(filePath); } using (BinaryWriter file = new BinaryWriter(File.OpenWrite(filePath))) { var filePos = _fileArchiveApi.GetFilePosition(inFile.Id); long fileSize = inFile.Size; while (fileSize > 0) { int chunkSize = fileSize > CHUNK_SIZE ? CHUNK_SIZE : (int)fileSize; var data = _fileArchiveApi.GetFileChunk(inFile.Id, filePos + inFile.Size - fileSize, chunkSize); file.Write(data); fileSize -= chunkSize; } file.Close(); } return(filePath); }
private void CreateFile(DFile file) { Logger.InfoFormat("Uploading file {0}", file.Body.Id); long pos = 0; if (file.Body.Size > MinResumeUploadFileSize) { //get file position from server pos = _fileArchiveApi.GetFilePosition(file.Body.Id); if (pos > file.Body.Size) { throw new Exception($"File with id {file.Body.Id} is corrupted"); } } // change progress _uploaded += pos; //send file body to server _fileStream.Position = 0; if (file.Body.Size != _fileStream.Length) { throw new Exception($"Local file size is incorrect: {file.Body.Id}"); } var fileBody = file.Body; const int maxAttemptCount = 5; int attemptCount = 0; bool succeed = false; do { UploadData(_fileStream, file.Body.Id, pos); try { _fileArchiveApi.PutFileInArchive(fileBody); succeed = true; } catch (Exception e) { Logger.Error("Error on uploading file {0}", e); pos = 0; _uploaded = 0; } attemptCount++; } while (!succeed && attemptCount < maxAttemptCount); if (!succeed) { throw new Exception($"Unable to upload file {file.Body.Id}"); } }
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)); } } }
public long GetFilePosition(string databaseName, Guid id) { return(_api.GetFilePosition(_database, id)); }