Skip to content

Commit a512e36

Browse files
authored
Merge pull request #34 from ipfs-shipyard/rel/0.2.0
Release 0.2.0; Kubo 0.26.0+ support
2 parents b9b71c4 + 9aafa1c commit a512e36

14 files changed

+117
-368
lines changed

src/Block.cs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,23 @@
1-
using System.IO;
2-
using System.Runtime.Serialization;
1+
using System.Runtime.Serialization;
32

43
namespace Ipfs.Http
54
{
65
/// <inheritdoc />
76
[DataContract]
87
public class Block : IDataBlock
98
{
10-
long? size;
11-
12-
/// <inheritdoc />
13-
[DataMember]
14-
public Cid Id { get; set; }
9+
/// <summary>
10+
/// The data of the block.
11+
/// </summary>
12+
public byte[] DataBytes { get; set; }
1513

1614
/// <inheritdoc />
1715
[DataMember]
18-
public byte[] DataBytes { get; set; }
16+
public required Cid Id { get; set; }
1917

20-
/// <inheritdoc />
21-
public Stream DataStream
22-
{
23-
get
24-
{
25-
return new MemoryStream(DataBytes, false);
26-
}
27-
}
28-
2918
/// <inheritdoc />
3019
[DataMember]
31-
public long Size
32-
{
33-
get
34-
{
35-
if (size.HasValue)
36-
{
37-
return size.Value;
38-
}
39-
return DataBytes.Length;
40-
}
41-
set
42-
{
43-
size = value;
44-
}
45-
}
46-
20+
public required long Size { get; set; }
4721
}
4822

4923
}

src/CoreApi/BitswapApi.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ internal BitswapApi(IpfsClient ipfs)
1616
this.ipfs = ipfs;
1717
}
1818

19-
public Task<IDataBlock> GetAsync(Cid id, CancellationToken cancel = default(CancellationToken))
20-
{
21-
return ipfs.Block.GetAsync(id, cancel);
22-
}
23-
2419
public async Task<IEnumerable<Cid>> WantsAsync(MultiHash peer = null, CancellationToken cancel = default(CancellationToken))
2520
{
2621
var json = await ipfs.DoCommandAsync("bitswap/wantlist", cancel, peer?.ToString());
@@ -35,11 +30,6 @@ internal BitswapApi(IpfsClient ipfs)
3530
return Cid.Decode(obj["/"].ToString());
3631
});
3732
}
38-
39-
public async Task UnwantAsync(Cid id, CancellationToken cancel = default(CancellationToken))
40-
{
41-
await ipfs.DoCommandAsync("bitswap/unwant", cancel, id);
42-
}
4333

4434
public async Task<BitswapLedger> LedgerAsync(Peer peer, CancellationToken cancel = default(CancellationToken))
4535
{

src/CoreApi/BlockApi.cs

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using Ipfs.CoreApi;
1+
using Ipfs.CoreApi;
22
using Newtonsoft.Json.Linq;
33
using System.Collections.Generic;
4-
using System.IO;
4+
using System.IO;
55
using System.Net.Http;
66
using System.Threading;
77
using System.Threading.Tasks;
8-
8+
99
namespace Ipfs.Http
1010
{
1111
class BlockApi : IBlockApi
@@ -17,69 +17,64 @@ internal BlockApi(IpfsClient ipfs)
1717
this.ipfs = ipfs;
1818
}
1919

20-
public async Task<IDataBlock> GetAsync(Cid id, CancellationToken cancel = default(CancellationToken))
20+
public async Task<byte[]> GetAsync(Cid id, CancellationToken cancel = default(CancellationToken))
2121
{
22-
var data = await ipfs.DownloadBytesAsync("block/get", cancel, id);
23-
return new Block
24-
{
25-
DataBytes = data,
26-
Id = id
27-
};
22+
return await ipfs.DownloadBytesAsync("block/get", cancel, id);
2823
}
2924

3025
public async Task<Cid> PutAsync(
31-
byte[] data,
26+
byte[] data,
3227
string contentType = Cid.DefaultContentType,
3328
string multiHash = MultiHash.DefaultAlgorithmName,
3429
string encoding = MultiBase.DefaultAlgorithmName,
3530
bool pin = false,
3631
CancellationToken cancel = default(CancellationToken))
3732
{
3833
var options = new List<string>();
39-
if (multiHash != MultiHash.DefaultAlgorithmName ||
40-
contentType != Cid.DefaultContentType ||
41-
encoding != MultiBase.DefaultAlgorithmName)
42-
{
43-
options.Add($"mhtype={multiHash}");
44-
options.Add($"format={contentType}");
45-
options.Add($"cid-base={encoding}");
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}");
4641
}
47-
var json = await ipfs.UploadAsync("block/put", cancel, data, options.ToArray());
48-
var info = JObject.Parse(json);
42+
var json = await ipfs.UploadAsync("block/put", cancel, data, options.ToArray());
43+
var info = JObject.Parse(json);
4944
Cid cid = (string)info["Key"];
5045

51-
if (pin)
52-
{
53-
await ipfs.Pin.AddAsync(cid, recursive: false, cancel: cancel);
46+
if (pin)
47+
{
48+
await ipfs.Pin.AddAsync(cid, recursive: false, cancel: cancel);
5449
}
5550

5651
return cid;
5752
}
5853

5954
public async Task<Cid> PutAsync(
60-
Stream data,
55+
Stream data,
6156
string contentType = Cid.DefaultContentType,
6257
string multiHash = MultiHash.DefaultAlgorithmName,
6358
string encoding = MultiBase.DefaultAlgorithmName,
6459
bool pin = false,
6560
CancellationToken cancel = default(CancellationToken))
6661
{
6762
var options = new List<string>();
68-
if (multiHash != MultiHash.DefaultAlgorithmName ||
69-
contentType != Cid.DefaultContentType ||
70-
encoding != MultiBase.DefaultAlgorithmName)
71-
{
72-
options.Add($"mhtype={multiHash}");
73-
options.Add($"format={contentType}");
74-
options.Add($"cid-base={encoding}");
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}");
7570
}
76-
var json = await ipfs.UploadAsync("block/put", cancel, data, null, options.ToArray());
77-
var info = JObject.Parse(json);
71+
var json = await ipfs.UploadAsync("block/put", cancel, data, null, options.ToArray());
72+
var info = JObject.Parse(json);
7873
Cid cid = (string)info["Key"];
7974

80-
if (pin)
81-
{
82-
await ipfs.Pin.AddAsync(cid, recursive: false, cancel: cancel);
75+
if (pin)
76+
{
77+
await ipfs.Pin.AddAsync(cid, recursive: false, cancel: cancel);
8378
}
8479

8580
return cid;
@@ -88,7 +83,7 @@ public async Task<Cid> PutAsync(
8883
public async Task<IDataBlock> StatAsync(Cid id, CancellationToken cancel = default(CancellationToken))
8984
{
9085
var json = await ipfs.DoCommandAsync("block/stat", cancel, id);
91-
var info = JObject.Parse(json);
86+
var info = JObject.Parse(json);
9287
return new Block
9388
{
9489
Size = (long)info["Size"],
@@ -106,8 +101,8 @@ public async Task<Cid> PutAsync(
106101
if (error != null)
107102
throw new HttpRequestException(error);
108103
return (Cid)(string)result["Hash"];
109-
}
110-
104+
}
105+
111106
}
112107

113108
}

src/CoreApi/FileSystemApi.cs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal FileSystemApi(IpfsClient ipfs)
2222
this.emptyFolder = new Lazy<DagNode>(() => ipfs.Object.NewDirectoryAsync().Result);
2323
}
2424

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

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

39-
public async Task<IFileSystemNode> AddAsync(Stream stream, string name = "", AddFileOptions options = null, CancellationToken cancel = default(CancellationToken))
39+
public async Task<IFileSystemNode> AddAsync(Stream stream, string name = "", AddFileOptions options = null, CancellationToken cancel = default)
4040
{
4141
if (options == null)
4242
options = new AddFileOptions();
43+
4344
var opts = new List<string>();
4445
if (!options.Pin)
4546
opts.Add("pin=false");
@@ -59,6 +60,7 @@ internal FileSystemApi(IpfsClient ipfs)
5960
opts.Add($"cid-base=${options.Encoding}");
6061
if (!string.IsNullOrWhiteSpace(options.ProtectionKey))
6162
opts.Add($"protect={options.ProtectionKey}");
63+
6264
opts.Add($"chunker=size-{options.ChunkSize}");
6365

6466
var response = await ipfs.Upload2Async("add", cancel, stream, name, opts.ToArray());
@@ -92,7 +94,6 @@ internal FileSystemApi(IpfsClient ipfs)
9294
Size = long.Parse((string)r["Size"]),
9395
IsDirectory = false,
9496
Name = name,
95-
IpfsClient = ipfs
9697
};
9798
}
9899
}
@@ -102,7 +103,7 @@ internal FileSystemApi(IpfsClient ipfs)
102103
return fsn;
103104
}
104105

105-
public async Task<IFileSystemNode> AddDirectoryAsync(string path, bool recursive = true, AddFileOptions options = null, CancellationToken cancel = default(CancellationToken))
106+
public async Task<IFileSystemNode> AddDirectoryAsync(string path, bool recursive = true, AddFileOptions options = null, CancellationToken cancel = default)
106107
{
107108
if (options == null)
108109
options = new AddFileOptions();
@@ -145,7 +146,6 @@ internal FileSystemApi(IpfsClient ipfs)
145146
Links = links,
146147
IsDirectory = true,
147148
Size = directory.Size,
148-
IpfsClient = ipfs
149149
};
150150

151151
}
@@ -163,7 +163,7 @@ internal FileSystemApi(IpfsClient ipfs)
163163
/// <returns>
164164
/// The contents of the <paramref name="path"/> as a <see cref="string"/>.
165165
/// </returns>
166-
public async Task<String> ReadAllTextAsync(string path, CancellationToken cancel = default(CancellationToken))
166+
public async Task<String> ReadAllTextAsync(string path, CancellationToken cancel = default)
167167
{
168168
using (var data = await ReadFileAsync(path, cancel))
169169
using (var text = new StreamReader(data))
@@ -186,12 +186,12 @@ internal FileSystemApi(IpfsClient ipfs)
186186
/// <returns>
187187
/// A <see cref="Stream"/> to the file contents.
188188
/// </returns>
189-
public Task<Stream> ReadFileAsync(string path, CancellationToken cancel = default(CancellationToken))
189+
public Task<Stream> ReadFileAsync(string path, CancellationToken cancel = default)
190190
{
191191
return ipfs.PostDownloadAsync("cat", cancel, path);
192192
}
193193

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

209+
/// <inheritdoc cref="ListAsync"/>
210+
public Task<IFileSystemNode> ListFileAsync(string path, CancellationToken cancel = default)
211+
{
212+
return ListAsync(path, cancel);
213+
}
214+
209215
/// <summary>
210-
/// Get information about the file or directory.
216+
/// Get information about the directory.
211217
/// </summary>
212218
/// <param name="path">
213-
/// A path to an existing file or directory, such as "QmXarR6rgkQ2fDSHjSY5nM2kuCXKYGViky5nohtwgF65Ec/about"
214-
/// or "QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V"
219+
/// A path to an existing directory, such as "QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V"
215220
/// </param>
216221
/// <param name="cancel">
217-
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
222+
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
218223
/// </param>
219224
/// <returns></returns>
220-
public async Task<IFileSystemNode> ListFileAsync(string path, CancellationToken cancel = default(CancellationToken))
225+
public async Task<IFileSystemNode> ListAsync(string path, CancellationToken cancel = default)
221226
{
222-
var json = await ipfs.DoCommandAsync("file/ls", cancel, path);
227+
var json = await ipfs.DoCommandAsync("ls", cancel, path);
223228
var r = JObject.Parse(json);
224-
var hash = (string)r["Arguments"][path];
225-
var o = (JObject)r["Objects"][hash];
229+
var o = (JObject)r["Objects"]?[0];
230+
226231
var node = new FileSystemNode()
227232
{
228233
Id = (string)o["Hash"],
229-
Size = (long)o["Size"],
230-
IsDirectory = (string)o["Type"] == "Directory",
231-
Links = new FileSystemLink[0],
232-
IpfsClient = ipfs
234+
IsDirectory = true,
235+
Links = Array.Empty<FileSystemLink>(),
233236
};
234-
var links = o["Links"] as JArray;
235-
if (links != null)
237+
238+
if (o["Links"] is JArray links)
236239
{
237240
node.Links = links
238241
.Select(l => new FileSystemLink()
@@ -245,9 +248,9 @@ internal FileSystemApi(IpfsClient ipfs)
245248
}
246249

247250
return node;
248-
}
249-
250-
public Task<Stream> GetAsync(string path, bool compress = false, CancellationToken cancel = default(CancellationToken))
251+
}
252+
253+
public Task<Stream> GetAsync(string path, bool compress = false, CancellationToken cancel = default)
251254
{
252255
return ipfs.PostDownloadAsync("get", cancel, path, $"compress={compress}");
253256
}

src/CoreApi/MfsApi.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public async Task<IEnumerable<IFileSystemNode>> ListAsync(string path, bool? U =
6161
Id = (string)l["Hash"],
6262
Size = (long)l["Size"],
6363
IsDirectory = (int)l["Type"] == 1,
64-
IpfsClient = ipfs
6564
})
6665
.ToArray();
6766
}

0 commit comments

Comments
 (0)