Skip to content

Commit 7200ceb

Browse files
authored
Merge pull request #43 from ipfs-shipyard/release/0.6.0
Release/0.6.0: API cleanup, Filestore support, WebRTC-Direct support
2 parents 8478efa + e43acf0 commit 7200ceb

21 files changed

+680
-771
lines changed

src/Block.cs

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
using System.Runtime.Serialization;
1+
using System.Runtime.Serialization;
2+
using Newtonsoft.Json;
23

34
namespace Ipfs.Http
45
{
56
/// <inheritdoc />
67
[DataContract]
7-
public class Block : IDataBlock
8+
public record Block : IBlockStat
89
{
9-
/// <summary>
10-
/// The data of the block.
11-
/// </summary>
12-
public byte[] DataBytes { get; set; }
13-
1410
/// <inheritdoc />
1511
[DataMember]
12+
[JsonProperty("Key")]
1613
public required Cid Id { get; set; }
1714

1815
/// <inheritdoc />
1916
[DataMember]
20-
public required long Size { get; set; }
17+
public required int Size { get; set; }
2118
}
22-
2319
}

src/CoreApi/BlockApi.cs

+63-65
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using Ipfs.CoreApi;
22
using Newtonsoft.Json.Linq;
3-
using System.Collections.Generic;
43
using System.IO;
54
using System.Net.Http;
65
using System.Threading;
76
using System.Threading.Tasks;
87

8+
#nullable enable
9+
910
namespace Ipfs.Http
1011
{
1112
class BlockApi : IBlockApi
@@ -17,92 +18,89 @@ internal BlockApi(IpfsClient ipfs)
1718
this.ipfs = ipfs;
1819
}
1920

20-
public async Task<byte[]> GetAsync(Cid id, CancellationToken cancel = default(CancellationToken))
21+
public async Task<byte[]> GetAsync(Cid id, CancellationToken cancel = default)
2122
{
2223
return await ipfs.DownloadBytesAsync("block/get", cancel, id);
2324
}
2425

25-
public async Task<Cid> PutAsync(
26+
public async Task<IBlockStat> PutAsync(
2627
byte[] data,
27-
string contentType = Cid.DefaultContentType,
28-
string multiHash = MultiHash.DefaultAlgorithmName,
29-
string encoding = MultiBase.DefaultAlgorithmName,
30-
bool pin = false,
31-
CancellationToken cancel = default(CancellationToken))
28+
string cidCodec = "raw",
29+
MultiHash? hash = null,
30+
bool? pin = null,
31+
bool? allowBigBlock = null,
32+
CancellationToken cancel = default)
3233
{
33-
var options = new List<string>();
34-
if (multiHash != MultiHash.DefaultAlgorithmName ||
35-
contentType != Cid.DefaultContentType ||
36-
encoding != MultiBase.DefaultAlgorithmName)
37-
{
38-
options.Add($"mhtype={multiHash}");
39-
options.Add($"format={contentType}");
40-
options.Add($"cid-base={encoding}");
41-
}
42-
var json = await ipfs.UploadAsync("block/put", cancel, data, options.ToArray());
43-
var info = JObject.Parse(json);
44-
Cid cid = (string)info["Key"];
45-
46-
if (pin)
47-
{
48-
await ipfs.Pin.AddAsync(cid, recursive: false, cancel: cancel);
49-
}
50-
51-
return cid;
34+
using var stream = new MemoryStream(data);
35+
return await PutAsync(stream, cidCodec, hash, pin, allowBigBlock, cancel);
5236
}
5337

54-
public async Task<Cid> PutAsync(
38+
public async Task<IBlockStat> PutAsync(
5539
Stream data,
56-
string contentType = Cid.DefaultContentType,
57-
string multiHash = MultiHash.DefaultAlgorithmName,
58-
string encoding = MultiBase.DefaultAlgorithmName,
59-
bool pin = false,
60-
CancellationToken cancel = default(CancellationToken))
40+
string cidCodec = "raw",
41+
MultiHash? hash = null,
42+
bool? pin = null,
43+
bool? allowBigBlock = null,
44+
CancellationToken cancel = default)
6145
{
62-
var options = new List<string>();
63-
if (multiHash != MultiHash.DefaultAlgorithmName ||
64-
contentType != Cid.DefaultContentType ||
65-
encoding != MultiBase.DefaultAlgorithmName)
66-
{
67-
options.Add($"mhtype={multiHash}");
68-
options.Add($"format={contentType}");
69-
options.Add($"cid-base={encoding}");
70-
}
71-
var json = await ipfs.UploadAsync("block/put", cancel, data, null, options.ToArray());
72-
var info = JObject.Parse(json);
73-
Cid cid = (string)info["Key"];
74-
75-
if (pin)
76-
{
77-
await ipfs.Pin.AddAsync(cid, recursive: false, cancel: cancel);
78-
}
46+
string[] options = [
47+
$"cid-codec={cidCodec}"
48+
];
7949

80-
return cid;
50+
if (hash != null)
51+
options = [.. options, $"mhtype={hash}", $"mhlen={hash.Algorithm.DigestSize}"];
52+
53+
if (pin != null)
54+
options = [.. options, $"pin={pin.ToString().ToLowerInvariant()}"];
55+
56+
if (allowBigBlock != null)
57+
options = [.. options, $"allow-big-block={allowBigBlock.ToString().ToLowerInvariant()}"];
58+
59+
var json = await ipfs.UploadAsync("block/put", cancel, data, null, options);
60+
var res = JObject.Parse(json).ToObject<Block>();
61+
if (res is null)
62+
throw new InvalidDataException("The response did not contain a block.");
63+
64+
return res;
8165
}
8266

83-
public async Task<IDataBlock> StatAsync(Cid id, CancellationToken cancel = default(CancellationToken))
67+
public async Task<IBlockStat> StatAsync(Cid id, CancellationToken cancel = default)
8468
{
8569
var json = await ipfs.DoCommandAsync("block/stat", cancel, id);
86-
var info = JObject.Parse(json);
87-
return new Block
88-
{
89-
Size = (long)info["Size"],
90-
Id = (string)info["Key"]
91-
};
70+
71+
var parsed = JObject.Parse(json);
72+
if (parsed is null)
73+
throw new InvalidDataException("The response could not be parsed.");
74+
75+
var error = (string?)parsed["Error"];
76+
if (error != null)
77+
throw new HttpRequestException(error);
78+
79+
var res = parsed.ToObject<Block>();
80+
if (res is null)
81+
throw new InvalidDataException("The response could not be deserialized.");
82+
83+
return res;
9284
}
9385

94-
public async Task<Cid> RemoveAsync(Cid id, bool ignoreNonexistent = false, CancellationToken cancel = default(CancellationToken))
86+
public async Task<Cid> RemoveAsync(Cid id, bool ignoreNonexistent = false, CancellationToken cancel = default)
9587
{
9688
var json = await ipfs.DoCommandAsync("block/rm", cancel, id, "force=" + ignoreNonexistent.ToString().ToLowerInvariant());
97-
if (json.Length == 0)
98-
return null;
99-
var result = JObject.Parse(json);
100-
var error = (string)result["Error"];
89+
90+
var parsed = JObject.Parse(json);
91+
if (parsed is null)
92+
throw new InvalidDataException("The response could not be parsed.");
93+
94+
var error = (string?)parsed["Error"];
10195
if (error != null)
10296
throw new HttpRequestException(error);
103-
return (Cid)(string)result["Hash"];
104-
}
10597

98+
var cid = parsed["Hash"]?.ToObject<Cid>();
99+
if (cid is null)
100+
throw new InvalidDataException("The response could not be deserialized.");
101+
102+
return cid;
103+
}
106104
}
107105

108106
}

0 commit comments

Comments
 (0)