130 lines
6.0 KiB
JavaScript
130 lines
6.0 KiB
JavaScript
|
"use strict";
|
||
|
exports.id = 5910;
|
||
|
exports.ids = [5910];
|
||
|
exports.modules = {
|
||
|
|
||
|
/***/ 5910:
|
||
|
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
|
||
|
|
||
|
// @ts-check
|
||
|
|
||
|
const sharp = __webpack_require__(7441);
|
||
|
const serverError = __webpack_require__(2163);
|
||
|
const grabPaths = __webpack_require__(6715);
|
||
|
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
|
||
|
*
|
||
|
* @param {object} params
|
||
|
* @param {string} params.imageSourceBase64
|
||
|
* @param {string} params.imageName
|
||
|
* @param {any} params.user
|
||
|
* @param {string} [params.mimeType]
|
||
|
* @param {number} [params.thumbnailSize]
|
||
|
* @param {string} [params.folder]
|
||
|
* @param {boolean} [params.isPrivate]
|
||
|
* @returns {Promise<{ urlPath: string, urlThumbnailPath: string, urlRelativePath: string, urlThumbnailRelativePath: string } | undefined | null>}
|
||
|
*/ module.exports = async function fsWriteImageToDiskFromBase64({ imageSourceBase64 , imageName , user , mimeType , thumbnailSize , folder , isPrivate , }) {
|
||
|
try {
|
||
|
const buffer = Buffer.from(imageSourceBase64, "base64");
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
const MAX_SIZE = 1800;
|
||
|
const MAX_SIZE_THUMBNAIL = thumbnailSize ? parseInt(thumbnailSize.toString()) : 400;
|
||
|
// const sharpImage = sharp(imagePath);
|
||
|
const sharpImageRaw = sharp(buffer);
|
||
|
const sharpImageThumbnailRaw = sharp(buffer);
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
/**
|
||
|
* Construct root paths
|
||
|
*/ const grabedPaths = grabPaths({
|
||
|
isPrivate: isPrivate,
|
||
|
user: user,
|
||
|
folder: folder
|
||
|
});
|
||
|
if (!grabedPaths) {
|
||
|
return null;
|
||
|
}
|
||
|
const { fileRootPath , urlRootPath , relativePath } = grabedPaths;
|
||
|
const imageRootPath = fileRootPath;
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
/**
|
||
|
* Main Image
|
||
|
*
|
||
|
* @description Main Image
|
||
|
*/ let imageMetadataRaw = await sharpImageRaw.metadata();
|
||
|
let { width , height , format } = imageMetadataRaw;
|
||
|
/** @type {keyof import("sharp").FormatEnum} */ // @ts-ignore
|
||
|
const finalFormat = mimeType ? mimeType : format;
|
||
|
if (width && height && width > MAX_SIZE) {
|
||
|
let resizeRatio = MAX_SIZE / width;
|
||
|
sharpImageRaw.resize(MAX_SIZE, Math.round(height * resizeRatio), {
|
||
|
fit: "cover"
|
||
|
});
|
||
|
}
|
||
|
sharpImageRaw.toFormat(finalFormat, {
|
||
|
quality: 80
|
||
|
});
|
||
|
////////////////////////////////////////
|
||
|
let newImageMetadataRaw = await sharpImageRaw.metadata();
|
||
|
////////////////////////////////////////
|
||
|
let imageFullName = `${imageName}.${finalFormat}`;
|
||
|
let imagePath = imageRootPath + imageFullName;
|
||
|
const urlPath = urlRootPath + imageFullName;
|
||
|
const urlRelativePath = relativePath + imageFullName;
|
||
|
await sharpImageRaw.toFile(imagePath);
|
||
|
/**
|
||
|
* Thumbnail
|
||
|
*
|
||
|
* @description Thumbnail
|
||
|
*/ if (width && height && width > MAX_SIZE_THUMBNAIL) {
|
||
|
let resizeRatio1 = MAX_SIZE_THUMBNAIL / width;
|
||
|
sharpImageThumbnailRaw.resize(MAX_SIZE_THUMBNAIL, Math.round(height * resizeRatio1), {
|
||
|
fit: "cover"
|
||
|
});
|
||
|
} else if (width && height) {
|
||
|
const LOWER_THUMBNAIL_SIZE = 150;
|
||
|
let resizeRatio2 = LOWER_THUMBNAIL_SIZE / width;
|
||
|
sharpImageThumbnailRaw.resize(LOWER_THUMBNAIL_SIZE, Math.round(height * resizeRatio2), {
|
||
|
fit: "cover"
|
||
|
});
|
||
|
}
|
||
|
sharpImageThumbnailRaw.toFormat(finalFormat, {
|
||
|
quality: 80
|
||
|
});
|
||
|
////////////////////////////////////////
|
||
|
let imageThumbnailFullName = `${imageName}_thumbnail.${finalFormat}`;
|
||
|
let imageThumbnailPath = imageRootPath + imageThumbnailFullName;
|
||
|
const urlThumbnailPath = urlRootPath + imageThumbnailFullName;
|
||
|
const urlThumbnailRelativePath = relativePath + imageThumbnailFullName;
|
||
|
await sharpImageThumbnailRaw.toFile(imageThumbnailPath);
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
return {
|
||
|
urlPath,
|
||
|
urlThumbnailPath,
|
||
|
urlRelativePath,
|
||
|
urlThumbnailRelativePath
|
||
|
};
|
||
|
// console.log("====================================");
|
||
|
// console.log("Complete!!!");
|
||
|
// console.log("====================================");
|
||
|
} catch (/** @type {any} */ error) {
|
||
|
console.log("Write Image to Disk error =>", error.message);
|
||
|
serverError({
|
||
|
component: "functions/backend/fsWriteImageToDiskFromBase64",
|
||
|
message: error.message
|
||
|
});
|
||
|
return null;
|
||
|
}
|
||
|
}; /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */
|
||
|
|
||
|
|
||
|
/***/ })
|
||
|
|
||
|
};
|
||
|
;
|