I am using ipfs-http-client to read the contents of a file form infura, how do i use the "cat" funtionality to correctly get the data in a string/json format?
const client = create({
url: ipfsUrl(),
headers: {
authorization: ipfsAuthPhrase(),
},
});
const cidformat = "f" + cid.substring(2);
const cidV0 = new CID(cidformat).toV0().toString();
const resp = await client.cat(cidV0);
let content = [];
for await (const chunk of resp) {
content = [...content, ...chunk];
}
console.log(content.toString());
right now i am just getting a array of binaries on the console log.
From this point on its just a matter of decoding the content buffer.
If the content is some JSON:
const raw = Buffer.from(content).toString('utf8')
console.log(JSON.parse(raw))
If the content is an image:
Buffer.from(content).toString('base64')
Related
I am trying to implement below feature using ReactJS and PDF_LIB.
Load PDF in Iframe from source URL -> PDF is editable test fields and then submit updated PDF to nodejs Server.
I achieved :
I render Iframe in ReactJS.
I used PDF_LIB js library to load PDFDocument and create Blob url for local change.
Issue:
On submit, Didn't get updated PDF content from Blob URL.
how can we get updated content from same Blob URL ?
Below code is for initialization of PDF.
const intializePdf = async () => {
const url = 'https://pdf-lib.js.org/assets/with_update_sections.pdf'
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer())
const pdfDoc = await PDFDocument.load(arrayBuffer)
const pdfBytes = await pdfDoc.save();
const blobObj = new Blob([pdfBytes], { type: "application/pdf" })
const docUrl = URL.createObjectURL(blobObj);
setPdfInfo(docUrl);
}
Below code for Submit PDF to Server
const submitPdf = async () => {
const url = pdfInfo
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer())
const pdfDoc = await PDFDocument.load(arrayBuffer)
pdfContentToParent(await pdfDoc.save())
}
On above code of "pdfDoc" is still don't have any updated fields.
Hello I'm sending/POST a File from a HTML Form on a browser client to a Remix application server.
The Remix applicaton server is sending/handling my File as a async AsyncIterable.
I now need to convert it back to a File object so I can send it to a backend server as FormData.
I tried both with and without buffer for demo:
Does anyone have experiance with convert AsyncIterable to Blob then to File??
const myHandler: UploadHandler = async ({ name, contentType, data, filename }) => {
//'data' is data of type AsyncIterable<Uint8Array>
//'contentType' is 'image/png'
let dataArray1 = []; //test1 without buffer
let dataArray2 = []; //test2 with buffer
for await (const x of data) {
dataArray1.push(x); //without buffer
dataArray2.push(x.buffer); //with buffer
}
const blob1 = new Blob(dataArray1, {type: contentType});
const blob2 = new Blob(dataArray2, {type: contentType});
const file1 = new File([blob1], filename, { type: contentType });
const file2 = new File([blob2], filename, { type: contentType });
console.log('file1.size: ', file1.size);
//file1 size is 1336843 (and for file2)
//but i'm getting content-length 1337028 from my browser Form
//so I'm not sure if it's correct
return file1;
};
[![content-length size][1]][1]
[![enter image description here][2]][2]
[![enter image description here][3]][3]
[![enter image description here][4]][4]
Try passing the blob parts directly into the file constructor.
const myHandler: UploadHandler = async ({ name, contentType, data, filename }) =>
{
const dataArray1 = [];
for await (const x of data)
{
dataArray1.push(x);
}
const file1 = new File(dataArray1, filename, { type: contentType });
return file1;
};
I am trying to send a yaml file as a base64 string so that this code works:
const response = await octokit.request('GET /repos/{owner}/{repo}/git/blobs/{file_sha}', {
owner: 'DevEx',
repo: 'hpdev-content',
file_sha: fileSha,
headers: {
authorization: `Bearer ${githubConfig?.token}`,
},
});
const decoded = Buffer.from(response.data.content, 'base64').toString('utf8');
In the above code response.data.content should have the data.
I have this route:
router.get('/repos/:owner/:repo/git/blobs/:file_sha', (req, res) => {
// TODO: do we need to do anything with the path params?
// eslint-disable-next-line #typescript-eslint/no-unused-vars
const { owner, repo, file_sha } = req.params;
const contents = writeUsersReport();
const encoded = Buffer.from(contents, 'binary').toString('base64');
res.send(encoded);
});
The code is working fine except that the client code expects the base64 string in a property called content in the following code:
const decoded = Buffer.from(response.data.content, 'base64').toString('utf8');
But the string is in response.data.
How can I set the content property instead?
How about sending a json response containing an object with a content property from your server side instead of the encoded string directly?
// ...
const encoded = Buffer.from(contents, 'binary').toString('base64');
res.json({content:encoded});
1.) I have a function that successfully generates random words in accordance with the user input (if user input is 10 than 10 random words will be displayed on the website). A have a json server were I'm getting the words using GET request fetch api as you can see below:
function getRandomWords() {
let words = getServerData('http://localhost:3000/words').then(data => {
const wordsToMemorize = document.querySelector('#words-to-memorize');
document.querySelector("#wordsInput").addEventListener("click", function() {
let temp = wordsToMemorize.value;
generatedArrayELements.innerHTML = "";
for(let i = 0; i < temp; i++) {
let rander = Math.floor(Math.random() * 3000);
generatedArrayELements.innerHTML += data[rander] + "</br>";
}})
});
}
2.) I want to transfer the generated words to my .json file in to the randomwords array but it doesn't want to work. And using DOM down there also doesnt make sense that I just realised:
function putServerData() {
let data = getRandomWords();
let fetchOptions = {
method: "POST",
mode: "cors",
cache: "no-cache",
headers: {
'Content-Type': 'application/json'
},
credentials: "same-origin",
body: JSON.stringify(data)
};
return fetch("http://localhost:3000/randomwords", fetchOptions)
.then(resp => resp.json())
.then(json => console.log(json));
}
document.querySelector("#wordsInput").addEventListener("click", function() {
putServerData();
})
3.) I feel like being very close to my goal because the random word generator works and I can also POST data in to the .json file if I set manually the value to the let data variable, like let data = ["test"]. What I dont know is how to send the generated random words in to my .json file.
4.) My json file:
{
"randomwords": [],
"words": [
"a",
"abandon",
"ability",
"able",
"abortion",
"about",
"above",
"abroad",
"absence",
"absolute",
"absolutely",
"absorb",
"abuse",
"academic",
"accept",
"access"...(remaining words...)
}]
4.) I tried to follow the documentation. Maybe I need to use some timeout because the random words first need to be generated before I use POST request on them. I've been struggling with this for almost a week and cant find solution, I read almost every relating post in SO. Every help would be greatly appreciated.
return a promise in my getRandomWords() with some timeout can be a working solution?
You can't add date to json file by making a fetch post call, however what you can do is write the data to your json file, you can do that by using the node fs module.
first make a route handler
const fs = require('fs')
router.post("/savedata", (req, res) => {
const jsondata = req.body
fs.writeFileSync('./path/to/your/json/data', jsondata, err => {
if (err) {
res.send("error saving file")
} else {
res.send("data saved successfully")
}
})
})
and then use fetch to make a call to your passing the data in the body
I'm new to Node.js and I need to upload some PDFs to an external API (Zip Forms).
Right now I have the code below but the PDF pages are blank when they arrive at the destination. I tried saving the PDF locally, using the same binary data that I'm sending to the API, and the PDFs are correctly saved.
I am also using setTimeout method here because I cannot find a method that waits for the PDF to read, before sending it to the API.
Also tried binary instead of latin-1 in readFileSync method, but it doesn't change anything.
Code:
const aws = require('aws-sdk');
const https = require('https');
const request = require('request');
const { createWriteStream, readFileSync, writeFileSync } = require('fs');
const s3 = new aws.S3(); // Pass in opts to S3 if necessary
// Look up order and related info.
var order = await Order.findOne({ id })
.populate('agent');
if (createZiplogixTransaction) {
ziplogixTransactionId = await sails.helpers.ziplogix.createZiplogixTransaction.with({
ziplogixContextId: ziplogixContextId,
transactionName: order.propertyStreetAddress + ', ' + order.propertyCity,
// FUTURE: if the transaction helper is updated, include actual order information
// e.g. Primary seller name, property street address, etc.
});
}
if (!order) {
throw 'noSuchOrder';
}
// Permissions
if (this.req.me && this.req.me.accountType !== 'agent' && !ziplogixContextId) {
throw 'forbidden';
}
let savedPdfs = await PdfOrderExternalId.find({ orderId: id });
await PdfOrderExternalId.destroy({
where: { orderId: id }
});
for (const pdf of pdfs) {
let url = await s3.getSignedUrl('getObject', {
Bucket: 'disclosure-pdfs',
Key: pdf.uploadFd,
Expires: 60 * 5
});
let file = createWriteStream(`/tmp/${pdf.slug}.pdf`);
await https.get(url, async (response) => {
await response.pipe(file);
// Need to wait for file to write on disk :|. Doesn't work with await or Promise (Why? IDK)
setTimeout(async () => {
let postData = await readFileSync(`/tmp/${pdf.slug}.pdf`, 'latin1');
let queryString = `Name=${pdf.displayName}&Description=${pdf.displayName}`;
savedPdfs.forEach(item => {
if (item.pdfTemplate === pdf.pdfTemplate) {
queryString += `Id=${item.externalId}`;
}
});
request({
method: 'POST',
url: `${sails.config.custom.ziplogixApiBaseUrl}/transactions/${ziplogixTransactionId}/documents/file?${queryString}`,
headers: {
'X-Auth-ContextID': ziplogixContextId,
'X-Auth-SharedKey': sails.config.custom.ziplogixSharedKey,
'Content-Type': ['application/pdf', 'application/pdf']
},
body: postData
}, async (error, response, body) => {
// code here ...
});
}, 1000);
});
}
await exits.success(Date.now());
Any ideas what I'm doing wrong?
Thank you