|
1 | 1 | using Newtonsoft.Json; |
| 2 | +using Newtonsoft.Json.Linq; |
2 | 3 | using System; |
3 | 4 | using System.Collections.Generic; |
4 | 5 | using System.Linq; |
| 6 | +using System.Net.Http; |
5 | 7 | using System.Text; |
6 | 8 | using System.Threading; |
7 | 9 | using System.Threading.Tasks; |
@@ -123,6 +125,36 @@ internal BlockApi(IpfsClient ipfs) |
123 | 125 | { |
124 | 126 | return ipfs.DoCommandAsync<BlockInfo>("block/stat", cancel, hash); |
125 | 127 | } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Remove a raw <see cref="Block">IPFS block</see>. |
| 131 | + /// </summary> |
| 132 | + /// <param name="cancel"> |
| 133 | + /// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised. |
| 134 | + /// </param> |
| 135 | + /// <param name="hash"> |
| 136 | + /// The <see cref="string"/> representation of a base58 encoded <see cref="Ipfs.MultiHash"/>. |
| 137 | + /// </param> |
| 138 | + /// <param name="ignoreNonexistent"> |
| 139 | + /// If <b>true</b> do not raise exception when <paramref name="hash"/> does not |
| 140 | + /// exist. Default value is <b>false</b>. |
| 141 | + /// </param> |
| 142 | + /// <returns> |
| 143 | + /// The awaited Task will return the deleted <paramref name="hash"/> or |
| 144 | + /// <see cref="string.Empty"/> if the hash does not exist and <paramref name="ignoreNonexistent"/> |
| 145 | + /// is <b>true</b>. |
| 146 | + /// </returns> |
| 147 | + public async Task<string> RemoveAsync(string hash, bool ignoreNonexistent = false, CancellationToken cancel = default(CancellationToken)) // TODO CID support |
| 148 | + { |
| 149 | + var json = await ipfs.DoCommandAsync("block/rm", cancel, hash, "force=" + ignoreNonexistent.ToString().ToLowerInvariant()); |
| 150 | + if (json.Length == 0) |
| 151 | + return ""; |
| 152 | + var result = JObject.Parse(json); |
| 153 | + var error = (string)result["Error"]; |
| 154 | + if (error != null) |
| 155 | + throw new HttpRequestException(error); |
| 156 | + return (string)result["Hash"]; |
| 157 | + } |
126 | 158 | } |
127 | 159 |
|
128 | 160 | } |
0 commit comments