Skip to content

Commit 9be00e8

Browse files
revamp (#14)
This is BREAKING CHANGES!
1 parent 67c2644 commit 9be00e8

File tree

15 files changed

+2612
-1176
lines changed

15 files changed

+2612
-1176
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ on:
1010
branches: ['main']
1111

1212
jobs:
13-
build:
13+
build_and_test:
1414
runs-on: ubuntu-latest
1515

1616
strategy:
1717
matrix:
18-
node-version: [18.x, 20.x]
18+
node-version:
19+
- 20.x
1920
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
2021

2122
steps:
@@ -24,8 +25,7 @@ jobs:
2425
uses: actions/setup-node@v4
2526
with:
2627
node-version: ${{ matrix.node-version }}
27-
cache: 'npm'
2828
- run: npm ci
2929
- run: npm run lint
30-
- run: npm run build --if-present
30+
- run: npm run build
3131
- run: npm test

README.md

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,45 @@
11
# img-dl
22

3-
Downloade image(s), by command or programmatically. The alternative for `image-downloader` package (see the [comparison](#comparison)).
3+
Downloade image(s), by command or programmatically. The alternative for `image-downloader` package (see the [features](#features)).
44

55
[![MIT license](https://img.shields.io/github/license/fityannugroho/img-dl.svg)](https://github.com/fityannugroho/img-dl/blob/main/LICENSE)
66
[![npm version](https://img.shields.io/npm/v/img-dl.svg)](https://www.npmjs.com/package/img-dl)
77
[![npm downloads](https://img.shields.io/npm/dm/img-dl.svg)](https://www.npmjs.com/package/img-dl)
88
[![install size](https://packagephobia.com/badge?p=img-dl)](https://packagephobia.com/result?p=img-dl)
99

10+
## Features
11+
12+
| Features | **img-dl** | [image-downloader][p1] |
13+
| --------------------------- | :--------: | :--------------------: |
14+
| Single download |||
15+
| Bulk download |||
16+
| CLI |||
17+
| Custom filename |||
18+
| Custom extension |||
19+
| Request timeout |||
20+
| Retry failed request |||
21+
| Abort request |||
22+
| **Increment mode (in CLI)** |||
23+
| **Overwrite prevention** |||
24+
25+
### Increment mode
26+
27+
Download images with an url that contains `{i}` placeholder for the index, and specify the start and end index.
28+
29+
### Overwrite prevention
30+
31+
To prevent overwriting, ` (n)` will be appended to the name of the new file if the file with the same name already exists.
32+
33+
The number will be incremented until the file name is unique in the directory, starting from 1 (e.g. `image (1).jpg`, `image (2).jpg`, etc.).
34+
35+
Image with different extension will be considered as **different** file, so it will not be appended with ` (n)`. For example, `image.jpg` and `image.png` will not be considered as the same file.
36+
37+
> This feature will work for both single and bulk download.
38+
1039
## Prerequisites
1140

12-
- Node.js 18 or later
13-
- npm 9 or later
41+
- Node.js 20.9 or later
42+
- npm 10 or later
1443

1544
## Installation
1645

@@ -43,7 +72,7 @@ USAGE
4372
PARAMETERS
4473
url The URL of the image to download. Provide multiple URLs to download multiple images.
4574
In increment mode, the URL must contain {i} placeholder for the index,
46-
only one URL is allowed, and the 'end' flag is required.
75+
only one URL is allowed, and the '--end' is required.
4776
4877
OPTIONS
4978
-d, --dir=<path> The output directory. Default: current working directory
@@ -53,7 +82,7 @@ OPTIONS
5382
-H, --header=<header> The header to send with the request. Can be used multiple times
5483
-i, --increment Enable increment mode. Default: false
5584
--interval=<number> The interval between each batch of requests in milliseconds
56-
-n, --name=<filename> The filename. Default: original filename or timestamp
85+
-n, --name=<filename> The filename. If not specified, the original filename will be used. Default: 'image'
5786
--max-retry=<number> Set the maximum number of times to retry the request if it fails
5887
--silent Disable logging
5988
--start=<number> The start index for increment mode. Default: 0
@@ -76,13 +105,13 @@ EXAMPLES
76105
imgdl https://example.com/image.jpg
77106
```
78107

79-
#### Download multiple images
108+
#### Bulk download
80109

81110
```bash
82111
imgdl https://example.com/image.jpg https://example.com/image2.jpg
83112
```
84113

85-
#### Download multiple images with increment mode
114+
#### Bulk download with increment mode
86115

87116
```bash
88117
imgdl https://example.com/image-{i}.jpg --increment --start=1 --end=10
@@ -95,7 +124,12 @@ imgdl https://example.com/image-{i}.jpg --increment --start=1 --end=10
95124
```js
96125
import imgdl from 'img-dl';
97126

98-
const image = await imgdl('https://example.com/image.jpg');
127+
const image = await new Promise((resolve, reject) => {
128+
imgdl('https://example.com/image.jpg', {
129+
onSuccess: resolve,
130+
onError: reject,
131+
});
132+
});
99133
console.log(image);
100134
/*
101135
{
@@ -110,21 +144,36 @@ console.log(image);
110144
*/
111145
```
112146

113-
#### Download multiple images
147+
#### Bulk download
114148

115149
```js
116150
import imgdl from 'img-dl';
117151

118-
const images = await imgdl([
152+
const urls = [
119153
'https://example.com/image.jpg',
120154
'https://example.com/image2.jpg',
121-
]);
155+
];
156+
157+
await imgdl(urls, {
158+
onSuccess: (image) => {
159+
// Do something with the downloaded image
160+
console.log(image);
161+
},
162+
onError: (error, url) => {
163+
// Do something when the image fails to download
164+
console.error(`Failed to download ${url}: ${error.message}`);
165+
},
166+
});
167+
168+
console.log('Download completed');
122169
```
123170

124171
## API
125172

126173
### imgdl(url, ?options)
127174

175+
Returns: `Promise<void>`
176+
128177
Download image(s) from the given URL(s).
129178

130179
#### `url`
@@ -172,7 +221,7 @@ The interval between each batch of requests in milliseconds when downloading mul
172221
Type: `string`<br>
173222
Default: `'image'`
174223

175-
The filename. If not specified, the original filename will be used. If the original filename is not available, 'image' will be used. <br>When downloading multiple images, `-index` will be appended to the end of the name (suffix). `index` will start from 1. For example: 'image-1'
224+
The filename. If not specified, the original filename will be used. If the original filename is not available, 'image' will be used.
176225

177226
##### `maxRetry`
178227

@@ -186,14 +235,14 @@ Set the maximum number of times to retry the request if it fails.
186235
Type: `(image: Image) => void`<br>
187236
Default: `undefined`
188237

189-
The callback function to be called when the image is successfully downloaded. Only available when downloading multiple images.
238+
The callback function to be called when the image is successfully downloaded.
190239

191240
##### `onError`
192241

193242
Type: `(error: Error, url: string) => void`<br>
194243
Default: `undefined`
195244

196-
The callback function to be called when the image fails to download. Only available when downloading multiple images.
245+
The callback function to be called when the image fails to download.
197246

198247
##### `signal`
199248

@@ -216,20 +265,6 @@ Default: `undefined`
216265

217266
Set timeout for each request in milliseconds.
218267

219-
## Comparison
220-
221-
| Features | **img-dl** | [image-downloader][p1] |
222-
| ------------------------ | :--------: | :--------------------: |
223-
| Download single image |||
224-
| Download multiple images |||
225-
| CLI |||
226-
| Increment download |||
227-
| Custom filename |||
228-
| Custom extension |||
229-
| Request timeout |||
230-
| Retry failed request |||
231-
| Abort request |||
232-
233268
<!-- Project links -->
234269

235270
[p1]: https://www.npmjs.com/package/image-downloader
@@ -238,4 +273,4 @@ Set timeout for each request in milliseconds.
238273

239274
Give a ⭐️ if this project helped you!
240275

241-
You can support this project by donating via [GitHub Sponsors](https://github.com/sponsors/fityannugroho), [Trakteer](https://trakteer.id/fityannugroho/tip), or [Saweria](https://saweria.co/fityannugroho).
276+
Also please consider supporting this project with a **donation**. Your donation will help us maintain and develop this project and provide you with better support.

0 commit comments

Comments
 (0)