コード例 #1
0
ファイル: UnixFsApi.cs プロジェクト: jakubbober/Catalyst
        public async Task <IFileSystemNode> AddAsync(Stream stream,
                                                     string name,
                                                     AddFileOptions options,
                                                     CancellationToken cancel)
        {
            options ??= new AddFileOptions();

            // TODO: various options
            if (options.Trickle)
            {
                throw new NotImplementedException("Trickle");
            }

            var blockService = GetBlockService(options);

            var chunker = new SizeChunker();
            var nodes   = await chunker.ChunkAsync(stream, name, options, blockService, _keyApi, cancel)
                          .ConfigureAwait(false);

            // Multiple nodes for the file?
            var node = await BuildTreeAsync(nodes, options, cancel);

            // Wrap in directory?
            if (options.Wrap)
            {
                var link   = node.ToLink(name);
                var wlinks = new[]
                {
                    link
                };
                node = await CreateDirectoryAsync(wlinks, options, cancel).ConfigureAwait(false);
            }
            else
            {
                node.Name = name;
            }

            // Advertise the root node.
            if (options.Pin && _dfsState.IsStarted)
            {
                await _dhtApi.ProvideAsync(node.Id, advertise : true, cancel : cancel).ConfigureAwait(false);
            }

            // Return the file system node.
            return(node);
        }
コード例 #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);
        }