nodejs实现下载图片功能

nodejs yekong

在用nodejs仿写单页模板扒手的时候,使用axios下载的图片无法正常打开,所以换了一种方式,下载图片需要两种一种http 一种https,用来下载http和https地址下的图片。


import fs from 'fs'
var Stream = require('stream').Transform
var https = require('https');
var http = require('http');
import {createFileHash256Sync, getFileName, GetRandomNum} from "./utils";
export function downloadImgs(u, p, path) {
    return new Promise((resolve) => {
        var filePath = path + getFileName(u)
        var filePathReName = path + GetRandomNum(1000, 10000) + getFileName(u)
        var filePaths = p + '/' + filePath
        var filePathReNames = p + '/' + filePathReName
        var str = new RegExp("https:");
        if (str.test(u)) {
            https.request(u, function (response) {
                var data = new Stream();
                response.on('data', function (chunk) {
                    data.push(chunk);
                });
                response.on('end', function () {
                    if (!fs.existsSync(p + '/' + path)) {
                        fs.mkdirSync(p + '/' + path);
                    }
                    if (!fs.existsSync(filePaths)) {
                        fs.writeFileSync(filePaths, data.read());
                        resolve(filePath)
                    } else {
                        fs.writeFileSync(filePathReNames, data.read());
                        const hash256Sync = createFileHash256Sync(filePaths)
                        const hash256Syncafter = createFileHash256Sync(filePathReNames)
                        if (hash256Sync == hash256Syncafter) {
                            fs.unlink(filePathReNames, (err, data) => {
                                if (err) {
                                    console.log(err);
                                } else {
                                    // console.log('');
                                    resolve(filePath)
                                }
                            })
                        } else {
                            resolve(filePathReName)
                        }
                    }
                });
            }).end();
        } else {
            http.request(u, function (response) {
                var data = new Stream();
                response.on('data', function (chunk) {
                    data.push(chunk);
                });
                response.on('end', function () {
                    if (!fs.existsSync(p + '/' + path)) {
                        fs.mkdirSync(p + '/' + path);
                    }
                    if (!fs.existsSync(filePaths)) {
                        fs.writeFileSync(filePaths, data.read());
                        resolve(filePath)
                    } else {
                        fs.writeFileSync(filePathReNames, data.read());
                        const hash256Sync = createFileHash256Sync(filePaths)
                        const hash256Syncafter = createFileHash256Sync(filePathReNames)
                        if (hash256Sync == hash256Syncafter) {
                            fs.unlink(filePathReNames, (err, data) => {
                                if (err) {
                                    console.log(err);
                                } else {
                                    // console.log('');
                                    resolve(filePath)
                                }
                            })
                        } else {
                            resolve(filePathReName)
                        }
                    }
                });
            }).end();
        }
    })
}
喜欢