4.0.2 • Published 5 years ago

simple-image-downloader v4.0.2

Weekly downloads
66
License
MIT
Repository
github
Last release
5 years ago

Node Image Downloader

npm version codecov

A Node module for downloading image to disk from a given URL

This module is an image-download fork, created by Sébastien Demanou. The original repository is on GitLab, I have migrated it to GitHub.

This module is a fork used to eliminate some errors generated by incorrect timeout management of the original repository, however feel free to open issue or PR for any additional features/errors found.

Install

npm install --save simple-image-downloader

Options

  • url (required) - the image URL to download
  • dest (required) - the image destination. Can be a directory or a filename. If a directory is given, ID will automatically extract the image filename from options.url (see usage bellow)
  • extractFilename - boolean indicating whether the image filename will be automatically extracted from options.url or not. Set to false to have options.dest without a file extension for example. (default: true)
  • headers - HTTP headers (default: {})
  • timeout - milliseconds before a request times out

For advanced options, see Node.js http.request()'s options documentation

Syntax

declare module download {
  image(options: Options): Promise<{ filename: string }>;
}

Usage

Download to a directory and save with the original filename

const download = require('image-downloader')

const options = {
  url: 'http://someurl.com/image.jpg',
  dest: '/path/to/dest'                // will be saved to /path/to/dest/image.jpg
}

download.image(options)
  .then(({ filename }) => {
    console.log('Saved to', filename)  // saved to /path/to/dest/image.jpg
  })
  .catch((err) => console.error(err))

Download to a directory and save with an another filename

const download = require('image-downloader')

options = {
  url: 'http://someurl.com/image2.jpg',
  dest: '/path/to/dest/photo.jpg'      // will be saved to /path/to/dest/photo.jpg
}

download.image(options)
  .then(({ filename }) => {
    console.log('Saved to', filename)  // saved to /path/to/dest/photo.jpg
  })
  .catch((err) => console.error(err))

Download with another filename without extension

const download = require('simple-image-downloader')

options = {
  url: 'http://someurl.com/image3.jpg',
  dest: '/path/to/dest/photo',         // will be saved to /path/to/dest/photo
  extractFilename: false
}

download.image(options)
  .then(({ filename }) => {
    console.log('Saved to', filename)  // saved to /path/to/dest/photo
  })
  .catch((err) => console.error(err))

License

Under the MIT license. See LICENSE file for more details.

4.0.2

5 years ago

4.0.1

5 years ago