private void UploadFull(Stream sourceStream, bool doInvokeFileStreamSent = true)
        {
            Logger.Debug($"Uploading [direct] {_file.FullPath}");

            var pushContent = new PushStreamContent((stream, httpContent, arg3) =>
            {
                try
                {
                    sourceStream.CopyTo(stream);
                    stream.Flush();
                    stream.Close();
                    if (doInvokeFileStreamSent)
                    {
                        OnFileStreamSent();
                    }
                }
                catch (Exception e)
                {
                    Logger.Error($"(inner) Uploading to {_file.FullPath} failed with {e}");
                    throw;
                }
            });

            var client           = HttpClientFabric.Instance[_cloud.Account];
            var uploadFileResult = Repo.DoUpload(client, pushContent, _file).Result;


            if (uploadFileResult.HttpStatusCode != HttpStatusCode.Created &&
                uploadFileResult.HttpStatusCode != HttpStatusCode.OK)
            {
                throw new Exception("Cannot upload file, status " + uploadFileResult.HttpStatusCode);
            }

            // 2020-10-26 mairu does not return file size now
            //if (uploadFileResult.HasReturnedData && _file.OriginalSize != uploadFileResult.Size)
            //    throw new Exception("Local and remote file size does not match");

            if (uploadFileResult.HasReturnedData && CheckHashes && null != uploadFileResult.Hash &&
                _cloudFileHasher != null && _cloudFileHasher.Hash.Hash.Value != uploadFileResult.Hash.Hash.Value)
            {
                throw new HashMatchException(_cloudFileHasher.Hash.ToString(), uploadFileResult.Hash.ToString());
            }

            if (uploadFileResult.HasReturnedData)
            {
                _file.Hash = uploadFileResult.Hash;
            }

            if (uploadFileResult.NeedToAddFile)
            {
                _cloud.AddFileInCloud(_file, ConflictResolver.Rewrite)
                .Result
                .ThrowIf(r => !r.Success, r => new Exception($"Cannot add file {_file.FullPath}"));
            }
        }
        private void Initialize()
        {
            _requestTask = Task.Run(() =>
            {
                try
                {
                    if (Repo.SupportsAddSmallFileByHash && _file.OriginalSize <= _cloudFileHasher.Length) // do not send upload request if file content fits to hash
                    {
                        using (var ms = new MemoryStream())
                        {
                            _ringBuffer.CopyTo(ms);
                        }
                        return;
                    }

                    _pushContent = new PushStreamContent((stream, httpContent, arg3) =>
                    {
                        try
                        {
                            _ringBuffer.CopyTo(stream);
                            stream.Close();
                        }
                        catch (Exception e)
                        {
                            Logger.Error($"(inner) Uploading to {_file.FullPath} failed with {e.Message}");
                            throw;
                        }
                    });

                    _client           = HttpClientFabric.Instance[_cloud.Account];
                    _uploadFileResult = Repo.DoUpload(_client, _pushContent, _file).Result;

                    //_request = Repo.UploadClientRequest(_pushContent, _file);
                    //_client = HttpClientFabric.Instance[_cloud.Account];
                    //_responseMessage = _client.SendAsync(_request).Result;
                }
                catch (Exception e)
                {
                    Logger.Error($"Uploading to {_file.FullPath} failed with {e.Message}"); //TODO remove duplicate exception catch?
                    throw;
                }
            });
        }
Example #3
0
 public PushStreamContent(Action <Stream, HttpContent, TransportContext> onStreamAvailable, string mediaType)
     : this(PushStreamContent.Taskify(onStreamAvailable), new MediaTypeHeaderValue(mediaType))
 {
 }