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()); } } }
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! } } }