I'm trying to send a file from my NodeJS Lambda function to Dailymotion, and I'm using the following code:
// Download the original file to the `tmp` folder of Lambda.
await axios.get('https://cdn.mysite.com/source.mp4', {
responseType: 'stream'
})
.then( response => {
response.data.pipe(fs.createWriteStream('/tmp/video.mp4'));
})
.catch(error => {
console.log(error);
});
const form = new FormData();
form.append('file', fs.createReadStream('/tmp/video.mp4'));
// Post the file to Dailymotion API.
axios.post('https://upload-xx.xxx.dailymotion.com/upload?uuid=xxxxx&seal=xxxxx&extra=xxxxx', form, {
headers: {
...form.getHeaders,
'Content-Type': 'multipart/formdata',
'Content-Length': fs.statSync('/tmp/video.mp4').size
},
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
I can use the following URL to check the upload progress of the file: https://upload-xx.xxx.dailymotion.com/progress?uuid=xxxxx, but it seems that it uploads only the first chunk of the file and then stops, and I'm not getting any error or response.
Did I miss anything here?
When you use await, the result in your case is not a promise but stream. So there's no sense in adding old-style .then and .catch to not-promise essense.
Try the following.
try {
const stream = await axios.get('https://cdn.mysite.com/source.mp4', {
responseType: 'stream'
});
stream.pipe(fs.createWriteStream('/tmp/video.mp4'));
stream.on('error', (err) => console.log({ err }));
stream.on('close', async () => {
try {
const form = new FormData();
form.append('file', fs.createReadStream('/tmp/video.mp4'));
// Post the file to Dailymotion API.
const postThis = await axios.post('https://upload-xx.xxx.dailymotion.com/upload?uuid=xxxxx&seal=xxxxx&extra=xxxxx', form, {
headers: {
...form.getHeaders,
'Content-Type': 'multipart/formdata',
'Content-Length': fs.statSync('/tmp/video.mp4').size
},
});
console.log({ postThis })
} catch (err) { console.log({ err }) }
})
} catch (err) { console.log({ err }) }
Related
I would like to download a docx file content from a url to S3 using Node JS. Is there a suggested library for doing the same. I tried to download locally using something like this but it turns out the document contents are Gibberish. Is there anything I am missing here.
const axios = require("axios");
const fs = require("fs");
(async function () {
let article;
try {
const httpResponse = await axios.get("https://<url>/Go_Lang.docx?raw=true",
{responseType: 'blob', headers : { 'Accept': "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}});
fs.writeFileSync(“./temp.docx", Buffer.from(httpResponse.data), function (err) {
if (err) console.log(err);
else console.log("done");
});
} catch (err) {
console.log(err)
}
})();
Change responseType to arraybuffer, and you don't need to convert it to Buffer.
Also, .writeFileSync does not take callback, so it's writeFile
try this:
(async function() {
let article;
try {
const httpResponse = await axios.get("https://<url>/Go_Lang.docx?raw=true", {
responseType: 'arraybuffer',
headers: {
'Accept': "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
}
});
fs.writeFile("./temp.docx", httpResponse.data, function (err) {
if (err) console.log(err);
else console.log("done");
});
} catch (err) {
console.log(err)
}
})();
I am trying to send files from Slack to WhatsApp using Node.js. For Slack, I am using Bolt and for WhatsApp API I'm using WATI.
This is the code that retrieves the file information as soon as it is uploaded to slack. I am retrieving the file's permalink and passing it to the load function. I tried using file permalink_public it still gives the same error.
index.js
const wa = require("./wati")
const request = require('request-promise')
url = file_permalink
async function load(uri, path, number) {
const options = {
uri: uri,
encoding: null,
};
const body = await request(options)
try {
await wa.sendMedia(body, path, number).then().catch(e => console.log(e))
}
catch (e) {
console.log(e)
}
}
load(url, "file.png", phone_number)
wati.js
var request = require('request');
const sendMedia = async (file, filename, senderID) => {
var options = {
'method': 'POST',
'url': 'https://' + process.env.URL + '/api/v1/sendSessionFile/' + senderID,
'headers': {
'Authorization': process.env.API,
},
formData: {
'file': {
'value': file,
'options': {
'filename': filename,
'contentType': null
}
}
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
}
Error
{"result":false,"info":"Failed to send file"}
I tried with axios as well but it throws
TypeError: Cannot read properties of undefined (reading 'name')
index.js
let body = await axios.get(url, {
responseType: 'stream',
}).then(async () => {
// console.log(body)
}).catch(function (error) {
if (error.response) {
// Request made and server responded
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
});
await wa.sendMedia(body.data, "file.png", phone_number).then().catch(e => {
console.log(e)
})
I have my function that create requests to get recents tweets by a keyword (I'm using NodeJS), but I need to stop it after 10 tweets, how can I do that? From the twitter api doc I didn't find anything...
Here only mentions the limit but not how to set it
https://developer.twitter.com/en/docs/twitter-api/rate-limits
Here the code:
const rules = [{
value: //keyword
}]
function streamTweets() {
const stream = needle.get(streamURL, {
headers: {
Authorization: `Bearer ${TOKEN}`
}
})
stream.on('data', (data) => {
try {
const json = JSON.parse(data)
console.log(json)
} catch (error) {
}
})
}
(async () => {
let currentRules
try {
currentRules = await getRules()
await deleteRules(currentRules)
await setRules()
} catch (error) {
console.error(error)
process.exit(1)
}
streamTweets()
})()
I'm having troubles saving an incoming webm blob to the server. I'm using react-video-recorder on NextJS like this:
<VideoRecorder
onRecordingComplete={(videoBlob) => {
// Do something with the video...
fetch("/api/video",
method: "post",
body: videoBlob,
})
.then(function (response) {
console.log("done");
return response;
})
.catch(function (err) {
console.log('error', err);
});
console.log(url);
// output: blob:http://localhost:3000/99a5b711-f3d5-481d-9e04-8981d1be3727
console.log(videoBlob);
// output Blob {size: 307028, type: "video/webm;codecs="vp8,opus""}
}}
/>
On the api side I'm trying to save the file like this. It does save something, but is only the first chunk or buffer. How can I capture and write the file to my server?
export default async (req, res) => {
fs.writeFile('test.webm', req.body, function (err) {
if (err) return console.log(err);
console.log('video saved');
} );
}
I did that task by doing this.
I saved the recordingChunks/Video blob to a state and then sent it to the Nodejs server from Reactjs Frontend
FrontEnd code:-
const blob = new Blob(context.data.recordedChunks, {
type: "video/webm",
});
const fd = new FormData()
fd.append('video', blob)
axios.post(`${process.env.REACT_APP_BASE_URL}/video/blob_video`, fd)
.then((res) => console.log(res.data))
.catch((err) => console.log(err))
Backend code:-
router.post('/blob_video', async (req, res) => {
try {
if (req.files.video !== undefined) {
const video = req.files.video // this is your file do what ever you want with it
const videoname = Date.now() + req.files.video.name + ".webm"
video.mv(`${__dirname}/../your_path/${videoname}`, err => {
if (err) {
console.log(err)
return res.json({ err })
}
})
}
res.json(req.body)
} catch (err) {
res.json({ success: false, err: err, msg: "Server error" })
console.log(err)
}
})
Using express-fileupload to upload a file you can do it with your favourite one.
this is how I try it
await this.base64.encodeFile(uri).then((base64File: string) => {
console.log(base64File);
base64_data = base64File
alert(JSON.stringify(base64_data));
console.log(base64_data);
}, (err) => {
console.log(err);
alert(JSON.stringify(err));
});
this.db.putAttachment(att_id,att_name,base64_data, att_file_type).then(function (result) {
console.log(result);
alert(JSON.stringify(result));
}).catch(function (err) {
// ouch, an error
console.log(err);
alert(JSON.stringify(err));
});
it works on image file but it return "" for pdf files
you can convert file data to binary data or base64
this example convert to binary data
first download file with XHR request can use jquery ajax , axios or ...
set responseType: 'arraybuffer'
after download or before download create document in pouch db
axios.get(url, {
progress: false, responseType: 'arraybuffer',
onDownloadProgress: (progressEvent) => {
precent = (100 * progressEvent.loaded / progressEvent.total)
console.log(precent)
}
})
.then(resp => {
//get db
let db = $db.dbModel
//set attach
db.get(doc._id).then((doc) => {
db.putAttachment(doc._id, 'index.mp4', doc._rev, new Blob([new Uint8Array(resp.data)], {type: 'video/mp4'}), 'video/mp4')
.then(res => {
// console.log('success store file')
})
})
})
https://github.com/mohammadnazari110/pwa_offline_video_download