public bool Commit()
        {
            if (_customMetadata != null)
            {
                if (UploadOperation.customMetadataMutex.WaitOne(1000))
                {
                    try
                    {
                        _customMetadata.ToSWIG(); //Appends the customMetadata in the go-layer to a global field
                        SWIG.Error customMetadataError = SWIG.storj_uplink.upload_set_custom_metadata2(_uploadResult.upload);
                        if (customMetadataError != null && !string.IsNullOrEmpty(customMetadataError.message))
                        {
                            _errorMessage = customMetadataError.message;
                            return(false);
                        }
                        SWIG.storj_uplink.free_error(customMetadataError);
                    }
                    finally
                    {
                        UploadOperation.customMetadataMutex.ReleaseMutex();
                    }
                }
            }

            SWIG.Error commitError = SWIG.storj_uplink.upload_commit(_uploadResult.upload);
            if (commitError != null && !string.IsNullOrEmpty(commitError.message))
            {
                _errorMessage = commitError.message;
                SWIG.storj_uplink.free_error(commitError);
                return(false);
            }
            SWIG.storj_uplink.free_error(commitError);
            return(true);
        }
Esempio n. 2
0
        public async Task <ObjectList> ListObjectsAsync(Bucket bucket, ListObjectsOptions listObjectsOptions)
        {
            SWIG.ObjectIterator objectIterator = await Task.Run(() => SWIG.storj_uplink.list_objects(_access._project, bucket.Name, listObjectsOptions.ToSWIG()));

            SWIG.Error error = SWIG.storj_uplink.object_iterator_err(objectIterator);
            if (error != null && !string.IsNullOrEmpty(error.message))
            {
                throw new BucketListException(error.message);
            }
            SWIG.storj_uplink.free_error(error);

            ObjectList objectList = new ObjectList();

            while (SWIG.storj_uplink.object_iterator_next(objectIterator))
            {
                objectList.Items.Add(uplink.NET.Models.Object.FromSWIG(SWIG.storj_uplink.object_iterator_item(objectIterator), true));
            }
            SWIG.storj_uplink.free_object_iterator(objectIterator);

            return(objectList);
        }
Esempio n. 3
0
 public void Dispose()
 {
     if (_project != null)
     {
         SWIG.Error closeError = SWIG.storj_uplink.close_project(_project);
         SWIG.storj_uplink.free_error(closeError);
         _project.Dispose();
         _project = null;
     }
     if (_accessResult != null)
     {
         SWIG.storj_uplink.free_access_result(_accessResult);
         _accessResult.Dispose();
         _accessResult = null;
     }
     if (_projectResult != null)
     {
         SWIG.storj_uplink.free_project_result(_projectResult);
         _projectResult.Dispose();
         _projectResult = null;
     }
 }
Esempio n. 4
0
        public async Task <BucketList> ListBucketsAsync(ListBucketsOptions listBucketsOptions)
        {
            using (SWIG.BucketIterator bucketIterator = await Task.Run(() => SWIG.storj_uplink.list_buckets(_access._project, listBucketsOptions.ToSWIG())))
            {
                SWIG.Error error = SWIG.storj_uplink.bucket_iterator_err(bucketIterator);
                if (error != null && !string.IsNullOrEmpty(error.message))
                {
                    throw new BucketListException(error.message);
                }
                SWIG.storj_uplink.free_error(error);

                BucketList bucketList = new BucketList();

                while (SWIG.storj_uplink.bucket_iterator_next(bucketIterator))
                {
                    var bucket = SWIG.storj_uplink.bucket_iterator_item(bucketIterator);
                    bucketList.Items.Add(Bucket.FromSWIG(bucket));
                }
                SWIG.storj_uplink.free_bucket_iterator(bucketIterator);

                return(bucketList);
            }
        }
        private void DoDownload()
        {
            Running = true;
            while (BytesReceived < TotalBytes)
            {
                var tenth = _bytesToDownload.Length / 10;
                if (TotalBytes - BytesReceived > tenth)
                {
                    //Fetch next bytes in batch
                    byte[]          part       = new byte[tenth];
                    SWIG.ReadResult readResult = SWIG.storj_uplink.download_read(_downloadResult.download, new SWIG.SWIGTYPE_p_void(System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(part, 0), true), (uint)tenth);
                    if (readResult.error != null && !string.IsNullOrEmpty(readResult.error.message))
                    {
                        _errorMessage = readResult.error.message;
                        Failed        = true;
                        Running       = false;
                        DownloadOperationEnded?.Invoke(this);
                        return;
                    }
                    if (readResult.bytes_read != 0)
                    {
                        Array.Copy(part, 0, _bytesToDownload, (long)BytesReceived, readResult.bytes_read);
                        BytesReceived += readResult.bytes_read;
                    }
                    SWIG.storj_uplink.free_read_result(readResult);
                }
                else
                {
                    //Fetch only the remaining bytes
                    var    remaining = TotalBytes - BytesReceived;
                    byte[] part      = new byte[remaining];

                    SWIG.ReadResult readResult = SWIG.storj_uplink.download_read(_downloadResult.download, new SWIG.SWIGTYPE_p_void(System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(part, 0), true), (uint)remaining);

                    if (readResult.error != null && !string.IsNullOrEmpty(readResult.error.message))
                    {
                        _errorMessage = readResult.error.message;
                        Failed        = true;
                        Running       = false;
                        DownloadOperationEnded?.Invoke(this);
                        return;
                    }
                    if (readResult.bytes_read != 0)
                    {
                        Array.Copy(part, 0, _bytesToDownload, (long)BytesReceived, readResult.bytes_read);
                        BytesReceived += readResult.bytes_read;
                    }
                    SWIG.storj_uplink.free_read_result(readResult);
                }

                if (_cancelled)
                {
                    SWIG.Error cancelError = SWIG.storj_uplink.close_download(_downloadResult.download);
                    if (cancelError != null && !string.IsNullOrEmpty(cancelError.message))
                    {
                        _errorMessage = cancelError.message;
                        Failed        = true;
                    }
                    else
                    {
                        Cancelled = true;
                    }
                    SWIG.storj_uplink.free_error(cancelError);

                    Running = false;
                    DownloadOperationEnded?.Invoke(this);
                    return;
                }
                DownloadOperationProgressChanged?.Invoke(this);
                if (!string.IsNullOrEmpty(_errorMessage))
                {
                    Failed  = true;
                    Running = false;
                    DownloadOperationEnded?.Invoke(this);
                    return;
                }
            }

            SWIG.Error closeError = SWIG.storj_uplink.close_download(_downloadResult.download);

            if (closeError != null && !string.IsNullOrEmpty(closeError.message))
            {
                _errorMessage = closeError.message;
                Failed        = true;
                Running       = false;
                DownloadOperationEnded?.Invoke(this);
                SWIG.storj_uplink.free_error(closeError);
                return;
            }
            SWIG.storj_uplink.free_error(closeError);

            Completed = true;
            Running   = false;
            DownloadOperationEnded?.Invoke(this);
        }
        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)
                            {
                                SWIG.WriteResult sentResult = SWIG.storj_uplink.upload_write(_uploadResult.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;
                                }

                                SWIG.storj_uplink.free_write_result(sentResult);
                                if (_cancelled)
                                {
                                    SWIG.Error abortError = SWIG.storj_uplink.upload_abort(_uploadResult.upload);
                                    if (abortError != null && !string.IsNullOrEmpty(abortError.message))
                                    {
                                        Failed        = true;
                                        _errorMessage = abortError.message;
                                    }
                                    else
                                    {
                                        Cancelled = true;
                                    }
                                    SWIG.storj_uplink.free_error(abortError);

                                    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
                                SWIG.Error customMetadataError = SWIG.storj_uplink.upload_set_custom_metadata2(_uploadResult.upload);
                                if (customMetadataError != null && !string.IsNullOrEmpty(customMetadataError.message))
                                {
                                    _errorMessage = customMetadataError.message;
                                    Failed        = true;
                                    UploadOperationEnded?.Invoke(this);
                                    return;
                                }
                                SWIG.storj_uplink.free_error(customMetadataError);
                            }
                            finally
                            {
                                customMetadataMutex.ReleaseMutex();
                            }
                        }
                    }

                    SWIG.Error commitError = SWIG.storj_uplink.upload_commit(_uploadResult.upload);
                    if (commitError != null && !string.IsNullOrEmpty(commitError.message))
                    {
                        _errorMessage = commitError.message;
                        Failed        = true;
                        UploadOperationEnded?.Invoke(this);
                        SWIG.storj_uplink.free_error(commitError);
                        return;
                    }
                    SWIG.storj_uplink.free_error(commitError);
                }
                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);
            }
        }