Skip to content
This repository has been archived by the owner on Sep 2, 2019. It is now read-only.

Commit

Permalink
Upgrade to java-libstorj 0.8.1 - First fully working version!
Browse files Browse the repository at this point in the history
  • Loading branch information
TopperDEL committed Mar 22, 2018
1 parent 9d3d572 commit 1410e89
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 63 deletions.
4 changes: 2 additions & 2 deletions LibStorj.Wrapper.Contracts/Interfaces/IStorj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface IStorj
DownloadJob DownloadFile(File file, string localPath);
DownloadJob DownloadFile(Bucket bucket, string fileId, string localPath);
DownloadJob DownloadFile(string bucketId, string fileId, string localPath);
Task<File> UploadFile(Bucket bucket, string fileName, string localPath, IProgress<ProgressStatusUpload> progress = null);
Task<File> UploadFile(string bucketId, string fileName, string localPath, IProgress<ProgressStatusUpload> progress = null);
UploadJob UploadFile(Bucket bucket, string fileName, string localPath);
UploadJob UploadFile(string bucketId, string fileName, string localPath);
}
}
7 changes: 3 additions & 4 deletions LibStorj.Wrapper.Contracts/Models/DownloadJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

namespace LibStorj.Wrapper.Contracts.Models
{
public class DownloadJob
public class DownloadJob : JobBase
{
public long Id { get; set; }
public bool IsFinished { get; set; }
public bool IsOnError { get; set; }
public ProgressStatusDownload CurrentProgress { get; set; }
public string FileId { get; set; }

public DownloadJob(string fileId)
{
FileId = fileId;
CurrentProgress = new ProgressStatusDownload(fileId, 0, 0, 0);
}
}
Expand Down
41 changes: 41 additions & 0 deletions LibStorj.Wrapper.Contracts/Models/JobBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace LibStorj.Wrapper.Contracts.Models
{
public delegate void ProgressChanged(JobBase job);
public delegate void JobFinished(JobBase job);
public delegate void JobFailed(JobBase job);

public abstract class JobBase
{
public long Id { get; set; }
public bool IsFinished { get; set; }
public bool IsOnError { get; set; }
public int ErrorCode { get; set; }
public string ErrorMessage { get; set; }

public event ProgressChanged ProgressChanged;
public event JobFinished JobFinished;
public event JobFailed JobFailed;

public void RaiseProgressChanged()
{
if (ProgressChanged != null)
ProgressChanged(this);
}

public void RaiseJobFinished()
{
if (JobFinished != null)
JobFinished(this);
}

public void RaiseJobFailed()
{
if (JobFailed != null)
JobFailed(this);
}
}
}
20 changes: 20 additions & 0 deletions LibStorj.Wrapper.Contracts/Models/UploadJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibStorj.Wrapper.Contracts.Models
{
public class UploadJob : JobBase
{
public ProgressStatusUpload CurrentProgress { get; set; }
public string FileName { get; set; }

public UploadJob(string fileName)
{
FileName = fileName;
CurrentProgress = new ProgressStatusUpload(fileName, 0, 0, 0);
}
}
}
40 changes: 7 additions & 33 deletions LibStorj.Wrapper.Test.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using LibStorj.Wrapper.Contracts.Interfaces;
using LibStorj.Wrapper.Contracts.Models;
using LibStorj.Wrapper.x64;
using Nito.AsyncEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibStorj.Wrapper.Test.Console.x64
{
Expand All @@ -24,39 +18,19 @@ static async void MainAsync(string[] args)
//First test - if this fails, the system could not load the DLLs correctly.
var timestamp = utils.GetTimestamp();

IStorj storj = new Storj();
//Set your keys and Mnemonics here or provide a keyfile vie the overloads.
storj.ImportKeys(new Contracts.Models.Keys("USER","PASSWORD","MNEMONIC"),"PASSPHRASE");
var keyExist = storj.KeysExist; //This is not really working - it always returns "true"

var buckets = await storj.GetBucketsAsync(); //Load all buckets - if that works your credentials did work
var files = await storj.ListFilesAsync(buckets[0]); //Load the files of the first bucket

//Get the versions of the dependencies to see if they work
var v1 = (new VersionInfo()).GetCurlVersion();
var v2 = (new VersionInfo()).GetLibuvCVersion();
var v3 = (new VersionInfo()).GetJsonCVersion();
var v4 = (new VersionInfo()).GetNettleVersion();

storj.UploadFile(buckets[0].Id, "Uploadfile.txt", @"YOURPATH\YOURFILE.txt"); //Test-upload a file - provide a path to a small file here

//At this point, the upload does not work.
//After the next line the upload works but blocks GetInfoAsync. If the file is uploaded, the GetInfo is executed and returns
var info = await storj.GetInfoAsync();

//The same with download
var job = storj.DownloadFile(files.Item2.First(), @"YOURPATH\YOURFILE.txt");
while(!job.IsFinished)
{
System.Console.Clear();
System.Console.WriteLine("Status: " + job.CurrentProgress.DoneBytes + "/" + job.CurrentProgress.TotalBytes + " - " + job.CurrentProgress.Progress + "%");
//The same like above
var infoDownload = await storj.GetInfoAsync();
}
}
static void OnDownloadProgress(ProgressStatusDownload progress)
{
System.Console.WriteLine("DownloadProgress: " + progress.Progress);
IStorj storj = new Storj();
//Set your keys and Mnemonics here or provide a keyfile via the overloads.
storj.ImportKeys(new Contracts.Models.Keys("USER","PASSWORD","MNEMONIC"),"PASSPHRASE");

var buckets = await storj.GetBucketsAsync(); //Load all buckets - if that works your credentials did work

//Now do whatever you like - storj is working for you! :)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,30 @@ public static DownloadJob DownloadFile(string bucketId, string fileId, string lo
return job;
}

public DownloadFileCallbackAsync(DownloadJob job)
private DownloadFileCallbackAsync(DownloadJob job)
{
_job = job;
}

public void onProgress(string fileId, double progress, long downloadedBytes, long totalBytes)
{
_job.CurrentProgress = new ProgressStatusDownload(fileId, progress, downloadedBytes, totalBytes);
_job.RaiseProgressChanged();
}

public void onComplete(string fileId, string localPath)
{
_job.CurrentProgress = new ProgressStatusDownload(fileId, 100, 0, 0);
_job.IsFinished = true;
_job.RaiseJobFinished();
}

public void onError(string fileId, int errorCode, string message)
{
_job.IsOnError = true;
//Todo: fehlermeldung übernehmen
_job.ErrorCode = errorCode;
_job.ErrorMessage = message;
_job.RaiseJobFailed();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,51 @@

namespace LibStorj.Wrapper.AsyncCallbackWrapper
{
class UploadFileCallbackAsync : TaskCompletionSource<File>, io.storj.libstorj.UploadFileCallback
class UploadFileCallbackAsync : io.storj.libstorj.UploadFileCallback
{
private IProgress<ProgressStatusUpload> _progress;
private UploadJob _job;

public UploadFileCallbackAsync(string bucketId, string fileName, string localPath, IProgress<ProgressStatusUpload> progress, io.storj.libstorj.Storj storj)
public static UploadJob UploadFile(string bucketId, string fileName, string localPath, io.storj.libstorj.Storj storj)
{
_progress = progress;
UploadJob job = new UploadJob(fileName);
UploadFileCallbackAsync callback = new UploadFileCallbackAsync(job);
try
{
storj.uploadFile(bucketId, fileName, localPath, this); //ToDo: CancelHandle

var handle = storj.uploadFile(bucketId, fileName, localPath, callback);
job.Id = handle;
}
catch (io.storj.libstorj.KeysNotFoundException)
{
throw new KeysNotFoundException();
}
return job;
}

private UploadFileCallbackAsync(UploadJob job)
{
_job = job;
}

public void onProgress(string filePath, double progress, long downloadedBytes, long totalBytes)
public void onProgress(string filePath, double progress, long uploadedBytes, long totalBytes)
{
if (_progress != null)
_progress.Report(new ProgressStatusUpload(filePath, progress, downloadedBytes, totalBytes));
_job.CurrentProgress = new ProgressStatusUpload(filePath, progress, uploadedBytes, totalBytes);
_job.RaiseProgressChanged();
}

public void onComplete(string str, io.storj.libstorj.File f)
public void onComplete(string filePath, io.storj.libstorj.File f)
{
File file = new File(f.getId(), f.getBucketId(), f.getName(), f.getCreated(), f.isDecrypted(), f.getSize(), f.getMimeType(), f.getErasure(), f.getIndex(), f.getHMAC());
SetResult(file);
_job.CurrentProgress = new ProgressStatusUpload(filePath, 100, f.getSize(), f.getSize());
_job.IsFinished = true;
_job.RaiseJobFinished();
}

public void onError(string filePath, int errorCode, string message)
{
SetException(new UploadFileFailedException(filePath, errorCode, message));
_job.IsOnError = true;
_job.ErrorCode = errorCode;
_job.ErrorMessage = message;
_job.RaiseJobFailed();
}
}
}
9 changes: 6 additions & 3 deletions LibStorj.Wrapper.x64/LibStorj.Wrapper.x64.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
<Reference Include="IKVM.Runtime.JNI">
<HintPath>IKVM.Runtime.JNI.dll</HintPath>
</Reference>
<Reference Include="libstorj-java-0.7.3">
<HintPath>libstorj-java-0.7.3.dll</HintPath>
</Reference>
<Reference Include="libstorj-java-0.8">
<HintPath>libstorj-java-0.8.dll</HintPath>
</Reference>
<Reference Include="libstorj-java-0.8.1">
<HintPath>libstorj-java-0.8.1.dll</HintPath>
</Reference>
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -88,6 +88,9 @@
<None Update="libstorj-2.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="libstorj-java-0.8.1.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="libstorj-java-0.8.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
8 changes: 4 additions & 4 deletions LibStorj.Wrapper.x64/Storj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ public DownloadJob DownloadFile(string bucketId, string fileId, string localPath
return DownloadFileCallbackAsync.DownloadFile(bucketId, fileId, localPath, _storjJava);
}

public Task<File> UploadFile(Bucket bucket, string fileName, string localPath, IProgress<ProgressStatusUpload> progress = null)
public UploadJob UploadFile(Bucket bucket, string fileName, string localPath)
{
return UploadFile(bucket.Id, fileName, localPath, progress);
return UploadFile(bucket.Id, fileName, localPath);
}

public Task<File> UploadFile(string bucketId, string fileName, string localPath, IProgress<ProgressStatusUpload> progress = null)
public UploadJob UploadFile(string bucketId, string fileName, string localPath)
{
return new UploadFileCallbackAsync(bucketId, fileName, localPath, progress, _storjJava).Task;
return UploadFileCallbackAsync.UploadFile(bucketId, fileName, localPath, _storjJava);
}


Expand Down
Binary file added LibStorj.Wrapper.x64/libstorj-java-0.8.1.dll
Binary file not shown.
Binary file removed LibStorj.Wrapper.x64/libstorj-java-0.8.dll
Binary file not shown.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

A .Net/C#-wrapper for Storj

This is the very first version - please excuse the missing documentation at this point. Just the short info: this library is based on the java-libstorj-binding and is done via IKVM. It works except the upload and download and there is still debug-code in it. Use at your own risk!
This is the very first version - please excuse the missing documentation at this point. Just the short info: this library is based on the java-libstorj-binding and is done via IKVM.

# Attention
Currently the library is not working due to threading issues.
Use at your own risk!

0 comments on commit 1410e89

Please sign in to comment.