adonisjs-cloudinary
Version:
[![npm-image]][npm-url] [![license-image]][license-url] [![typescript-image]][typescript-url]
85 lines (84 loc) • 2.75 kB
JavaScript
;
/*
* adonisjs-cloudinary
*
* (c) Liam Edwards <me@liamedwards.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
class Cloudinary {
constructor(config, cloudinary) {
/* eslint-disable camelcase */
cloudinary.config({
cloud_name: config.cloudName,
api_key: config.apiKey,
api_secret: config.apiSecret,
secure: config.secure,
});
/* eslint-enable camelcase */
this.config = config;
this.cloudinary = cloudinary;
}
getCloudinary() {
return this.cloudinary;
}
static getPathFromFile(file) {
const path = file.tmpPath ?? file.filePath;
if (!path) {
throw new Error('File\'s tmpPath or filePath must exist');
}
return path;
}
async upload(file, publicId = null, uploadOptions = {}, callback) {
let filePath;
if (typeof file === 'string') {
filePath = file;
}
else {
filePath = Cloudinary.getPathFromFile(file);
}
return this.uploadResponse = await this.cloudinary.uploader.upload(filePath, {
/* eslint-disable camelcase */
public_id: publicId,
/* eslint-enable camelcase */
...uploadOptions,
}, callback);
}
async unsignedUpload(file, uploadPreset, publicId = null, uploadOptions = {}, callback) {
let filePath;
if (typeof file === 'string') {
filePath = file;
}
else {
filePath = Cloudinary.getPathFromFile(file);
}
return this.uploadResponse = await this.cloudinary.uploader.unsigned_upload(filePath, uploadPreset, {
/* eslint-disable camelcase */
public_id: publicId,
/* eslint-enable camelcase */
...uploadOptions,
}, callback);
}
getResult() {
return this.uploadResponse;
}
getPublicId() {
return this.uploadResponse.public_id;
}
show(publicId, options = {}) {
const defaults = this.config.scaling;
// @ts-ignore
options = { ...defaults, ...options };
return this.cloudinary.url(publicId, options);
}
secureShow(publicId, options = {}) {
// @ts-ignore
return this.show(publicId, { ...options, secure: true });
}
async destroy(publicId, options) {
return await this.cloudinary.uploader.destroy(publicId, options);
}
}
exports.default = Cloudinary;