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); }
public async Task <IFileSystemNode> AddAsync( Stream stream, string name, AddFileOptions options, CancellationToken cancel) { options = options ?? new AddFileOptions(); // TODO: various options if (options.Trickle) { throw new NotImplementedException("Trickle"); } var blockService = GetBlockService(options); var keyChain = await ipfs.KeyChain(cancel).ConfigureAwait(false); var chunker = new SizeChunker(); var nodes = await chunker.ChunkAsync(stream, name, options, blockService, keyChain, cancel).ConfigureAwait(false); // Multiple nodes for the file? FileSystemNode node = null; if (nodes.Count() == 1) { node = nodes.First(); } else { // Build the DAG that contains all the file nodes. var links = nodes.Select(n => n.ToLink()).ToArray(); var fileSize = (ulong)nodes.Sum(n => n.Size); var dm = new DataMessage { Type = DataType.File, FileSize = fileSize, BlockSizes = nodes.Select(n => (ulong)n.Size).ToArray() }; var pb = new MemoryStream(); ProtoBuf.Serializer.Serialize <DataMessage>(pb, dm); var dag = new DagNode(pb.ToArray(), links, options.Hash); // Save it. dag.Id = await blockService.PutAsync( data : dag.ToArray(), multiHash : options.Hash, encoding : options.Encoding, pin : options.Pin, cancel : cancel).ConfigureAwait(false); node = new FileSystemNode { Id = dag.Id, Size = (long)dm.FileSize, DagSize = dag.Size, Links = links }; } // Wrap in directory? if (options.Wrap) { var link = node.ToLink(name); var wlinks = new IFileSystemLink[] { link }; node = await CreateDirectoryAsync(wlinks, options, cancel).ConfigureAwait(false); } else { node.Name = name; } // Advertise the root node. if (options.Pin && ipfs.IsStarted) { await ipfs.Dht.ProvideAsync(node.Id, advertise : true, cancel : cancel).ConfigureAwait(false); } // Return the file system node. return(node); }