77using System . Text ;
88using System . Threading ;
99using System . Threading . Tasks ;
10-
10+ using Ipfs . CoreApi ;
11+ using System . IO ;
12+
1113namespace Ipfs . Api
1214{
1315
14- /// <summary>
15- /// Information about a raw IPFS Block.
16- /// </summary>
17- /// <seealso cref="BlockApi.StatAsync"/>
18- public class BlockInfo
19- {
20- /// <summary>
21- /// The <see cref="Cid"/> of the block.
22- /// </summary>
23- /// <value>
24- /// The unique ID of the block.
25- /// </value>
26- public Cid Id { get ; set ; }
27-
28- /// <summary>
29- /// The serialised size (in bytes) of the block.
30- /// </summary>
31- public long Size { get ; set ; }
32- }
33-
34- /// <summary>
35- /// Manages the raw <see cref="Block">IPFS blocks</see>.
36- /// </summary>
37- /// <remarks>
38- /// An IPFS Block is a byte sequence that represents an IPFS Object
39- /// (i.e. serialized byte buffers). It is useful to talk about them as "blocks" in Bitswap
40- /// and other things that do not care about what is being stored.
41- /// <para>
42- /// It is also possible to store arbitrary stuff using ipfs block put/get as the API
43- /// does not check for proper IPFS Object formatting.
44- /// </para>
45- /// <note>
46- /// This may be very good or bad, we haven't decided yet 😄
47- /// </note>
48- /// <para>
49- /// This API is accessed via the <see cref="IpfsClient.Block"/> property.
50- /// </para>
51- /// </remarks>
52- /// <seealso href="https://github.com/ipfs/interface-ipfs-core/tree/master/API/block">Block API</seealso>
53- public class BlockApi
16+ class BlockApi : IBlockApi
5417 {
5518 IpfsClient ipfs ;
5619
@@ -59,16 +22,7 @@ internal BlockApi(IpfsClient ipfs)
5922 this . ipfs = ipfs ;
6023 }
6124
62- /// <summary>
63- /// Gets a raw <see cref="Block">IPFS block</see>.
64- /// </summary>
65- /// <param name="cancel">
66- /// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
67- /// </param>
68- /// <param name="id">
69- /// The <see cref="Cid"/> of the block.
70- /// </param>
71- public async Task < Block > GetAsync ( Cid id , CancellationToken cancel = default ( CancellationToken ) ) // TODO CID support
25+ public async Task < IDataBlock > GetAsync ( Cid id , CancellationToken cancel = default ( CancellationToken ) ) // TODO CID support
7226 {
7327 var data = await ipfs . DownloadBytesAsync ( "block/get" , cancel , id ) ;
7428 return new Block
@@ -78,89 +32,63 @@ internal BlockApi(IpfsClient ipfs)
7832 } ;
7933 }
8034
81- /// <summary>
82- /// Stores a byte array as a raw <see cref="Block">IPFS block</see>.
83- /// </summary>
84- /// <param name="data">
85- /// The byte array to send to the IPFS network.
86- /// </param>
87- /// <param name="cancel">
88- /// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
89- /// </param>
90- public async Task < Block > PutAsync ( byte [ ] data , CancellationToken cancel = default ( CancellationToken ) )
35+ public async Task < Cid > PutAsync (
36+ byte [ ] data ,
37+ string contentType = Cid . DefaultContentType ,
38+ string multiHash = MultiHash . DefaultAlgorithmName ,
39+ CancellationToken cancel = default ( CancellationToken ) )
9140 {
92- var json = await ipfs . UploadAsync ( "block/put" , cancel , data ) ;
41+ var options = new List < string > ( ) ;
42+ if ( multiHash != MultiHash . DefaultAlgorithmName || contentType != Cid . DefaultContentType )
43+ {
44+ options . Add ( $ "mhtype={ multiHash } ") ;
45+ options . Add ( $ "format={ contentType } ") ;
46+ }
47+ var json = await ipfs . UploadAsync ( "block/put" , cancel , data , options . ToArray ( ) ) ;
9348 var info = JObject . Parse ( json ) ;
94- return new Block
95- {
96- DataBytes = data ,
97- Id = ( string ) info [ "Key" ]
98- } ;
49+ return ( string ) info [ "Key" ] ;
9950 }
10051
101- /// <summary>
102- /// Stores a raw <see cref="Block">IPFS block</see>.
103- /// </summary>
104- /// <param name="block">
105- /// The <seealso cref="Block"/> to send to the IPFS network.
106- /// </param>
107- /// <param name="cancel">
108- /// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
109- /// </param>
110- public Task < Block > PutAsync ( Block block , CancellationToken cancel = default ( CancellationToken ) )
52+ public async Task < Cid > PutAsync (
53+ Stream data ,
54+ string contentType = Cid . DefaultContentType ,
55+ string multiHash = MultiHash . DefaultAlgorithmName ,
56+ CancellationToken cancel = default ( CancellationToken ) )
11157 {
112- return PutAsync ( block . DataBytes , cancel ) ;
58+ var options = new List < string > ( ) ;
59+ if ( multiHash != MultiHash . DefaultAlgorithmName || contentType != Cid . DefaultContentType )
60+ {
61+ options . Add ( $ "mhtype={ multiHash } ") ;
62+ options . Add ( $ "format={ contentType } ") ;
63+ }
64+ var json = await ipfs . UploadAsync ( "block/put" , cancel , data , options . ToArray ( ) ) ;
65+ var info = JObject . Parse ( json ) ;
66+ return ( string ) info [ "Key" ] ;
11367 }
11468
115- /// <summary>
116- /// Information on a raw <see cref="Block">IPFS block</see>.
117- /// </summary>
118- /// <param name="id">
119- /// The <see cref="Cid"/> of the block.
120- /// </param>
121- /// <param name="cancel">
122- /// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
123- /// </param>
124- public async Task < BlockInfo > StatAsync ( Cid id , CancellationToken cancel = default ( CancellationToken ) )
69+ public async Task < IDataBlock > StatAsync ( Cid id , CancellationToken cancel = default ( CancellationToken ) )
12570 {
12671 var json = await ipfs . DoCommandAsync ( "block/stat" , cancel , id ) ;
12772 var info = JObject . Parse ( json ) ;
128- return new BlockInfo
73+ return new Block
12974 {
13075 Size = ( long ) info [ "Size" ] ,
13176 Id = ( string ) info [ "Key" ]
13277 } ;
13378 }
13479
135- /// <summary>
136- /// Remove a raw <see cref="Block">IPFS block</see>.
137- /// </summary>
138- /// <param name="cancel">
139- /// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
140- /// </param>
141- /// <param name="id">
142- /// The <see cref="Cid"/> of the block.
143- /// </param>
144- /// <param name="ignoreNonexistent">
145- /// If <b>true</b> do not raise exception when <paramref name="id"/> does not
146- /// exist. Default value is <b>false</b>.
147- /// </param>
148- /// <returns>
149- /// The awaited Task will return the deleted <paramref name="id"/> or
150- /// <see cref="string.Empty"/> if the hash does not exist and <paramref name="ignoreNonexistent"/>
151- /// is <b>true</b>.
152- /// </returns>
153- public async Task < string > RemoveAsync ( Cid id , bool ignoreNonexistent = false , CancellationToken cancel = default ( CancellationToken ) ) // TODO CID support
80+ public async Task < Cid > RemoveAsync ( Cid id , bool ignoreNonexistent = false , CancellationToken cancel = default ( CancellationToken ) ) // TODO CID support
15481 {
15582 var json = await ipfs . DoCommandAsync ( "block/rm" , cancel , id , "force=" + ignoreNonexistent . ToString ( ) . ToLowerInvariant ( ) ) ;
15683 if ( json . Length == 0 )
157- return "" ;
84+ return null ;
15885 var result = JObject . Parse ( json ) ;
15986 var error = ( string ) result [ "Error" ] ;
16087 if ( error != null )
16188 throw new HttpRequestException ( error ) ;
162- return ( string ) result [ "Hash" ] ;
163- }
89+ return ( Cid ) ( string ) result [ "Hash" ] ;
90+ }
91+
16492 }
16593
16694}
0 commit comments