コード例 #1
0
ファイル: BlockApi.cs プロジェクト: avanaur/net-ipfs-api
        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(CancellationToken))
        {
            var options = new List <string>();

            if (multiHash != MultiHash.DefaultAlgorithmName ||
                contentType != Cid.DefaultContentType ||
                encoding != MultiBase.DefaultAlgorithmName)
            {
                options.Add($"mhtype={multiHash}");
                options.Add($"format={contentType}");
                options.Add($"cid-base={encoding}");
            }
            var json = await ipfs.UploadAsync("block/put", cancel, data, options.ToArray());

            var info = JObject.Parse(json);
            Cid cid  = (string)info["Key"];

            if (pin)
            {
                await ipfs.Pin.AddAsync(cid, recursive : false, cancel : cancel);
            }

            return(cid);
        }
コード例 #2
0
ファイル: NameApi.cs プロジェクト: saluspass/salus-client
        /// <summary>
        /// This will publish a file hash to IPNS
        /// </summary>
        /// <param name="hashFilename">
        ///   Hash of the already uploaded file to publish
        /// </param>
        /// <param name="cancel">
        ///   Is used to stop the task.  When cancelled, the <see cref="TaskCanceledException"/> is raised.
        /// </param>
        /// <returns>True if success</returns>
        public async Task <bool> PublishAsync(byte[] hashFilename, CancellationToken cancel = default(CancellationToken))
        {
            string json = await ipfs.UploadAsync("name/publish", cancel, hashFilename);

            JObject jObject = JObject.Parse(json);

            return(jObject["Name"] != null && jObject["Value"] != null);
        }
コード例 #3
0
ファイル: BlockApi.cs プロジェクト: ruo2012/net-ipfs-api
        public async Task <Cid> PutAsync(
            byte[] data,
            string contentType       = Cid.DefaultContentType,
            string multiHash         = MultiHash.DefaultAlgorithmName,
            CancellationToken cancel = default(CancellationToken))
        {
            var options = new List <string>();

            if (multiHash != MultiHash.DefaultAlgorithmName || contentType != Cid.DefaultContentType)
            {
                options.Add($"mhtype={multiHash}");
                options.Add($"format={contentType}");
            }
            var json = await ipfs.UploadAsync("block/put", cancel, data, options.ToArray());

            var info = JObject.Parse(json);

            return((string)info["Key"]);
        }
コード例 #4
0
        /// <summary>
        ///   Stores a byte array as a raw <see cref="Block">IPFS block</see>.
        /// </summary>
        /// <param name="data">
        ///   The byte array to send to the IPFS network.
        /// </param>
        /// <param name="cancel">
        ///   Is used to stop the task.  When cancelled, the <see cref="TaskCanceledException"/> is raised.
        /// </param>
        public async Task <Block> PutAsync(byte[] data, CancellationToken cancel = default(CancellationToken))
        {
            var json = await ipfs.UploadAsync("block/put", cancel, data);

            var info = JsonConvert.DeserializeObject <BlockInfo>(json);

            return(new Block
            {
                DataBytes = data,
                Hash = info.Key
            });
        }
コード例 #5
0
        /// <summary>
        ///   Add a <see cref="Stream"/> to interplanetary file system.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="name"></param>
        /// <param name="cancel">
        ///   Is used to stop the task.  When cancelled, the <see cref="TaskCanceledException"/> is raised.
        /// </param>
        public async Task <FileSystemNode> AddAsync(Stream stream, string name = "", CancellationToken cancel = default(CancellationToken))
        {
            var json = await ipfs.UploadAsync("add", cancel, stream);

            var r = JObject.Parse(json);

            return(new FileSystemNode
            {
                Hash = (string)r["Hash"],
                Name = name,
                IpfsClient = ipfs
            });
        }
コード例 #6
0
        /// <summary>
        ///   Add a <see cref="Stream"/> to interplanetary file system.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="name"></param>
        public async Task <FileSystemNode> AddAsync(Stream stream, string name = "")
        {
            var json = await ipfs.UploadAsync("add", stream);

            var r = JObject.Parse(json);

            return(new FileSystemNode
            {
                Hash = (string)r["Hash"],
                Name = name,
                IpfsClient = ipfs
            });
        }
コード例 #7
0
ファイル: DagApi.cs プロジェクト: avanaur/net-ipfs-api
        public async Task <Cid> PutAsync(
            Stream data,
            string contentType       = "dag-cbor",
            string multiHash         = MultiHash.DefaultAlgorithmName,
            string encoding          = MultiBase.DefaultAlgorithmName,
            bool pin                 = true,
            CancellationToken cancel = default(CancellationToken))
        {
            var json = await ipfs.UploadAsync("dag/put", cancel,
                                              data, null,
                                              $"format={contentType}",
                                              $"pin={pin.ToString().ToLowerInvariant()}",
                                              $"hash={multiHash}",
                                              $"cid-base={encoding}");

            var result = JObject.Parse(json);

            return((Cid)(string)result["Cid"]["/"]);
        }
コード例 #8
0
ファイル: FileSystemApi.cs プロジェクト: ruo2012/net-ipfs-api
        public async Task <IFileSystemNode> AddAsync(Stream stream, string name = "", CancellationToken cancel = default(CancellationToken))
        {
            var json = await ipfs.UploadAsync("add", cancel, stream);

            var r   = JObject.Parse(json);
            var fsn = new FileSystemNode
            {
                Id          = (string)r["Hash"],
                Size        = long.Parse((string)r["Size"]),
                IsDirectory = false,
                Name        = name,
                IpfsClient  = ipfs
            };

            if (log.IsDebugEnabled)
            {
                log.Debug("added " + fsn.Id + " " + fsn.Name);
            }
            return(fsn);
        }
コード例 #9
0
        /// <summary>
        ///   Store a MerkleDAG node.
        /// </summary>
        /// <param name="node">A merkle dag</param>
        /// <param name="cancel">
        ///   Is used to stop the task.  When cancelled, the <see cref="TaskCanceledException"/> is raised.
        /// </param>
        public async Task <DagNode> PutAsync(DagNode node, CancellationToken cancel = default(CancellationToken))
        {
            var json = await ipfs.UploadAsync("object/put", cancel, node.ToArray(), "inputenc=protobuf");

            return(node);
        }
コード例 #10
0
        /// <summary>
        ///   Store a MerkleDAG node.
        /// </summary>
        public async Task <DagNode> PutAsync(DagNode node)
        {
            var json = await ipfs.UploadAsync("object/put", node.ToArray(), "inputenc=protobuf");

            return(node);
        }
コード例 #11
0
ファイル: FileSystemApi.cs プロジェクト: avanaur/net-ipfs-api
        public async Task <IFileSystemNode> AddAsync(Stream stream, string name = "", AddFileOptions options = null, CancellationToken cancel = default(CancellationToken))
        {
            if (options == null)
            {
                options = new AddFileOptions();
            }
            var opts = new List <string>();

            if (!options.Pin)
            {
                opts.Add("pin=false");
            }
            if (options.Wrap)
            {
                opts.Add("wrap-with-directory=true");
            }
            if (options.RawLeaves)
            {
                opts.Add("raw-leaves=true");
            }
            if (options.OnlyHash)
            {
                opts.Add("only-hash=true");
            }
            if (options.Trickle)
            {
                opts.Add("trickle=true");
            }
            if (options.Hash != MultiHash.DefaultAlgorithmName)
            {
                opts.Add($"hash=${options.Hash}");
            }
            if (options.Encoding != MultiBase.DefaultAlgorithmName)
            {
                opts.Add($"cid-base=${options.Encoding}");
            }
            opts.Add($"chunker=size-{options.ChunkSize}");

            var json = await ipfs.UploadAsync("add", cancel, stream, name, opts.ToArray());

            // The result is a stream of LDJSON objects.
            // See https://github.com/ipfs/go-ipfs/issues/4852
            FileSystemNode fsn = null;

            using (var sr = new StringReader(json))
                using (var jr = new JsonTextReader(sr)
                {
                    SupportMultipleContent = true
                })
                {
                    while (jr.Read())
                    {
                        var r = await JObject.LoadAsync(jr, cancel);

                        fsn = new FileSystemNode
                        {
                            Id          = (string)r["Hash"],
                            Size        = long.Parse((string)r["Size"]),
                            IsDirectory = false,
                            Name        = name,
                            IpfsClient  = ipfs
                        };
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("added " + fsn.Id + " " + fsn.Name);
                        }
                    }
                }

            fsn.IsDirectory = options.Wrap;
            return(fsn);
        }