Skip to content

Release/0.6.0: API cleanup, Filestore support, WebRTC-Direct support #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/Block.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
using System.Runtime.Serialization;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace Ipfs.Http
{
/// <inheritdoc />
[DataContract]
public class Block : IDataBlock
public record Block : IBlockStat
{
/// <summary>
/// The data of the block.
/// </summary>
public byte[] DataBytes { get; set; }

/// <inheritdoc />
[DataMember]
[JsonProperty("Key")]
public required Cid Id { get; set; }

/// <inheritdoc />
[DataMember]
public required long Size { get; set; }
public required int Size { get; set; }
}

}
128 changes: 63 additions & 65 deletions src/CoreApi/BlockApi.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Ipfs.CoreApi;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

#nullable enable

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

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

public async Task<Cid> PutAsync(
public async Task<IBlockStat> PutAsync(
byte[] data,
string contentType = Cid.DefaultContentType,
string multiHash = MultiHash.DefaultAlgorithmName,
string encoding = MultiBase.DefaultAlgorithmName,
bool pin = false,
CancellationToken cancel = default(CancellationToken))
string cidCodec = "raw",
MultiHash? hash = null,
bool? pin = null,
bool? allowBigBlock = null,
CancellationToken cancel = default)
{
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}");
}
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);
}

return cid;
using var stream = new MemoryStream(data);
return await PutAsync(stream, cidCodec, hash, pin, allowBigBlock, cancel);
}

public async Task<Cid> PutAsync(
public async Task<IBlockStat> PutAsync(
Stream data,
string contentType = Cid.DefaultContentType,
string multiHash = MultiHash.DefaultAlgorithmName,
string encoding = MultiBase.DefaultAlgorithmName,
bool pin = false,
CancellationToken cancel = default(CancellationToken))
string cidCodec = "raw",
MultiHash? hash = null,
bool? pin = null,
bool? allowBigBlock = null,
CancellationToken cancel = default)
{
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}");
}
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);
}
string[] options = [
$"cid-codec={cidCodec}"
];

return cid;
if (hash != null)
options = [.. options, $"mhtype={hash}", $"mhlen={hash.Algorithm.DigestSize}"];

if (pin != null)
options = [.. options, $"pin={pin.ToString().ToLowerInvariant()}"];

if (allowBigBlock != null)
options = [.. options, $"allow-big-block={allowBigBlock.ToString().ToLowerInvariant()}"];

var json = await ipfs.UploadAsync("block/put", cancel, data, null, options);
var res = JObject.Parse(json).ToObject<Block>();
if (res is null)
throw new InvalidDataException("The response did not contain a block.");

return res;
}

public async Task<IDataBlock> StatAsync(Cid id, CancellationToken cancel = default(CancellationToken))
public async Task<IBlockStat> StatAsync(Cid id, CancellationToken cancel = default)
{
var json = await ipfs.DoCommandAsync("block/stat", cancel, id);
var info = JObject.Parse(json);
return new Block
{
Size = (long)info["Size"],
Id = (string)info["Key"]
};

var parsed = JObject.Parse(json);
if (parsed is null)
throw new InvalidDataException("The response could not be parsed.");

var error = (string?)parsed["Error"];
if (error != null)
throw new HttpRequestException(error);

var res = parsed.ToObject<Block>();
if (res is null)
throw new InvalidDataException("The response could not be deserialized.");

return res;
}

public async Task<Cid> RemoveAsync(Cid id, bool ignoreNonexistent = false, CancellationToken cancel = default(CancellationToken))
public async Task<Cid> RemoveAsync(Cid id, bool ignoreNonexistent = false, CancellationToken cancel = default)
{
var json = await ipfs.DoCommandAsync("block/rm", cancel, id, "force=" + ignoreNonexistent.ToString().ToLowerInvariant());
if (json.Length == 0)
return null;
var result = JObject.Parse(json);
var error = (string)result["Error"];

var parsed = JObject.Parse(json);
if (parsed is null)
throw new InvalidDataException("The response could not be parsed.");

var error = (string?)parsed["Error"];
if (error != null)
throw new HttpRequestException(error);
return (Cid)(string)result["Hash"];
}

var cid = parsed["Hash"]?.ToObject<Cid>();
if (cid is null)
throw new InvalidDataException("The response could not be deserialized.");

return cid;
}
}

}
Loading