Updated the files.
This commit is contained in:
parent
1553e6b971
commit
753967d4f5
23418 changed files with 3784666 additions and 0 deletions
21
my-app/node_modules/needle/examples/stream-multiple/app.js
generated
vendored
Executable file
21
my-app/node_modules/needle/examples/stream-multiple/app.js
generated
vendored
Executable file
|
@ -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
my-app/node_modules/needle/examples/stream-multiple/env.js
generated
vendored
Executable file
9
my-app/node_modules/needle/examples/stream-multiple/env.js
generated
vendored
Executable file
|
@ -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
my-app/node_modules/needle/examples/stream-multiple/package.json
generated
vendored
Executable file
19
my-app/node_modules/needle/examples/stream-multiple/package.json
generated
vendored
Executable file
|
@ -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
my-app/node_modules/needle/examples/stream-multiple/stream-multiple.js
generated
vendored
Executable file
50
my-app/node_modules/needle/examples/stream-multiple/stream-multiple.js
generated
vendored
Executable file
|
@ -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 }
|
Loading…
Add table
Add a link
Reference in a new issue