/// <summary> /// Add an object to ipfs. /// Adds contents of <path> to ipfs. Use -r to add directories. /// Note that directories are added recursively, to form the ipfs /// MerkleDAG.A smarter partial add with a staging area(like git) /// remains to be implemented /// </summary> /// <param name="stream">The ipfs stream.</param> /// <param name="recursive">Add directory paths recursively</param> /// <param name="quiet">Write minimal output</param> /// <param name="progress">Stream progress data</param> /// <param name="wrapWithDirectory">Wrap files with a directory object</param> /// <param name="trickle">Use trickle-dag format for dag generation</param> /// <param name="cancellationToken">Token allowing you to cancel the request</param> /// <returns>The merkle node of the added file in IPFS</returns> public async Task <MerkleNode> Add(IpfsStream stream, bool recursive = false, bool quiet = false, bool wrapWithDirectory = false, bool trickle = false, CancellationToken cancellationToken = default(CancellationToken)) { var flags = new Dictionary <string, string>() { { "stream-channels", "true" }, { "progress", "false" } }; if (recursive) { flags.Add("recursive", "true"); } if (quiet) { flags.Add("quiet", "true"); } MultipartFormDataContent multiContent = new MultipartFormDataContent(); StreamContent sc = new StreamContent(stream); sc.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); multiContent.Add(sc, "file", stream.Name); HttpContent content = await ExecutePostAsync("add", null, flags, multiContent, cancellationToken); string json = await content.ReadAsStringAsync(); IpfsAdd add = _jsonSerializer.Deserialize <IpfsAdd>(json); return(new MerkleNode(new MultiHash(add.Hash)) { Name = add.Name }); }
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()); } } }
/// <summary> /// Add an object to ipfs. /// Adds contents of <path> to ipfs. Use -r to add directories. /// Note that directories are added recursively, to form the ipfs /// MerkleDAG.A smarter partial add with a staging area(like git) /// remains to be implemented /// </summary> /// <param name="stream">The ipfs stream.</param> /// <param name="recursive">Add directory paths recursively</param> /// <param name="quiet">Write minimal output</param> /// <param name="progress">Stream progress data</param> /// <param name="wrapWithDirectory">Wrap files with a directory object</param> /// <param name="trickle">Use trickle-dag format for dag generation</param> /// <returns>The merkle node of the added file in IPFS</returns> public async Task<MerkleNode> Add(IpfsStream stream, bool recursive = false, bool quiet = false, bool wrapWithDirectory = false, bool trickle = false) { var flags = new Dictionary<string, string>() { { "stream-channels", "true" }, { "progress", "false" } }; if (recursive) { flags.Add("recursive", "true"); } if (quiet) { flags.Add("quiet", "true"); } MultipartFormDataContent multiContent = new MultipartFormDataContent(); StreamContent sc = new StreamContent(stream); sc.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); multiContent.Add(sc, "file", stream.Name); HttpContent content = await ExecutePostAsync("add", null, flags, multiContent); string json = await content.ReadAsStringAsync(); IpfsAdd add = _jsonSerializer.Deserialize<IpfsAdd>(json); return new MerkleNode(new MultiHash(add.Hash)) { Name = add.Name }; }
public async Task <ActionResult> Upload([Bind(Include = ("Id,Title,IsPublic"))] Document document, HttpPostedFileBase file) { Document newDocument = new Document { Id = document.Id, Title = document.Title, IsPublic = document.IsPublic, TimeStamp = DateTime.Now }; string filepath = System.IO.Path.GetFullPath(file.FileName); BinaryReader binary = new BinaryReader(file.InputStream); MemoryStream mStream = new MemoryStream(binary.ReadBytes(file.ContentLength)); IpfsStream fileIpfs = new IpfsStream(filepath, mStream); IpfsClient ipfs = new IpfsClient("https://ipfs.infura.io:5001"); MerkleNode node = await ipfs.Add(fileIpfs); newDocument.Hash = node.Hash.ToString(); newDocument.UserId = User.Identity.GetUserId(); newDocument.UserName = db.Users.Where(u => u.Id == newDocument.UserId).FirstOrDefault().UserName; newDocument.FileExtension = Path.GetExtension(filepath); ContractReceipt receipt = await AddContractToEthereum(newDocument.Hash); newDocument.ContractAddress = receipt.ContractAddress; db.Documents.Add(newDocument); db.SaveChanges(); return(RedirectToAction("Index", new { newDocument.UserName })); }
public async Task <MerkleNode> Add(string name, Stream stream) { using (var ipfs = new IpfsClient(ipfsUrl)) { var inputStream = new IpfsStream(name, stream); var merkleNode = await ipfs.Add(inputStream).ConfigureAwait(false); var multiHash = ipfs.Pin.Add(merkleNode.Hash.ToString()); return(merkleNode); } }
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); } } }
private async Task <string> AddToIPFSAsync(string fullPath) { using (var ipfs = new IpfsClient("http://ipfs:5001")) { var stream = System.IO.File.OpenRead(fullPath); //Name of the file to add string fileName = Guid.NewGuid().ToString(); //Wrap our stream in an IpfsStream, so it has a file name. IpfsStream inputStream = new IpfsStream(fileName, stream); MerkleNode node = await ipfs.Add(inputStream); return(node.Hash.ToString()); } }
public async Task <string> AddFile(string fileName, Stream fileStream) { if (string.IsNullOrEmpty(fileName)) { return(null); } if (fileStream == null) { return(null); } using (var inputStream = new IpfsStream(fileName, fileStream)) { var node = await ipfs.Add(inputStream); return(node.ToString()); } }
public static async Task <string> Add(string fileName) { using (var httpClient = new HttpClient() { Timeout = Timeout.InfiniteTimeSpan }) using (var ipfs = new IpfsClient(new Uri("http://127.0.0.1:5001"), httpClient)) { IpfsStream inputStream = new IpfsStream(fileName, File.OpenRead(fileName)); try { MerkleNode node = await ipfs.Add(inputStream); return((String)node.Hash); } catch (Exception ex) { throw ex; } } }
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! } } }