Esempio n. 1
0
        public static async Task ProcessImage()
        {
            using (var ipfs = new IpfsClient("https://ipfs.infura.io:5001"))
            {
                //Name of the file to add
                string fileName = "kids.jpg";

                //Wrap our stream in an IpfsStream, so it has a file name.
                IpfsStream inputStream = new IpfsStream(fileName, File.OpenRead(fileName));

                MerkleNode node = await ipfs.Add(inputStream);

                Debug.WriteLine(node.Hash.ToString());

                Stream outputStream = await ipfs.Cat(node.Hash.ToString());

                using (var image = Image.FromStream(outputStream))
                {
                    var newImage = ScaleImage(image, 500);
                    newImage.Save("newKids.jpg", ImageFormat.Jpeg);

                    inputStream = new IpfsStream("kids500", File.OpenRead("newKids.jpg"));

                    node = await ipfs.Add(inputStream);

                    Debug.WriteLine(node.Hash.ToString());
                }
            }
        }
Esempio n. 2
0
        public async Task <Stream> ReadFile(string hash)
        {
            if (string.IsNullOrEmpty(hash))
            {
                return(null);
            }

            return(await ipfs.Cat(hash));
        }
Esempio n. 3
0
        public async Task <Image> DownloadImage(string ipfsHash)
        {
            using (var ipfs = new IpfsClient(ipfsUrl))
            {
                Stream outputStream = await ipfs.Cat(ipfsHash).ConfigureAwait(false);

                return(Image.FromStream(outputStream));
            }
        }
        public async Task ClientShouldBeAbleToDownloadLargeFiles()
        {
            /* This test is a bit long because it relies of having
             * a large file available through IPFS and the best way to
             * ensure that is to simply create that file in the first place */

            var sourceFile = Path.GetTempFileName();
            var targetFile = Path.GetTempFileName();

            try
            {
                using (var client = new IpfsClient())
                {
                    string sourceHash;
                    await CreateDummyFileAsync(sourceFile);

                    using (var sourceStream = File.OpenRead(sourceFile))
                        using (var ipfsSourceStream = new IpfsStream("source", sourceStream))
                        {
                            var hash = await client.Add(ipfsSourceStream);

                            sourceHash = hash.ToString();
                        }

                    using (var stream = await client.Cat(sourceHash))
                        using (var outputFilename = File.OpenWrite(targetFile))
                        {
                            await stream.CopyToAsync(outputFilename);
                        }

                    await client.Pin.Rm(sourceHash);

                    Assert.IsTrue(FileHashesAreEqual(sourceFile, targetFile));
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                if (File.Exists(sourceFile))
                {
                    File.Delete(sourceFile);
                }

                if (File.Exists(targetFile))
                {
                    File.Delete(targetFile);
                }
            }
        }
Esempio n. 5
0
        public async Task <ActionResult> Download(int id, string differentSignature)
        {
            Document   document = db.Documents.Find(id);
            IpfsClient ipfs     = new IpfsClient("https://ipfs.infura.io:5001");
            Stream     stream   = await ipfs.Cat(document.Hash);

            FileStream fileStream = new FileStream("C:\\Users\\zsche\\Documents\\IPFS\\" + document.Hash + document.FileExtension, FileMode.Create);

            stream.CopyTo(fileStream);
            fileStream.Close();

            return(View("DownloadConfirmed", document));
        }
Esempio n. 6
0
        public static async void Get(string hash, string filePath)
        {
            using (var httpClient = new HttpClient()
            {
                Timeout = Timeout.InfiniteTimeSpan
            })
                using (var ipfs = new IpfsClient(new Uri("http://127.0.0.1:5001"), httpClient))
                {
                    try
                    {
                        Stream outputStream = await ipfs.Cat(hash);

                        FileStream writeStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
                        ReadWriteStream(outputStream, writeStream);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
        }
Esempio n. 7
0
        public static async Task ProcessFile()
        {
            using (var ipfs = new IpfsClient())
            {
                //Name of the file to add
                string fileName = "test.txt";

                //Wrap our stream in an IpfsStream, so it has a file name.
                IpfsStream inputStream = new IpfsStream(fileName, File.OpenRead(fileName));

                MerkleNode node = await ipfs.Add(inputStream);

                Stream outputStream = await ipfs.Cat(node.Hash.ToString());

                using (StreamReader sr = new StreamReader(outputStream))
                {
                    string contents = sr.ReadToEnd();

                    Console.WriteLine(contents); //Contents of test.txt are printed here!
                }
            }
        }