jimp并行下载图片

const express = require('express')
const app = express()
const cors = require('cors')
const fs = require('fs')
const path = require('path')
const Jimp = require('jimp')
const https = require('https')
const PromisePool = require('es6-promise-pool')


const imgs = [
'https://readfree.me/media/covers/2018/12/05/cover_QALqTsz.jpg', 
'https://readfree.me/media/covers/2017/04/21/%E7%94%B0.jpg', 
'https://readfree.me/media/covers/2018/08/12/s27310588.jpg'
]

const ps = imgs.map((item) => {
    // 原生方案
    // return new Promise((resolve, reject) => {
    //     https.get(i, (res) => {
    //         const a = i.slice(-8)
    //         res.pipe(fs.createWriteStream(path.join(__dirname, `./static/${a}`))).on("finish", () => {
    //             resolve()
    //             console.log(a)
    //         }).on('error', (err) => {
    //             console.log(err)
    //             resolve()
    //         })
    //     })
    // })
    
    
    // 这种并发有数量限制,imgs具体数量未知
    return new Promise((resolve, reject) => {
       const a = item.slice(-8)
       Jimp.read(item)
            .then(lenna => {
                const url = path.join(__dirname, `./static/${a}`)
                lenna
                    .resize(56, Jimp.AUTO) // resize
                    .write(url) // save
                    console.log(a)
                resolve()
            })
            .catch(err => {
                console.log(err)
                resolve()
            })
    })
    
    // 新的并发方式
     const generatePromises = function* () {
        for (const item of list) {
            yield Jimp.read(`https://readfree.me/${item.src}`)
                .then(lenna => {
                    const url = path.join(__dirname, `../public3/${item.thumbnail}`)
                    lenna
                        .write(url) // save
                    console.log(item._id, item.subject)
                })
                .catch(err => {
                    console.log(err)
                })
        }
    }
    const promiseIterator = generatePromises()
    const pool = new PromisePool(promiseIterator, 5)

    pool.start()
        .then(() => console.log('Complete'))
})


// Promise.all(ps)


app.listen(3002)
console.log('listen 3002')

posted @ 2019-08-31 20:50:26 浏览(32) NodeJS

avatar