imgbb-uploader
Advanced tools
Comparing version 1.3.5 to 1.4.0
@@ -8,2 +8,2 @@ /** | ||
*/ | ||
export declare const fileToString: (imagePath: string) => Promise<any>; | ||
export declare const fileToString: (imagePath: string) => Promise<string>; |
@@ -15,5 +15,3 @@ "use strict"; | ||
*/ | ||
exports.fileToString = function (imagePath) { | ||
return fs_1.default.promises.readFile(imagePath, "base64").catch(function (e) { return e; }); | ||
}; | ||
exports.fileToString = function (imagePath) { return fs_1.default.promises.readFile(imagePath, "base64"); }; | ||
//# sourceMappingURL=fileToString.js.map |
import { IOptionObject, IResponseObject } from "./interfaces"; | ||
/** | ||
* Upload local pictures files to imgbb API and get display URLs in response. | ||
* Upload local pictures files to imgBB API (or other Chevereto instances) & get display URLs in response. | ||
* | ||
* @param {string} apiKey - Your imgBB API key | ||
* @param {string} apiKey - Your API key | ||
* @param {string} pathToFile - Path to your file | ||
* | ||
* @param {Object} options - OPTIONAL: pass Option object as parameter | ||
* @param {string} options.apiKey - Your imgBB API key | ||
* @param {string} options.apiKey - Your API key | ||
* @param {string} options.imagePath - Path to your image | ||
@@ -15,12 +15,14 @@ * @param {string} options.name - Custom name for your file | ||
* @param {string} options.imageUrl - URL of your image (32Mb max) | ||
* @param {string} options.cheveretoHost - Define to switch into 'chevereto mode' | ||
* @param {string} options.customPayload - Pass custom key-value pairs (in chevereto mode only) | ||
* | ||
* @returns {Promise.<ResponseObject>} | ||
* A promise. Access your data using `.then` as shown in [the README](https://github.com/TheRealBarenziah/imgbb-uploader#use) : | ||
* A promise. See [README](https://github.com/TheRealBarenziah/imgbb-uploader#use) for more infos | ||
* | ||
* @example | ||
* imgbbUploader("your-api-key", "/absolute/path/to/file.jpg") | ||
* imgbbUploader("your-api-key", "path/to/file.jpg") | ||
* .then(res => console.log(res)) | ||
* .catch(err => console.error(err)) | ||
*/ | ||
declare const imgbbUploader: (...args: string[] | IOptionObject[]) => Promise<IResponseObject>; | ||
declare const imgbbUploader: (...args: string[] | IOptionObject[]) => Promise<IResponseObject | string>; | ||
export = imgbbUploader; |
@@ -52,10 +52,11 @@ "use strict"; | ||
var validateInput_1 = require("./validateInput"); | ||
var postToChevereto_1 = require("./postToChevereto"); | ||
/** | ||
* Upload local pictures files to imgbb API and get display URLs in response. | ||
* Upload local pictures files to imgBB API (or other Chevereto instances) & get display URLs in response. | ||
* | ||
* @param {string} apiKey - Your imgBB API key | ||
* @param {string} apiKey - Your API key | ||
* @param {string} pathToFile - Path to your file | ||
* | ||
* @param {Object} options - OPTIONAL: pass Option object as parameter | ||
* @param {string} options.apiKey - Your imgBB API key | ||
* @param {string} options.apiKey - Your API key | ||
* @param {string} options.imagePath - Path to your image | ||
@@ -66,8 +67,10 @@ * @param {string} options.name - Custom name for your file | ||
* @param {string} options.imageUrl - URL of your image (32Mb max) | ||
* @param {string} options.cheveretoHost - Define to switch into 'chevereto mode' | ||
* @param {string} options.customPayload - Pass custom key-value pairs (in chevereto mode only) | ||
* | ||
* @returns {Promise.<ResponseObject>} | ||
* A promise. Access your data using `.then` as shown in [the README](https://github.com/TheRealBarenziah/imgbb-uploader#use) : | ||
* A promise. See [README](https://github.com/TheRealBarenziah/imgbb-uploader#use) for more infos | ||
* | ||
* @example | ||
* imgbbUploader("your-api-key", "/absolute/path/to/file.jpg") | ||
* imgbbUploader("your-api-key", "path/to/file.jpg") | ||
* .then(res => console.log(res)) | ||
@@ -82,5 +85,5 @@ * .catch(err => console.error(err)) | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, _b, _c, apiKey, name_1, expiration, image, e_1; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
var image, _a, apiKey, name_1, expiration, cheveretoHost, customPayload, image, e_1; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
@@ -90,10 +93,10 @@ if (!(args.length === 2)) return [3 /*break*/, 5]; | ||
case 1: | ||
if (!_d.sent()) return [3 /*break*/, 3]; | ||
_a = postToImgbb_1.postToImgbb; | ||
_b = { | ||
apiKey: String(args[0]) | ||
}; | ||
if (!_b.sent()) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, fileToString_1.fileToString(String(args[1]))]; | ||
case 2: return [2 /*return*/, _a.apply(void 0, [(_b.image = _d.sent(), | ||
_b)])]; | ||
case 2: | ||
image = _b.sent(); | ||
return [2 /*return*/, postToImgbb_1.postToImgbb({ | ||
apiKey: String(args[0]), | ||
image: image, | ||
})]; | ||
case 3: throw new Error("Invalid params: please make sure that first argument is an imgBB API key, and second argument is a valid path to image file."); | ||
@@ -103,20 +106,33 @@ case 4: return [3 /*break*/, 11]; | ||
if (!(args.length === 1 && typeof args[0] === "object")) return [3 /*break*/, 10]; | ||
_c = __assign({}, args[0]), apiKey = _c.apiKey, name_1 = _c.name, expiration = _c.expiration; | ||
_d.label = 6; | ||
_a = __assign({}, args[0]), apiKey = _a.apiKey, name_1 = _a.name, expiration = _a.expiration, cheveretoHost = _a.cheveretoHost, customPayload = _a.customPayload; | ||
_b.label = 6; | ||
case 6: | ||
_d.trys.push([6, 8, , 9]); | ||
_b.trys.push([6, 8, , 9]); | ||
return [4 /*yield*/, validateInput_1.validateOptionObject(__assign({}, args[0]))]; | ||
case 7: | ||
image = _d.sent(); | ||
return [2 /*return*/, postToImgbb_1.postToImgbb({ | ||
apiKey: String(apiKey), | ||
image: image, | ||
name: name_1, | ||
expiration: expiration, | ||
})]; | ||
image = _b.sent(); | ||
// no cheveretoHost defined => post to imgBB | ||
if (!cheveretoHost) { | ||
return [2 /*return*/, postToImgbb_1.postToImgbb({ | ||
apiKey: String(apiKey), | ||
image: image, | ||
name: name_1, | ||
expiration: expiration, | ||
})]; | ||
} | ||
// cheveretoHost is defined: switch in chevereto mode | ||
else { | ||
return [2 /*return*/, postToChevereto_1.postToChevereto({ | ||
apiKey: String(apiKey), | ||
image: image, | ||
cheveretoHost: cheveretoHost, | ||
customPayload: customPayload, | ||
})]; | ||
} | ||
return [3 /*break*/, 9]; | ||
case 8: | ||
e_1 = _d.sent(); | ||
e_1 = _b.sent(); | ||
throw new Error(String(e_1)); | ||
case 9: return [3 /*break*/, 11]; | ||
case 10: throw new Error("It seems you didn't pass your arguments properly! Please check imgbbUploader documentation here:\nhttps://github.com/TheRealBarenziah/imgbb-uploader/tree/master"); | ||
case 10: throw new Error("It seems you didn't pass your arguments properly! Please check the documentation here:\nhttps://github.com/TheRealBarenziah/imgbb-uploader/tree/master"); | ||
case 11: return [2 /*return*/]; | ||
@@ -123,0 +139,0 @@ } |
@@ -40,2 +40,4 @@ export interface IResponseObject { | ||
imageUrl?: string; | ||
cheveretoHost?: string; | ||
customPayload?: Record<string, unknown>; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { IResponseObject } from "./interfaces"; | ||
import { IOptionObject, IResponseObject } from "./interfaces"; | ||
/** | ||
@@ -6,14 +6,12 @@ * Now using the standard 'https' module instead of 'request' deprecated dependency. | ||
* To tweak the method, edit 'postToImgbb.ts' with the help of [the docs](https://nodejs.org/api/https.html#https_https_request_options_callback) | ||
* | ||
* @param {string} apiKey - Your imgBB API key | ||
* @param {string} imageing - Typically, the output of fileToString("path") function | ||
* @param {string} image - Typically, the output of fileToString("path") function | ||
* | ||
* @returns A promise. Use `.then` as shown in [the README](https://github.com/TheRealBarenziah/imgbb-uploader#use) : | ||
* @returns A promise. [README](https://github.com/TheRealBarenziah/imgbb-uploader#use) : | ||
*/ | ||
interface IPostParams { | ||
apiKey: string; | ||
image?: string; | ||
name?: string; | ||
expiration?: number; | ||
interface IPostParams extends IOptionObject { | ||
image: string; | ||
} | ||
export declare const postToImgbb: (params: IPostParams) => Promise<IResponseObject>; | ||
export {}; |
@@ -34,2 +34,3 @@ "use strict"; | ||
exports.postToImgbb = void 0; | ||
/* eslint-disable */ | ||
var https = __importStar(require("https")); | ||
@@ -40,6 +41,7 @@ var querystring = __importStar(require("querystring")); | ||
var _a = __assign({}, params), apiKey = _a.apiKey, image = _a.image, _b = _a.name, name = _b === void 0 ? null : _b, _c = _a.expiration, expiration = _c === void 0 ? null : _c; | ||
// query string & payload structures are different for imgBB & chevereto-free | ||
var query = "/1/upload?key=" + apiKey; | ||
var payload = querystring.stringify({ | ||
image: image, | ||
}); | ||
var query = "/1/upload?key=" + apiKey; | ||
if (name) | ||
@@ -69,3 +71,3 @@ query += "&name=" + encodeURIComponent(name); | ||
var error = { | ||
message: "There was a problem with imgBB API", | ||
message: "imgBB API returned an error", | ||
imgbbApiResponse: JSON.parse(response), | ||
@@ -72,0 +74,0 @@ }; |
import { IOptionObject } from "./interfaces"; | ||
export declare const validateStringInput: (apiKey: string | null, path: string) => Promise<boolean>; | ||
export declare const isFile: (path: string) => Promise<boolean>; | ||
/** | ||
* Formally validate input 2 strings params | ||
* | ||
* @param {string} apiKey - Should be 32-character long string | ||
* @param {string} path - Should be a valid file path | ||
* | ||
* @returns {Promise.<Boolean>} | ||
* A promise that resolve to `true` if things are looking good, and to `false` otherwise | ||
*/ | ||
export declare const validateStringInput: (apiKey: string | undefined, path: string) => Promise<boolean>; | ||
/** | ||
* Formally validate option object. Either return proper string or throws | ||
@@ -9,4 +19,4 @@ * | ||
* @returns {Promise.<Boolean>} | ||
* A promise that resolve to a valid "image" value if things are looking good, and throws otherwise | ||
* A promise that resolve to a valid "image" value if things are looking good, and throws otherwise | ||
*/ | ||
export declare const validateOptionObject: (options: IOptionObject) => Promise<string | undefined>; | ||
export declare const validateOptionObject: (options: IOptionObject) => Promise<string>; |
@@ -53,5 +53,16 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateOptionObject = exports.validateStringInput = void 0; | ||
exports.validateOptionObject = exports.validateStringInput = exports.isFile = void 0; | ||
var fs_1 = __importDefault(require("fs")); | ||
var fileToString_1 = require("./fileToString"); | ||
exports.isFile = function (path) { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, fs_1.default.promises | ||
.lstat(path) | ||
.then(function (res) { return res.isFile(); }) | ||
.catch(function () { return false; })]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
} | ||
}); | ||
}); }; | ||
/** | ||
@@ -64,21 +75,53 @@ * Formally validate input 2 strings params | ||
* @returns {Promise.<Boolean>} | ||
* A promise that resolve to `true` if things are looking good, and to `false` otherwise | ||
* A promise that resolve to `true` if things are looking good, and to `false` otherwise | ||
*/ | ||
var looksLikeApiKey = function (value) { | ||
return value && value.length === 32 ? true : false; | ||
}; | ||
exports.validateStringInput = function (apiKey, path) { return __awaiter(void 0, void 0, void 0, function () { | ||
var file; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, fs_1.default.promises | ||
.lstat(path) | ||
.then(function (res) { return res.isFile(); }) | ||
.catch(function () { return false; })]; | ||
case 1: | ||
file = _a.sent(); | ||
return [2 /*return*/, file && looksLikeApiKey(apiKey) ? true : false]; | ||
case 0: return [4 /*yield*/, exports.isFile(path)]; | ||
case 1: return [2 /*return*/, (_a.sent()) && apiKey ? true : false]; | ||
} | ||
}); | ||
}); }; | ||
var validateImageInput = function (_a) { | ||
var imagePath = _a.imagePath, base64string = _a.base64string, imageUrl = _a.imageUrl; | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var oopsie, validPath; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
oopsie = Error("A single input key must be defined between: 'imagePath', 'imageUrl', 'base64string'."); | ||
if (!imagePath) return [3 /*break*/, 6]; | ||
return [4 /*yield*/, exports.isFile(imagePath)]; | ||
case 1: | ||
validPath = _b.sent(); | ||
if (!(base64string || imageUrl)) return [3 /*break*/, 2]; | ||
throw oopsie; | ||
case 2: | ||
if (!!validPath) return [3 /*break*/, 3]; | ||
throw Error("'imagePath' seem invalid (" + imagePath + ")"); | ||
case 3: return [4 /*yield*/, fileToString_1.fileToString(imagePath)]; | ||
case 4: return [2 /*return*/, _b.sent()]; | ||
case 5: return [3 /*break*/, 7]; | ||
case 6: | ||
if (base64string) { | ||
if (imageUrl) { | ||
throw oopsie; | ||
} | ||
else { | ||
return [2 /*return*/, base64string]; | ||
} | ||
} | ||
else if (imageUrl) { | ||
return [2 /*return*/, imageUrl]; | ||
} | ||
else { | ||
throw oopsie; | ||
} | ||
_b.label = 7; | ||
case 7: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -90,16 +133,21 @@ * Formally validate option object. Either return proper string or throws | ||
* @returns {Promise.<Boolean>} | ||
* A promise that resolve to a valid "image" value if things are looking good, and throws otherwise | ||
* A promise that resolve to a valid "image" value if things are looking good, and throws otherwise | ||
*/ | ||
exports.validateOptionObject = function (options) { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, _b, imagePath, _c, apiKey, _d, | ||
// name = null, | ||
expiration, _e, base64string, _f, imageUrl, oopsie, e_1; | ||
return __generator(this, function (_g) { | ||
switch (_g.label) { | ||
case 0: | ||
_g.trys.push([0, 7, , 8]); | ||
_a = __assign({}, options), _b = _a.imagePath, imagePath = _b === void 0 ? null : _b, _c = _a.apiKey, apiKey = _c === void 0 ? null : _c, _d = _a.expiration, expiration = _d === void 0 ? null : _d, _e = _a.base64string, base64string = _e === void 0 ? null : _e, _f = _a.imageUrl, imageUrl = _f === void 0 ? null : _f; | ||
oopsie = Error("A single input key must be defined between: 'imagePath', 'imageUrl', 'base64string'."); | ||
if (!looksLikeApiKey(apiKey)) | ||
throw new Error("'apiKey' looks invalid (should be 32 characters long)."); | ||
var _a, _b, imagePath, _c, apiKey, _d, expiration, _e, base64string, _f, imageUrl, _g, cheveretoHost; | ||
return __generator(this, function (_h) { | ||
try { | ||
_a = __assign({}, options), _b = _a.imagePath, imagePath = _b === void 0 ? undefined : _b, _c = _a.apiKey, apiKey = _c === void 0 ? undefined : _c, _d = _a.expiration, expiration = _d === void 0 ? undefined : _d, _e = _a.base64string, base64string = _e === void 0 ? undefined : _e, _f = _a.imageUrl, imageUrl = _f === void 0 ? undefined : _f, _g = _a.cheveretoHost, cheveretoHost = _g === void 0 ? undefined : _g; | ||
// case 1: validate inputs before !imgBB chevereto API call | ||
if (cheveretoHost) { | ||
return [2 /*return*/, validateImageInput({ | ||
imagePath: imagePath, | ||
imageUrl: imageUrl, | ||
base64string: base64string, | ||
})]; | ||
} | ||
// case 2: validate inputs before imgBB API call | ||
else { | ||
if (!apiKey) | ||
throw new Error("no 'apiKey' provided."); | ||
if (expiration) { | ||
@@ -113,33 +161,17 @@ if (typeof expiration !== "number") { | ||
} | ||
if (!imagePath) return [3 /*break*/, 5]; | ||
if (!(base64string || imageUrl)) return [3 /*break*/, 1]; | ||
throw oopsie; | ||
case 1: | ||
if (!!exports.validateStringInput(apiKey, imagePath)) return [3 /*break*/, 2]; | ||
throw Error("'imagePath' seem invalid (" + imagePath + ")"); | ||
case 2: return [4 /*yield*/, fileToString_1.fileToString(imagePath)]; | ||
case 3: return [2 /*return*/, _g.sent()]; | ||
case 4: return [3 /*break*/, 6]; | ||
case 5: | ||
if (base64string) { | ||
if (imageUrl) | ||
throw oopsie; | ||
else | ||
return [2 /*return*/, base64string]; | ||
} | ||
else if (imageUrl) { | ||
// todo: some research on imgBB opinions before pasting a regex | ||
return [2 /*return*/, imageUrl]; | ||
} | ||
else | ||
throw oopsie; | ||
_g.label = 6; | ||
case 6: return [3 /*break*/, 8]; | ||
case 7: | ||
e_1 = _g.sent(); | ||
throw new Error(String(e_1)); | ||
case 8: return [2 /*return*/]; | ||
// todo: if(!nameLooksValid(name))... | ||
return [2 /*return*/, validateImageInput({ | ||
apiKey: apiKey, | ||
imagePath: imagePath, | ||
imageUrl: imageUrl, | ||
base64string: base64string, | ||
})]; | ||
} | ||
} | ||
catch (e) { | ||
throw new Error(String(e)); | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); }; | ||
//# sourceMappingURL=validateInput.js.map |
{ | ||
"name": "imgbb-uploader", | ||
"version": "1.3.5", | ||
"description": "Lightweight module to upload images through Imgbb API.", | ||
"version": "1.4.0", | ||
"description": "Lightweight module to upload images through imgBB and other chevereto-based APIs.", | ||
"main": "lib/cjs/index.js", | ||
@@ -9,3 +9,2 @@ "browser": "lib/cjs/index.js", | ||
"types": "lib/index.d.ts", | ||
"dependencies": {}, | ||
"scripts": { | ||
@@ -19,6 +18,6 @@ "dev": "npm run build && npm run format && npm run lint && npm run test && npm run remap", | ||
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"", | ||
"lint": "tslint -p tsconfig.json", | ||
"lint": "eslint --fix .", | ||
"clean": "node clean.js", | ||
"prepare": "npm run build", | ||
"prepublishOnly": "npm test && npm run lint", | ||
"prepublishOnly": "npm run build && npm test && npm run lint", | ||
"preversion": "npm run lint", | ||
@@ -38,10 +37,13 @@ "version": "npm run format && git add -A src", | ||
"@types/node": "^14.0.27", | ||
"@typescript-eslint/eslint-plugin": "^4.27.0", | ||
"@typescript-eslint/parser": "^4.27.0", | ||
"@typescript-eslint/eslint-plugin": "^4.33.0", | ||
"@typescript-eslint/parser": "^4.33.0", | ||
"axios": "^0.21.4", | ||
"dotenv": "^8.2.0", | ||
"eslint": "^7.28.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint": "^7.32.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-import": "^2.22.0", | ||
"eslint-plugin-jsdoc": "^39.2.9", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-prefer-arrow": "^1.2.3", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"husky": "^4.2.5", | ||
@@ -51,3 +53,3 @@ "jest": "^26.4.0", | ||
"pretty-quick": "^2.0.1", | ||
"remap-istanbul": "git:github.com/TheRealBarenziah/remap-istanbul", | ||
"remap-istanbul": "0.13.0", | ||
"rimraf": "^3.0.2", | ||
@@ -57,4 +59,2 @@ "sharp": "^0.26.3", | ||
"ts-jest": "^26.2.0", | ||
"tslint": "^6.1.3", | ||
"tslint-config-prettier": "^1.18.0", | ||
"typescript": "^3.9.9", | ||
@@ -71,6 +71,12 @@ "waifu-generator": "^2.0.4" | ||
"image upload", | ||
"picture upload" | ||
"picture upload", | ||
"chevereto", | ||
"chevereto-free", | ||
"upload" | ||
], | ||
"author": "TheRealBarenziah", | ||
"license": "SEE LICENSE IN TARGARYEN_UNLICENSE", | ||
"engines": { | ||
"node": ">=8.3.0" | ||
}, | ||
"bugs": { | ||
@@ -77,0 +83,0 @@ "url": "https://github.com/TheRealBarenziah/imgbb-uploader/issues" |
# imgbb-uploader | ||
Lightweight Nodejs module to upload local pictures files to imgbb API and get display URLs in response. | ||
Lightweight Nodejs module to upload pictures to imgBB (or other chevereto-based APIs) and get display URLs in response. | ||
Primary use is letting imgBB handle the hosting & serving of images. | ||
@@ -9,19 +9,27 @@ | ||
[![dependencies](https://img.shields.io/badge/dependencies-0-brightgreen)](https://www.npmjs.com/package/imgbb-uploader?activeTab=dependencies) | ||
[![Build Status](https://travis-ci.org/TheRealBarenziah/imgbb-uploader.svg?branch=master)](https://travis-ci.org/TheRealBarenziah/imgbb-uploader) | ||
![Test suite](https://github.com/TheRealBarenziah/imgbb-uploader/actions/workflows/tests.yml/badge.svg) | ||
## Install | ||
```bash | ||
npm install imgbb-uploader | ||
``` | ||
## Compatibility: | ||
**Node >= 8** ( [async/await](https://node.green/) ) | ||
**Node >= 8** ( [Promises/await](https://node.green/) ) | ||
_Care: this module uses `fs` under the hood. **It WON'T work outside the node environment !**_ | ||
[I want to use this client-side](https://stackoverflow.com/a/63669049/11894221) | ||
**Want to use this client-side?** [Click here](https://stackoverflow.com/a/63669049/11894221) | ||
**Formats supported by ImgBB API:** `.jpg`, `.png`,`.bmp`,`.gif`, `base64`, `url`. | ||
## Install | ||
## We also support Chevereto v3 & v4! | ||
`npm install imgbb-uploader` | ||
Did you know? imgBB is based on **Chevereto**, a software written by [rodber](https://github.com/rodber) that [you can easily host yourself](https://github.com/TheRealBarenziah/imgbb-uploader/blob/master/CONTRIBUTING.md#docker-container-run-chevereto). | ||
## Use with two string params (legacy, LTS) | ||
**[To use with Chevereto, click here](https://github.com/TheRealBarenziah/imgbb-uploader/blob/master/USE_WITH_CHEVERETO.md)**! | ||
## Upload to imgBB with two string params (legacy) | ||
- I) [Get a free API key from imgbb](https://api.imgbb.com/) ( estimated time ~1 minute ) | ||
@@ -39,3 +47,3 @@ - II) [Put that in an environment variable](https://www.npmjs.com/package/dotenv) | ||
### `.then((response) => console.log(response))` output example : | ||
## `.then((response) => console.log(response))` output example : | ||
@@ -77,12 +85,9 @@ ```javascript | ||
This async function returns a promise: | ||
`console.log(imgbbUploader(myKey, myPath)) // Promise { <pending> }` | ||
Use `await` or `.then` as shown above. | ||
_**Note about imgBB API:** the `medium` Object will only be returned for `.png` and `base64` files !_ | ||
## Use with options object (more features, yay! ) | ||
## With options object (more features, yay! ) | ||
From version 1.2.0 onward, you can also pass an options object as param. | ||
Use it to customize filename and/or a set duration after which the image will be deleted, [cf their docs](https://api.imgbb.com/). | ||
From version 1.2.0 onward, you can pass an options object as param. | ||
Use it to customize filename and/or a set duration after which the image will be deleted, [cf their docs](https://api.imgbb.com/). | ||
The key you'll use for your image depends on its nature. **One of these must be defined:** | ||
@@ -105,3 +110,3 @@ | ||
expiration: 3600 /* OPTIONAL: pass a numeric value in seconds. | ||
It must be in the 60-15552000 range (POSIX time ftw). | ||
It must be in the 60-15552000 range. | ||
Enable this to force your image to be deleted after that time. */, | ||
@@ -113,8 +118,3 @@ | ||
"iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNcLVNbzwAEjDAGACcSA4kB6ARiAAAAAElFTkSuQmCC", | ||
/* OPTIONAL: pass base64-encoded image (max 32Mb) | ||
Enable this to upload base64-encoded image directly as string. (available from 1.3.0 onward) | ||
Allows to work with RAM directly for increased performance (skips fs I/O calls). | ||
Beware: options.imagePath will be ignored as long as options.base64string is defined! | ||
*/ | ||
// OPTIONAL: pass base64-encoded image (max 32Mb) | ||
}; | ||
@@ -130,25 +130,26 @@ | ||
## More examples using option object | ||
#### Another example: handling buffer with option object | ||
Using options.base64string: | ||
```javascript | ||
const imgbbUploader = require("imgbb-uploader"); | ||
// Some buffer we need to upload | ||
const data = "definitely-not-an-image-binary"; | ||
// Some promise of base64 data | ||
const base64str = () => | ||
const bufferToBase64 = (buffer) => | ||
new Promise((resolve) => { | ||
const buff = new Buffer(buffer); | ||
const base64string = buff.toString("base64"); // https://nodejs.org/api/buffer.html#buftostringencoding-start-end | ||
return setTimeout(() => { | ||
resolve( | ||
"iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNcLVNbzwAEjDAGACcSA4kB6ARiAAAAAElFTkSuQmCC", | ||
); | ||
resolve(base64string); | ||
}, 1000); | ||
}); | ||
// Your barebone async function | ||
const myUrl = async (name) => { | ||
// Some async function | ||
const getDisplayUrl = async (buffer, name = "Default-filename") => { | ||
return await imgbbUploader({ | ||
apiKey: "definitely-not-a-valid-key", | ||
base64string: await base64str(), | ||
name: name, | ||
base64string: await bufferToBase64(buffer), | ||
name, | ||
}) | ||
@@ -165,8 +166,10 @@ .then((res) => { | ||
myUrl("Dolunay_Obruk-Sama_<3"); | ||
const myUrl = getDisplayUrl(data, "Dolunay_Obruk-Sama_<3"); | ||
``` | ||
## Learn more | ||
## Working with directories/arrays | ||
This module doesn't support array uploads. For heavy duty, you'll probably have to work with [fs.readdir](https://nodejs.org/api/fs.html#fspromisesreaddirpath-options) and maybe [async forEach](https://www.npmjs.com/package/async-foreach). For example, you can create a `baseDir.js` file wherever it suits you: | ||
_This module don't_ **and won't** directly _support array uploads_. Only you can pick the best solution for your usecase. | ||
For example, to upload local directories of pictures, I enjoy working with [fs.readdir](https://nodejs.org/api/fs.html#fspromisesreaddirpath-options). | ||
I usually create an `imagesDir.js` file wherever it suits me: | ||
@@ -177,6 +180,12 @@ ```javascript | ||
Then you can require it elsewhere and use something like `path.join(myDirpath, "subfolder")` to dig into directories programmatically. | ||
Once there you can for example `fs.readdir` and iterate `forEach` file of that directory. | ||
See `fs` documentation and Stack Overflow for more inspiration on the matter. | ||
Then `require` that elsewhere and use `path.join(imagesDir, relevantSubfolder)` to dig into directories. | ||
Once there, iterate using `fs.readdir` and `forEach` as needed. | ||
If you need more inspiration [Stack Overflow should have you covered](https://www.google.com/search?q=site%3Astackoverflow.com+promise.all)! | ||
## Contributing | ||
Issues & PRs are very welcome! | ||
[Get started with local development](https://github.com/TheRealBarenziah/imgbb-uploader/blob/master/CONTRIBUTING.md) | ||
[CHANGELOG](https://github.com/TheRealBarenziah/imgbb-uploader/blob/master/CHANGELOG.md) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39831
14
659
184
26
3