コード例 #1
0
        public async Task <Cid> RemoveAsync(Cid id,
                                            bool ignoreNonexistent   = false,
                                            CancellationToken cancel = default)
        {
            if (id.Hash.IsIdentityHash)
            {
                return(id);
            }

            if (await Store.ExistsAsync(id, cancel).ConfigureAwait(false))
            {
                await Store.RemoveAsync(id, cancel).ConfigureAwait(false);

                await PinApi.RemoveAsync(id, false, cancel).ConfigureAwait(false);

                return(id);
            }

            if (ignoreNonexistent)
            {
                return(null);
            }

            throw new KeyNotFoundException($"Block '{id.Encode()}' does not exist.");
        }
コード例 #2
0
        public async Task <Cid> PutAsync(byte[] data,
                                         string contentType       = Cid.DefaultContentType,
                                         string multiHash         = MultiHash.DefaultAlgorithmName,
                                         string encoding          = MultiBase.DefaultAlgorithmName,
                                         bool pin                 = false,
                                         CancellationToken cancel = default)
        {
            if (data.Length > _dfsOptions.Block.MaxBlockSize)
            {
                throw new ArgumentOutOfRangeException($"data.Length",
                                                      $@"Block length can not exceed {_dfsOptions.Block.MaxBlockSize.ToString()}.");
            }

            // Small enough for an inline CID?
            if (_dfsOptions.Block.AllowInlineCid && data.Length <= _dfsOptions.Block.InlineCidLimit)
            {
                return(new Cid
                {
                    ContentType = contentType,
                    Hash = MultiHash.ComputeHash(data, "identity")
                });
            }

            // CID V1 encoding defaulting to base32 which is not
            var cid = new Cid
            {
                ContentType = contentType,
                Hash        = MultiHash.ComputeHash(data, multiHash)
            };

            if (encoding != "base58btc")
            {
                cid.Encoding = encoding;
            }

            var block = new DataBlock
            {
                DataBytes = data,
                Id        = cid,
                Size      = data.Length
            };

            if (await Store.ExistsAsync(cid, cancel).ConfigureAwait(false))
            {
                Log.DebugFormat("Block '{0}' already present", cid);
            }
            else
            {
                await Store.PutAsync(cid, block, cancel).ConfigureAwait(false);

                if (_dfsState.IsStarted)
                {
                    await _dhtApi.ProvideAsync(cid, false, cancel).ConfigureAwait(false);
                }

                Log.DebugFormat("Added block '{0}'", cid);
            }

            // Inform the BitSwap service.
            _bitSwapApi.FoundBlock(block);

            // To pin or not.
            if (pin)
            {
                await PinApi.AddAsync(cid, false, cancel).ConfigureAwait(false);
            }
            else
            {
                await PinApi.RemoveAsync(cid, false, cancel).ConfigureAwait(false);
            }

            return(cid);
        }