public bool WriteBytes(byte[] buffer) { uint bytesSent = 0; while (bytesSent != buffer.Length) { var uploadChunk = buffer.Take((int)(buffer.Length - bytesSent)).ToArray(); fixed(byte *arrayPtr = uploadChunk) { using (SWIG.UplinkWriteResult sentResult = SWIG.storj_uplink.uplink_upload_write(_upload, new SWIG.SWIGTYPE_p_void(new IntPtr(arrayPtr), true), (uint)uploadChunk.Length)) { if (sentResult.error != null && !string.IsNullOrEmpty(sentResult.error.message)) { _errorMessage = sentResult.error.message; return(false); } else { bytesSent += sentResult.bytes_written; } } } } return(true); }
private unsafe uint DoUnsafeUpload(SWIG.UplinkPartUpload partUpload, string objectKey, byte[] partBytes) { fixed(byte *arrayPtr = partBytes) { using (SWIG.UplinkWriteResult sentResult = SWIG.storj_uplink.uplink_part_upload_write(partUpload, new SWIG.SWIGTYPE_p_void(new IntPtr(arrayPtr), true), (uint)partBytes.Length)) { if (sentResult.error != null && !string.IsNullOrEmpty(sentResult.error.message)) { throw new MultipartUploadFailedException(objectKey, sentResult.error.message); } using (var commitResult = SWIG.storj_uplink.uplink_part_upload_commit(partUpload)) { if (commitResult != null && !string.IsNullOrEmpty(commitResult.message)) { throw new MultipartUploadFailedException(objectKey, commitResult.message); } return(sentResult.bytes_written); } } } }
private void DoUpload() { try { if (_byteStreamToUpload != null) { Running = true; int bytesToUploadCount = 0; do { byte[] bytesToUpload = new byte[262144]; bytesToUploadCount = _byteStreamToUpload.Read(bytesToUpload, 0, 262144); if (bytesToUploadCount > 0) { byte[] targetArray = bytesToUpload.Take((int)bytesToUploadCount).ToArray(); fixed(byte *arrayPtr = targetArray) { using (SWIG.UplinkWriteResult sentResult = SWIG.storj_uplink.uplink_upload_write(_upload, new SWIG.SWIGTYPE_p_void(new IntPtr(arrayPtr), true), (uint)bytesToUploadCount)) { if (sentResult.error != null && !string.IsNullOrEmpty(sentResult.error.message)) { _errorMessage = sentResult.error.message; Failed = true; Running = false; UploadOperationEnded?.Invoke(this); return; } else { BytesSent += sentResult.bytes_written; } } if (_cancelled) { using (SWIG.UplinkError abortError = SWIG.storj_uplink.uplink_upload_abort(_upload)) { if (abortError != null && !string.IsNullOrEmpty(abortError.message)) { Failed = true; _errorMessage = abortError.message; } else { Cancelled = true; } } Running = false; UploadOperationEnded?.Invoke(this); return; } UploadOperationProgressChanged?.Invoke(this); if (!string.IsNullOrEmpty(_errorMessage)) { Failed = true; UploadOperationEnded?.Invoke(this); return; } } } } while (bytesToUploadCount > 0); if (_customMetadata != null) { if (customMetadataMutex.WaitOne(1000)) { try { _customMetadata.ToSWIG(); //Appends the customMetadata in the go-layer to a global field using (SWIG.UplinkError customMetadataError = SWIG.storj_uplink.upload_set_custom_metadata2(_upload)) { if (customMetadataError != null && !string.IsNullOrEmpty(customMetadataError.message)) { _errorMessage = customMetadataError.message; Failed = true; UploadOperationEnded?.Invoke(this); return; } } } finally { customMetadataMutex.ReleaseMutex(); } } } using (SWIG.UplinkError commitError = SWIG.storj_uplink.uplink_upload_commit(_upload)) { if (commitError != null && !string.IsNullOrEmpty(commitError.message)) { _errorMessage = commitError.message; Failed = true; UploadOperationEnded?.Invoke(this); return; } } } if (!string.IsNullOrEmpty(_errorMessage)) { Failed = true; UploadOperationEnded?.Invoke(this); return; } Completed = true; UploadOperationEnded?.Invoke(this); } catch (Exception ex) { Failed = true; _errorMessage = ex.Message; return; } finally { Running = false; UploadOperationEnded?.Invoke(this); } }