1
1
using Ipfs . CoreApi ;
2
2
using Newtonsoft . Json . Linq ;
3
- using System . Collections . Generic ;
4
3
using System . IO ;
5
4
using System . Net . Http ;
6
5
using System . Threading ;
7
6
using System . Threading . Tasks ;
8
7
8
+ #nullable enable
9
+
9
10
namespace Ipfs . Http
10
11
{
11
12
class BlockApi : IBlockApi
@@ -17,92 +18,89 @@ internal BlockApi(IpfsClient ipfs)
17
18
this . ipfs = ipfs ;
18
19
}
19
20
20
- public async Task < byte [ ] > GetAsync ( Cid id , CancellationToken cancel = default ( CancellationToken ) )
21
+ public async Task < byte [ ] > GetAsync ( Cid id , CancellationToken cancel = default )
21
22
{
22
23
return await ipfs . DownloadBytesAsync ( "block/get" , cancel , id ) ;
23
24
}
24
25
25
- public async Task < Cid > PutAsync (
26
+ public async Task < IBlockStat > PutAsync (
26
27
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 )
32
33
{
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 ) ;
52
36
}
53
37
54
- public async Task < Cid > PutAsync (
38
+ public async Task < IBlockStat > PutAsync (
55
39
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 )
61
45
{
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
+ ] ;
79
49
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 ;
81
65
}
82
66
83
- public async Task < IDataBlock > StatAsync ( Cid id , CancellationToken cancel = default ( CancellationToken ) )
67
+ public async Task < IBlockStat > StatAsync ( Cid id , CancellationToken cancel = default )
84
68
{
85
69
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 ;
92
84
}
93
85
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 )
95
87
{
96
88
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" ] ;
101
95
if ( error != null )
102
96
throw new HttpRequestException ( error ) ;
103
- return ( Cid ) ( string ) result [ "Hash" ] ;
104
- }
105
97
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
+ }
106
104
}
107
105
108
106
}
0 commit comments