Skip to content

Commit

Permalink
Merge pull request #34 from ipfs-shipyard/rel/0.2.0
Browse files Browse the repository at this point in the history
Release 0.2.0; Kubo 0.26.0+ support
  • Loading branch information
Arlodotexe authored Apr 3, 2024
2 parents b9b71c4 + 9aafa1c commit a512e36
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 368 deletions.
40 changes: 7 additions & 33 deletions src/Block.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,23 @@
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization;

namespace Ipfs.Http
{
/// <inheritdoc />
[DataContract]
public class Block : IDataBlock
{
long? size;

/// <inheritdoc />
[DataMember]
public Cid Id { get; set; }
/// <summary>
/// The data of the block.
/// </summary>
public byte[] DataBytes { get; set; }

/// <inheritdoc />
[DataMember]
public byte[] DataBytes { get; set; }
public required Cid Id { get; set; }

/// <inheritdoc />
public Stream DataStream
{
get
{
return new MemoryStream(DataBytes, false);
}
}

/// <inheritdoc />
[DataMember]
public long Size
{
get
{
if (size.HasValue)
{
return size.Value;
}
return DataBytes.Length;
}
set
{
size = value;
}
}

public required long Size { get; set; }
}

}
10 changes: 0 additions & 10 deletions src/CoreApi/BitswapApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ internal BitswapApi(IpfsClient ipfs)
this.ipfs = ipfs;
}

public Task<IDataBlock> GetAsync(Cid id, CancellationToken cancel = default(CancellationToken))
{
return ipfs.Block.GetAsync(id, cancel);
}

public async Task<IEnumerable<Cid>> WantsAsync(MultiHash peer = null, CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.DoCommandAsync("bitswap/wantlist", cancel, peer?.ToString());
Expand All @@ -35,11 +30,6 @@ internal BitswapApi(IpfsClient ipfs)
return Cid.Decode(obj["/"].ToString());
});
}

public async Task UnwantAsync(Cid id, CancellationToken cancel = default(CancellationToken))
{
await ipfs.DoCommandAsync("bitswap/unwant", cancel, id);
}

public async Task<BitswapLedger> LedgerAsync(Peer peer, CancellationToken cancel = default(CancellationToken))
{
Expand Down
73 changes: 34 additions & 39 deletions src/CoreApi/BlockApi.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Ipfs.CoreApi;
using Ipfs.CoreApi;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;


namespace Ipfs.Http
{
class BlockApi : IBlockApi
Expand All @@ -17,69 +17,64 @@ internal BlockApi(IpfsClient ipfs)
this.ipfs = ipfs;
}

public async Task<IDataBlock> GetAsync(Cid id, CancellationToken cancel = default(CancellationToken))
public async Task<byte[]> GetAsync(Cid id, CancellationToken cancel = default(CancellationToken))
{
var data = await ipfs.DownloadBytesAsync("block/get", cancel, id);
return new Block
{
DataBytes = data,
Id = id
};
return await ipfs.DownloadBytesAsync("block/get", cancel, id);
}

public async Task<Cid> PutAsync(
byte[] data,
byte[] data,
string contentType = Cid.DefaultContentType,
string multiHash = MultiHash.DefaultAlgorithmName,
string encoding = MultiBase.DefaultAlgorithmName,
bool pin = false,
CancellationToken cancel = default(CancellationToken))
{
var options = new List<string>();
if (multiHash != MultiHash.DefaultAlgorithmName ||
contentType != Cid.DefaultContentType ||
encoding != MultiBase.DefaultAlgorithmName)
{
options.Add($"mhtype={multiHash}");
options.Add($"format={contentType}");
options.Add($"cid-base={encoding}");
if (multiHash != MultiHash.DefaultAlgorithmName ||
contentType != Cid.DefaultContentType ||
encoding != MultiBase.DefaultAlgorithmName)
{
options.Add($"mhtype={multiHash}");
options.Add($"format={contentType}");
options.Add($"cid-base={encoding}");
}
var json = await ipfs.UploadAsync("block/put", cancel, data, options.ToArray());
var info = JObject.Parse(json);
var json = await ipfs.UploadAsync("block/put", cancel, data, options.ToArray());
var info = JObject.Parse(json);
Cid cid = (string)info["Key"];

if (pin)
{
await ipfs.Pin.AddAsync(cid, recursive: false, cancel: cancel);
if (pin)
{
await ipfs.Pin.AddAsync(cid, recursive: false, cancel: cancel);
}

return cid;
}

public async Task<Cid> PutAsync(
Stream data,
Stream data,
string contentType = Cid.DefaultContentType,
string multiHash = MultiHash.DefaultAlgorithmName,
string encoding = MultiBase.DefaultAlgorithmName,
bool pin = false,
CancellationToken cancel = default(CancellationToken))
{
var options = new List<string>();
if (multiHash != MultiHash.DefaultAlgorithmName ||
contentType != Cid.DefaultContentType ||
encoding != MultiBase.DefaultAlgorithmName)
{
options.Add($"mhtype={multiHash}");
options.Add($"format={contentType}");
options.Add($"cid-base={encoding}");
if (multiHash != MultiHash.DefaultAlgorithmName ||
contentType != Cid.DefaultContentType ||
encoding != MultiBase.DefaultAlgorithmName)
{
options.Add($"mhtype={multiHash}");
options.Add($"format={contentType}");
options.Add($"cid-base={encoding}");
}
var json = await ipfs.UploadAsync("block/put", cancel, data, null, options.ToArray());
var info = JObject.Parse(json);
var json = await ipfs.UploadAsync("block/put", cancel, data, null, options.ToArray());
var info = JObject.Parse(json);
Cid cid = (string)info["Key"];

if (pin)
{
await ipfs.Pin.AddAsync(cid, recursive: false, cancel: cancel);
if (pin)
{
await ipfs.Pin.AddAsync(cid, recursive: false, cancel: cancel);
}

return cid;
Expand All @@ -88,7 +83,7 @@ public async Task<Cid> PutAsync(
public async Task<IDataBlock> StatAsync(Cid id, CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.DoCommandAsync("block/stat", cancel, id);
var info = JObject.Parse(json);
var info = JObject.Parse(json);
return new Block
{
Size = (long)info["Size"],
Expand All @@ -106,8 +101,8 @@ public async Task<Cid> PutAsync(
if (error != null)
throw new HttpRequestException(error);
return (Cid)(string)result["Hash"];
}

}

}

}
55 changes: 29 additions & 26 deletions src/CoreApi/FileSystemApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal FileSystemApi(IpfsClient ipfs)
this.emptyFolder = new Lazy<DagNode>(() => ipfs.Object.NewDirectoryAsync().Result);
}

public async Task<IFileSystemNode> AddFileAsync(string path, AddFileOptions options = null, CancellationToken cancel = default(CancellationToken))
public async Task<IFileSystemNode> AddFileAsync(string path, AddFileOptions options = null, CancellationToken cancel = default)
{
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
Expand All @@ -31,15 +31,16 @@ internal FileSystemApi(IpfsClient ipfs)
}
}

public Task<IFileSystemNode> AddTextAsync(string text, AddFileOptions options = null, CancellationToken cancel = default(CancellationToken))
public Task<IFileSystemNode> AddTextAsync(string text, AddFileOptions options = null, CancellationToken cancel = default)
{
return AddAsync(new MemoryStream(Encoding.UTF8.GetBytes(text), false), "", options, cancel);
}

public async Task<IFileSystemNode> AddAsync(Stream stream, string name = "", AddFileOptions options = null, CancellationToken cancel = default(CancellationToken))
public async Task<IFileSystemNode> AddAsync(Stream stream, string name = "", AddFileOptions options = null, CancellationToken cancel = default)
{
if (options == null)
options = new AddFileOptions();

var opts = new List<string>();
if (!options.Pin)
opts.Add("pin=false");
Expand All @@ -59,6 +60,7 @@ internal FileSystemApi(IpfsClient ipfs)
opts.Add($"cid-base=${options.Encoding}");
if (!string.IsNullOrWhiteSpace(options.ProtectionKey))
opts.Add($"protect={options.ProtectionKey}");

opts.Add($"chunker=size-{options.ChunkSize}");

var response = await ipfs.Upload2Async("add", cancel, stream, name, opts.ToArray());
Expand Down Expand Up @@ -92,7 +94,6 @@ internal FileSystemApi(IpfsClient ipfs)
Size = long.Parse((string)r["Size"]),
IsDirectory = false,
Name = name,
IpfsClient = ipfs
};
}
}
Expand All @@ -102,7 +103,7 @@ internal FileSystemApi(IpfsClient ipfs)
return fsn;
}

public async Task<IFileSystemNode> AddDirectoryAsync(string path, bool recursive = true, AddFileOptions options = null, CancellationToken cancel = default(CancellationToken))
public async Task<IFileSystemNode> AddDirectoryAsync(string path, bool recursive = true, AddFileOptions options = null, CancellationToken cancel = default)
{
if (options == null)
options = new AddFileOptions();
Expand Down Expand Up @@ -145,7 +146,6 @@ internal FileSystemApi(IpfsClient ipfs)
Links = links,
IsDirectory = true,
Size = directory.Size,
IpfsClient = ipfs
};

}
Expand All @@ -163,7 +163,7 @@ internal FileSystemApi(IpfsClient ipfs)
/// <returns>
/// The contents of the <paramref name="path"/> as a <see cref="string"/>.
/// </returns>
public async Task<String> ReadAllTextAsync(string path, CancellationToken cancel = default(CancellationToken))
public async Task<String> ReadAllTextAsync(string path, CancellationToken cancel = default)
{
using (var data = await ReadFileAsync(path, cancel))
using (var text = new StreamReader(data))
Expand All @@ -186,12 +186,12 @@ internal FileSystemApi(IpfsClient ipfs)
/// <returns>
/// A <see cref="Stream"/> to the file contents.
/// </returns>
public Task<Stream> ReadFileAsync(string path, CancellationToken cancel = default(CancellationToken))
public Task<Stream> ReadFileAsync(string path, CancellationToken cancel = default)
{
return ipfs.PostDownloadAsync("cat", cancel, path);
}

public Task<Stream> ReadFileAsync(string path, long offset, long length = 0, CancellationToken cancel = default(CancellationToken))
public Task<Stream> ReadFileAsync(string path, long offset, long length = 0, CancellationToken cancel = default)
{
// https://github.com/ipfs/go-ipfs/issues/5380
if (offset > int.MaxValue)
Expand All @@ -206,33 +206,36 @@ internal FileSystemApi(IpfsClient ipfs)
$"length={length}");
}

/// <inheritdoc cref="ListAsync"/>
public Task<IFileSystemNode> ListFileAsync(string path, CancellationToken cancel = default)
{
return ListAsync(path, cancel);
}

/// <summary>
/// Get information about the file or directory.
/// Get information about the directory.
/// </summary>
/// <param name="path">
/// A path to an existing file or directory, such as "QmXarR6rgkQ2fDSHjSY5nM2kuCXKYGViky5nohtwgF65Ec/about"
/// or "QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V"
/// A path to an existing directory, such as "QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V"
/// </param>
/// <param name="cancel">
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
/// </param>
/// <returns></returns>
public async Task<IFileSystemNode> ListFileAsync(string path, CancellationToken cancel = default(CancellationToken))
public async Task<IFileSystemNode> ListAsync(string path, CancellationToken cancel = default)
{
var json = await ipfs.DoCommandAsync("file/ls", cancel, path);
var json = await ipfs.DoCommandAsync("ls", cancel, path);
var r = JObject.Parse(json);
var hash = (string)r["Arguments"][path];
var o = (JObject)r["Objects"][hash];
var o = (JObject)r["Objects"]?[0];

var node = new FileSystemNode()
{
Id = (string)o["Hash"],
Size = (long)o["Size"],
IsDirectory = (string)o["Type"] == "Directory",
Links = new FileSystemLink[0],
IpfsClient = ipfs
IsDirectory = true,
Links = Array.Empty<FileSystemLink>(),
};
var links = o["Links"] as JArray;
if (links != null)

if (o["Links"] is JArray links)
{
node.Links = links
.Select(l => new FileSystemLink()
Expand All @@ -245,9 +248,9 @@ internal FileSystemApi(IpfsClient ipfs)
}

return node;
}

public Task<Stream> GetAsync(string path, bool compress = false, CancellationToken cancel = default(CancellationToken))
}

public Task<Stream> GetAsync(string path, bool compress = false, CancellationToken cancel = default)
{
return ipfs.PostDownloadAsync("get", cancel, path, $"compress={compress}");
}
Expand Down
1 change: 0 additions & 1 deletion src/CoreApi/MfsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public async Task<IEnumerable<IFileSystemNode>> ListAsync(string path, bool? U =
Id = (string)l["Hash"],
Size = (long)l["Size"],
IsDirectory = (int)l["Type"] == 1,
IpfsClient = ipfs
})
.ToArray();
}
Expand Down
Loading

0 comments on commit a512e36

Please sign in to comment.