Skip to content

Commit

Permalink
Merge pull request #414 from mohamed-bahaa21/master
Browse files Browse the repository at this point in the history
added: stream-multiple example
  • Loading branch information
tomas authored Dec 8, 2023
2 parents 3aaf142 + 280f637 commit f0164f7
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/stream-multiple/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const { stream_multiple } = require('./stream-multiple')
const env = require('./env')

app.use(
bodyParser.urlencoded({ extended: false }),
bodyParser.json(),
express.static(__dirname + '/public'),
);

app.get('/', (req, res) => res.send(`
<h1>Thanks Tomás Pollak</h1>
<a href="/stream_files">Stream Multiple Files</a>
`));

app.get('/stream_multiple_files', (req, res) => stream_multiple(req, res, env._urls, env.stream_dir));

let PORT = process.env.PORT || 3000;
app.listen(PORT, console.log(`Main Server: ${PORT}`));
9 changes: 9 additions & 0 deletions examples/stream-multiple/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
_urls: [
"https://images.unsplash.com/photo-1619410283995-43d9134e7656?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80",
"https://images.unsplash.com/photo-1555949963-aa79dcee981c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80",
"https://images.unsplash.com/photo-1511376777868-611b54f68947?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80"
],
_url: "https://images.unsplash.com/photo-1511376777868-611b54f68947?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80",
stream_dir: `public/`
}
19 changes: 19 additions & 0 deletions examples/stream-multiple/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "express-needle",
"version": "1.0.0",
"description": "Express & Needle are friends <3",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
},
"keywords": [],
"author": "mohamed-bahaa21",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.1",
"express": "^4.18.2",
"fs-extra": "^10.1.0",
"needle": "^3.1.0"
}
}
50 changes: 50 additions & 0 deletions examples/stream-multiple/stream-multiple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var needle = require('needle');
const fs = require('fs-extra')

function stream_multiple(req, res, _urls, stream_dir, index = 0) {
if (index == 0) {
// initial state
}

let writeStream;
const uri = _urls[index];

if (index == undefined) {
index = 0;
stream_multiple(req, res, _urls, stream_dir, index);
} else {

writeStream = fs.createWriteStream(`${stream_dir}` + `${index}.jpeg`);

writeStream.on("ready", () => console.log({ msg: `STREAM::WRITE::READY::${index}` }));
writeStream.on("open", () => console.log({ msg: `STREAM::WRITE::OPEN::${index}` }));
writeStream.on("finish", () => console.log({ msg: `STREAM::WRITE::DONE::${index}` }));

writeStream.on('close', () => {
if (index >= _urls.length - 1) {
res.redirect('/');
} else {
stream_multiple(req, res, _urls, stream_dir, index + 1);
}
})

needle
.get(uri, function (error, response) {
if (response.bytes >= 1) {
// you want to kill our servers
}

if (!error && response.statusCode == 200) {
// good
} else {
// then we can retry later
}
})
.pipe(writeStream)
.on('done', function () {
// needle
});
}
}

module.exports = { stream_multiple }

0 comments on commit f0164f7

Please sign in to comment.