UNPKG

cky-image-public

Version:

public image to public host

619 lines (600 loc) 22.4 kB
# cky-image-public cky-image-public is a tool upload image file from local address to some host free or non-free with api_key, auth_token, ... > All bug or issue please send to email: Chickyky@gmail.com # Features! - public image to imgur, imgbb, dropbox, google drive ... - upload to all host you config or random, host you want, or fallback if a host upload failed ### Installation ```sh $ npm install cky-image-public --save ``` ### function * **contructor (options)** * `options` **<Object>**: object init which host you use * `cloudinary: true` if you use cloudinary and have `CLOUDINARY_URL` in environment with format `cloudinary://<api_key>:<api_secret>@<cloud_name>` or you can use: ``` cloudinary: { cloud_name: <your_cloud_name>, api_key: <your_api_key>, api_secret: <your_api_secret> } ``` * `drive: { access_token: <your_google_drive_access_token> }` your image will store in your google drive * `dropbox: { token: <your_dropbox_token> }` your image will store in dropbox * `filestack: { api_key: <your_filestack_api_key> }` image will upload to `https://www.filestack.com/` * `imageshack: { api_key: <your_imageshack_api_key> }` image will upload to `https://imageshack.com`, read `https://imageshack.com/contact/api` for get `api_key` * `imgbb: true` image will upload to `https://imgbb.com/` * `imgbox: true` image will upload to `https://imgbox.com/` * `imgeto: true` image will upload to `https://imge.to/` * `imggmi: { cookie: <cookie_string_to_bypass_CloudfareProtection> }` cookie string with 3 properties `__cfduid`, `cf_clearance`, `PHPSESSID` for bypass cloud fare protection, image will upload to `https://imggmi.com/` * `imgur: true` image will upload to `https://imgur.com/` * `pinterest: { token: <your_pinterest_token> }` when you register account complete in `https://www.pinterest.com/`, you open `dev-tool` and find your `csrftoken` in `Cookies` * `postimg: true` image will upload to `https://postimages.org/` * **upload (options, callback)** * `options` ***<[Object](http://chickyky.com)>***: object have information to upload * `hosts` ***<Array>(optional)*** : host use to upload (use in case you dont want use hosts you config when init lib), each element is `String` and includes in `[IMGBB, IMGUR, FILESTACK, DROPBOX, DRIVE, PINTEREST, POSTIMG, IMAGESHACK, IMGBOX, CLOUDINARY, IMGETO, IMGGMI]`, this is all host lib support in current. * `type` ***<String|Enum> (required)*** : `ALL`|`RANDOM`|`FALLBACK`|`ROUNDROBIN` , default is `ALL` * `ALL`: upload to all host you config when init or `hosts` you pass in params `options` * `RANDOM`: upload random in host * `FALLBACK`: try fallback to next host if a host upload failed * `ROUNDROBIN`: upload roundrobin between host * `filePath` ***<String> (required)***: full address local file image to upload * `callback` ***<[function](http://chickyky.com)>***: callback function when upload complete with `params_1` is `errror`, `params_2` is `result` * `result` is **Object**, with format: ``` { direct_url: 'xxx', image_url: 'yyy', urls: ['xxx', 'yyy', 'zzz'], response: [Object] } ``` direct_url: try get direct url return from host image_url: try get page share image of host urls: is array try get all url image return from host (thumb, square, ...) response: is Ojbect response from host, you can use this for get url image ---- > in other case, you alse use those function with same params: `(filePath, callback, hosts)` with `hosts is option` * ***uploadAll*** * ***uploadFallback*** * ***uploadRoundRobin*** * ***uploadRandom*** ### init lib you can use this lib by ```javascript const { ImgPub } = require('cky-image-public'); // this lib or const { Imgur } = require('cky-image-public'); // a fragment of lib ``` ```javascript // this lib export some host to use single module.exports = { ImgPublic, ImgBB, Imgur, Filestack, Dropbox, Drive, Pinterest, Postimg, Imageshack, Imgbox, Cloudinary, Imgeto, Imggmi, }; ``` ### Usage Example ```javascript const path = require('path'); // get environment require('dotenv').config({ path: path.join(__dirname, '../.env') }) const { ImgPublic } = require('cky-image-public'); // use this lib "ImgPub" const GOOGLE_ACCESS_TOKEN = process.env.GOOGLE_ACCESS_TOKEN; const DROPBOX_ACCESS_TOKEN = process.env.DROPBOX_ACCESS_TOKEN; const IMAGESHACK_API_KEY = process.env.IMAGESHACK_API_KEY; const PINTEREST_TOKEN = process.env.PINTEREST_TOKEN; const FILESTACK_API_KEY = process.env.FILESTACK_API_KEY; let imgPub = new ImgPublic({ cloudinary: true, drive: { access_token: GOOGLE_ACCESS_TOKEN, }, dropbox: { token: DROPBOX_ACCESS_TOKEN }, filestack: { api_key: FILESTACK_API_KEY }, imageshack: { api_key: IMAGESHACK_API_KEY }, imgbb: true, imgbox: true, imgeto: true, imggmi: { cookie: '__cfduid=de698bbc3991b2917e63ed656f85b40c01565343963; cf_clearance=4d81408955ae900654d67dbc01e43da8388ef98b-1565343967-1800-150; PHPSESSID=nnnsn1obsf87quonbdcinhkr31;' }, imgur: { accesstoken: false }, pinterest: { token: PINTEREST_TOKEN }, postimg: true, }); imgPub.upload({ filePath: path.join(__dirname, 'spiderman.jpg'), type: 'ALL', // default is ALL, or use uploadAll }, (err, result) => { console.log('err=', err); console.log('result=', JSON.stringify(result)); }) ``` result will like this: ```json { "pinterest": { "direct_url": "https://s3.amazonaws.com/media.pinterest.com/previews/Uw7TyLfH.jpeg", "image_url": "https://s3.amazonaws.com/media.pinterest.com/previews/Uw7TyLfH.jpeg", "urls": [ "https://s3.amazonaws.com/media.pinterest.com/previews/Uw7TyLfH.jpeg" ], "response": { "width": 360, "height": 540, "image_url": "https://s3.amazonaws.com/media.pinterest.com/previews/Uw7TyLfH.jpeg", "success": true, "filename": null } }, "imggmi": { "direct_url": "https://cdn1.imggmi.com/uploads/2019/8/14/5256015929cbc8662d97ab41d1c2f5cb-full.jpg", "image_url": "https://cdn1.imggmi.com/uploads/2019/8/14/5256015929cbc8662d97ab41d1c2f5cb-full.jpg", "urls": [ "https://cdn1.imggmi.com/uploads/2019/8/14/5256015929cbc8662d97ab41d1c2f5cb-full.jpg", "https://cdn3.imggmi.com/uploads/2019/8/14/5256015929cbc8662d97ab41d1c2f5cb-prev.jpg" ], "response": { "error": false, "redirect_path": "https://imggmi.com/uploaded/2019/8/14/5256015929cbc8662d97ab41d1c2f5cb-full.jpg.html", "viewerLink": "https://cdn1.imggmi.com/uploads/2019/8/14/5256015929cbc8662d97ab41d1c2f5cb-full.jpg", "previewLink": "https://cdn3.imggmi.com/uploads/2019/8/14/5256015929cbc8662d97ab41d1c2f5cb-prev.jpg" } }, "cloudinary": { "direct_url": "http://res.cloudinary.com/chickyky/image/upload/v1565776619/qeazc3c2yuwxmpjd8k1m.jpg", "image_url": "http://res.cloudinary.com/chickyky/image/upload/v1565776619/qeazc3c2yuwxmpjd8k1m.jpg", "urls": [ "http://res.cloudinary.com/chickyky/image/upload/v1565776619/qeazc3c2yuwxmpjd8k1m.jpg", "https://res.cloudinary.com/chickyky/image/upload/v1565776619/qeazc3c2yuwxmpjd8k1m.jpg" ], "response": { "public_id": "qeazc3c2yuwxmpjd8k1m", "version": 1565776619, "signature": "85e31c922013f5a6b03add4c3517694b4ee21fa3", "width": 360, "height": 540, "format": "jpg", "resource_type": "image", "created_at": "2019-08-14T09:56:59Z", "tags": [], "bytes": 229156, "type": "upload", "etag": "3faa07e151badd17a6c05d1b6379795d", "placeholder": false, "url": "http://res.cloudinary.com/chickyky/image/upload/v1565776619/qeazc3c2yuwxmpjd8k1m.jpg", "secure_url": "https://res.cloudinary.com/chickyky/image/upload/v1565776619/qeazc3c2yuwxmpjd8k1m.jpg", "original_filename": "4" } }, "imgur": { "direct_url": "https://imgur.com/GJFQDVk", "image_url": "https://imgur.com/GJFQDVk", "urls": [ "https://imgur.com/GJFQDVk" ], "response": { "data": { "hashes": [ "GJFQDVk" ], "hash": "GJFQDVk", "deletehash": "DtT5dmh1bth03Cv", "ticket": false, "album": false, "edit": false, "gallery": null, "poll": false, "animated": false, "height": 540, "width": 360, "ext": ".jpg", "msid": "69b8eee2510e7d123c76d9c62a62d0f7" }, "success": true, "status": 200 } }, "imgeto": { "direct_url": "https://c.imge.to/2019/08/14/8qJfG.jpg", "image_url": "https://imge.to/i/8qJfG", "urls": [], "response": { "status_code": 200, "success": { "message": "image uploaded", "code": 200 }, "image": { "name": "8qJfG", "extension": "jpg", "size": 229156, "width": "360", "height": "540", "date": "2019-08-14 15:26:59", "date_gmt": "2019-08-14 09:56:59", "title": "4", "description": null, "nsfw": "0", "storage_mode": "datefolder", "md5": "3faa07e151badd17a6c05d1b6379795d", "source_md5": null, "original_filename": "4.jpg", "original_exifdata": "{\"FileName\":\"4.jpg\",\"FileDateTime\":\"1565776619\",\"FileSize\":\"229156\",\"FileType\":\"2\",\"MimeType\":\"image\\/jpeg\",\"SectionsFound\":\"ANY_TAG, IFD0, THUMBNAIL, EXIF\",\"COMPUTED\":{\"html\":\"width=\\\"360\\\" height=\\\"540\\\"\",\"Height\":\"540\",\"Width\":\"360\",\"IsColor\":\"1\",\"ByteOrderMotorola\":\"1\",\"Thumbnail.FileType\":\"2\",\"Thumbnail.MimeType\":\"image\\/jpeg\"},\"ImageWidth\":\"360\",\"ImageLength\":\"540\",\"BitsPerSample\":[\"8\",\"8\",\"8\"],\"PhotometricInterpretation\":\"2\",\"Orientation\":\"1\",\"SamplesPerPixel\":\"3\",\"XResolution\":\"720000\\/10000\",\"YResolution\":\"720000\\/10000\",\"ResolutionUnit\":\"2\",\"Software\":\"Adobe Photoshop CC 2017 (Windows)\",\"DateTime\":\"2019:07:10 15:43:07\",\"Exif_IFD_Pointer\":\"236\",\"THUMBNAIL\":{\"Compression\":\"6\",\"XResolution\":\"72\\/1\",\"YResolution\":\"72\\/1\",\"ResolutionUnit\":\"2\",\"JPEGInterchangeFormat\":\"386\",\"JPEGInterchangeFormatLength\":\"8982\"},\"ExifVersion\":\"0221\",\"ColorSpace\":\"65535\",\"ExifImageWidth\":\"360\",\"ExifImageLength\":\"540\"}", "views": "0", "category_id": null, "chain": "5", "thumb_size": "20818", "medium_size": "0", "expiration_date_gmt": null, "likes": "0", "is_animated": "0", "file": { "resource": { "type": "url", "chain": { "image": "https://c.imge.to/2019/08/14/8qJfG.jpg", "thumb": "https://c.imge.to/2019/08/14/8qJfG.th.jpg" } } }, "id_encoded": "8qJfG", "filename": "8qJfG.jpg", "mime": "image/jpeg", "url": "https://c.imge.to/2019/08/14/8qJfG.jpg", "ratio": 0.6666666666666666, "size_formatted": "229.2 KB", "url_viewer": "https://imge.to/i/8qJfG", "url_short": "https://imge.to/i/8qJfG", "image": { "filename": "8qJfG.jpg", "name": "8qJfG", "mime": "image/jpeg", "extension": "jpg", "url": "https://c.imge.to/2019/08/14/8qJfG.jpg", "size": 229156 }, "thumb": { "filename": "8qJfG.th.jpg", "name": "8qJfG.th", "mime": "image/jpeg", "extension": "jpg", "url": "https://c.imge.to/2019/08/14/8qJfG.th.jpg", "size": "20818" }, "display_url": "https://c.imge.to/2019/08/14/8qJfG.th.jpg", "display_width": "160", "display_height": "160", "views_label": "views", "likes_label": "likes", "how_long_ago": "1 second ago", "date_fixed_peer": "2019-08-14 09:56:59", "title_truncated": "4", "title_truncated_html": "4", "is_use_loader": false }, "request": { "type": "file", "action": "upload", "timestamp": "1565776618402", "auth_token": "5d17605c8ba97c5ed63bac93533f56c514ab8c51" }, "status_txt": "OK" } }, "dropbox": { "direct_url": "https://www.dropbox.com/s/77oykfl87tzyoqw/4.jpg?dl=0", "image_url": "https://www.dropbox.com/s/77oykfl87tzyoqw/4.jpg?dl=0", "urls": [ "https://www.dropbox.com/s/77oykfl87tzyoqw/4.jpg?dl=0" ], "response": { ".tag": "file", "url": "https://www.dropbox.com/s/77oykfl87tzyoqw/4.jpg?dl=0", "id": "id:Zj_ASS7ZjvAAAAAAAAAAig", "name": "4.jpg", "path_lower": "/4.jpg", "link_permissions": { "resolved_visibility": { ".tag": "public" }, "requested_visibility": { ".tag": "public" }, "can_revoke": true, "visibility_policies": [ { "policy": { ".tag": "public" }, "resolved_policy": { ".tag": "public" }, "allowed": true }, { "policy": { ".tag": "team_only" }, "resolved_policy": { ".tag": "team_only" }, "allowed": false, "disallowed_reason": { ".tag": "user_not_on_team" } }, { "policy": { ".tag": "password" }, "resolved_policy": { ".tag": "password" }, "allowed": false, "disallowed_reason": { ".tag": "user_account_type" } } ], "can_set_expiry": false, "can_remove_expiry": true, "allow_download": true, "can_allow_download": true, "can_disallow_download": false, "allow_comments": true, "team_restricts_comments": false }, "preview_type": "photo", "client_modified": "2019-08-14T09:56:58Z", "server_modified": "2019-08-14T09:56:58Z", "rev": "59010c72a27a050c49840", "size": 229156 } }, "drive": { "direct_url": "https://drive.google.com/file/d/1M89xC1MHmn0ixeQgcbKaStrt24CrxRKA/view?usp=sharing", "image_url": "https://drive.google.com/file/d/1M89xC1MHmn0ixeQgcbKaStrt24CrxRKA/view?usp=sharing", "urls": [ "https://drive.google.com/file/d/1M89xC1MHmn0ixeQgcbKaStrt24CrxRKA/view?usp=sharing" ], "response": { "id": "anyoneWithLink", "urlShare": "https://drive.google.com/file/d/1M89xC1MHmn0ixeQgcbKaStrt24CrxRKA/view?usp=sharing", "img_urls": [ "https://lh5.googleusercontent.com/vEMUH-BklYLX7gSda_pvpZL1qZdwcXTXdOKoavIjuMVcHKw2JaZ0Vw7LbHYHff0RiKgHLU0_5rO3ZQ=s2560" ] } }, "postimg": { "direct_url": "https://i.postimg.cc/15q9Cz95/4.jpg", "image_url": "https://postimg.cc/qzpVg4h9", "urls": [ "https://i.postimg.cc/15q9Cz95/4.jpg", "https://postimg.cc/qzpVg4h9", "https://postimg.cc/qzpVg4h9/e3d29dd5", "https://i.postimg.cc/qzpVg4h9/4.jpg" ], "response": { "status": "OK", "url": "https://postimg.cc/qzpVg4h9/e3d29dd5", "link": "https://postimg.cc/qzpVg4h9", "linkDirect": "https://i.postimg.cc/15q9Cz95/4.jpg", "linkRemove": "https://postimg.cc/delete/7cgxp2N3/e3d29dd5", "linkThumbnail": "https://i.postimg.cc/qzpVg4h9/4.jpg" } }, "imageshack": { "direct_url": "http://imagizer.imageshack.com/img921/9771/a7G9jY.jpg", "image_url": "http://imageshack.com/i/pla7G9jYj", "urls": [ "http://imagizer.imageshack.com/img921/9771/a7G9jY.jpg", "http://imageshack.com/i/pla7G9jYj", "http://imagizer.imageshack.com/img921/9771/a7G9jY.th.jpg" ], "response": { "status": "1", "version": 9, "timestamp": 1565776621, "base_url": "http://imagizer.imageshack.com/", "lp_hash": "pla7G9jYj", "id": "1336123181", "rating": { "ratings": 0, "avg": 0 }, "files": { "server": 921, "bucket": 9771, "image": { "size": 224, "content-type": "image/jpeg", "filename": "a7G9jY.jpg", "original_filename": "4.jpg" }, "thumb": { "size": 0, "content": "image/jpeg", "filename": "a7G9jY.th.jpg" } }, "resolution": { "width": 360, "height": 540 }, "exif-info": { "exiforientation": "Horizontal (normal)", "exifcompression": "JPEG (old-style)", "exifimagesnum": "1", "exifwidth": "360", "exifheight": "540", "exiffilesize": "224" }, "class": "r", "visibility": "yes", "uploader": { "ip": "210.245.122.197", "cookie": "f0beea20000a5e827e5376ba49ba8ca0" }, "links": { "image_link": "http://imagizer.imageshack.com/img921/9771/a7G9jY.jpg", "image_html": "<a href='http://imageshack.com/i/pla7G9jYj' target='_blank'><img src='http://imagizer.imageshack.com/img921/9771/a7G9jY.jpg' alt='Free Image Hosting at www.imageshack.com' border='0'/></a>'", "image_bb": "[URL=http://imageshack.com/i/pla7G9jYj][IMG]http://imagizer.imageshack.com/img921/9771/a7G9jY.jpg[/IMG][/URL]", "image_bb2": "[url=http://imageshack.com/i/pla7G9jYj][img=http://imagizer.imageshack.com/img921/9771/a7G9jY.jpg][/url]", "thumb_link": "http://imagizer.imageshack.com/img921/9771/a7G9jY.th.jpg", "thumb_html": "<a href='http://imageshack.com/i/pla7G9jYj' target='_blank'><img src='http://imagizer.imageshack.com/img921/9771/a7G9jY.th.jpg' alt='Free Image Hosting at www.imageshack.com' border='0'/></a>'", "thumb_bb": "[URL=http://imageshack.com/i/pla7G9jYj][IMG]http://imagizer.imageshack.com/img921/9771/a7G9jY.th.jpg[/IMG][/URL]", "thumb_bb2": "[url=http://imageshack.com/i/pla7G9jYj][img=http://imagizer.imageshack.com/img921/9771/a7G9jY.th.jpg][/url]", "is_link": "http://imageshack.com/i/pla7G9jYj", "done": "http://imageshack.com/i/pla7G9jYj" } } }, "imgbb": { "direct_url": "https://i.ibb.co/Sm2hdGg/4.jpg", "image_url": "https://ibb.co/M1bYGQH", "urls": [ "https://i.ibb.co/Sm2hdGg/4.jpg", "https://ibb.co/M1bYGQH", "https://i.ibb.co/M1bYGQH/4.jpg" ], "response": { "status_code": 200, "success": { "message": "image uploaded", "code": 200 }, "image": { "title": "4", "name": "4", "extension": "jpg", "original_filename": "4.jpg", "width": "360", "height": "540", "size": 229156, "thumb_size": "19242", "medium_size": "0", "time": "1565776622", "expiration": "0", "description": null, "likes": "0", "is_animated": 0, "nsfw": 0, "id_encoded": "M1bYGQH", "filename": "4.jpg", "mime": "image/jpeg", "url": "https://i.ibb.co/Sm2hdGg/4.jpg", "size_formatted": "229.2 KB", "url_viewer": "https://ibb.co/M1bYGQH", "url_viewer_preview": "https://imgbb.com/", "url_viewer_thumb": "https://ibb.co/M1bYGQH", "image": { "filename": "4.jpg", "name": "4", "mime": "image/jpeg", "extension": "jpg", "url": "https://i.ibb.co/Sm2hdGg/4.jpg", "size": 229156 }, "thumb": { "filename": "4.jpg", "name": "4", "mime": "image/jpeg", "extension": "jpg", "url": "https://i.ibb.co/M1bYGQH/4.jpg", "size": "19242" }, "display_url": "https://i.ibb.co/Sm2hdGg/4.jpg", "display_width": "360", "display_height": "540", "delete_url": "https://ibb.co/M1bYGQH/83cb691bdbe90d529d30525679accfca", "views_label": "views", "likes_label": "likes", "how_long_ago": "moments ago", "date_fixed_peer": "2019-08-14 09:57:02", "title_truncated": "4", "title_truncated_html": "4", "is_use_loader": false }, "request": { "type": "file", "action": "upload", "timestamp": "1565776617202" }, "status_txt": "OK" } }, "imgbox": { "direct_url": "https://images2.imgbox.com/15/de/TJxLy0a1_o.jpg", "image_url": "https://images2.imgbox.com/15/de/TJxLy0a1_o.jpg", "urls": [ "https://images2.imgbox.com/15/de/TJxLy0a1_o.jpg", "https://thumbs2.imgbox.com/15/de/TJxLy0a1_t.jpg", "https://thumbs2.imgbox.com/15/de/TJxLy0a1_b.jpg" ], "response": { "files": [ { "id": "TJxLy0a1", "slug": "TJxLy0a1", "name": "4.jpg", "name_html_escaped": "4.jpg", "created_at": "2019-08-14T09:57:03.000Z", "created_at_human": "August 14, 2019 09:57", "updated_at": "2019-08-14T09:57:04.198Z", "gallery_id": "RzA6h5GxOK", "url": "http://imgbox.com/TJxLy0a1", "original_url": "https://images2.imgbox.com/15/de/TJxLy0a1_o.jpg", "thumbnail_url": "https://thumbs2.imgbox.com/15/de/TJxLy0a1_t.jpg", "square_url": "https://thumbs2.imgbox.com/15/de/TJxLy0a1_b.jpg", "selected": false, "comments_enabled": 0, "comments_count": 0 } ] } }, "filestack": { "direct_url": "https://cdn.filestackcontent.com/6oGt5ArsQ4qsjSKNmEm7", "image_url": "https://cdn.filestackcontent.com/6oGt5ArsQ4qsjSKNmEm7", "urls": [ "https://cdn.filestackcontent.com/6oGt5ArsQ4qsjSKNmEm7" ], "response": { "name": "4.jpg", "status": "Stored", "type": "image/jpeg", "size": 229156, "url": "https://cdn.filestackcontent.com/6oGt5ArsQ4qsjSKNmEm7", "handle": "6oGt5ArsQ4qsjSKNmEm7" } } } ``` License ---- ISC ##### - Chickyky -