@xuda.io/ai_module 1.1.4528 → 1.1.4530
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +7 -69
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -7000,55 +7000,6 @@ const get_chat_picture_cache = async function (uid, docType, name) {
|
|
|
7000
7000
|
};
|
|
7001
7001
|
|
|
7002
7002
|
const save_drive_file_to_disk = async function (drive_file) {
|
|
7003
|
-
async function hasTransparentBackground(filePath) {
|
|
7004
|
-
const image = sharp(filePath);
|
|
7005
|
-
const metadata = await image.metadata();
|
|
7006
|
-
|
|
7007
|
-
// If the image doesn't have an alpha channel at all → no transparency possible
|
|
7008
|
-
if (!metadata.hasAlpha) {
|
|
7009
|
-
return false;
|
|
7010
|
-
}
|
|
7011
|
-
|
|
7012
|
-
// For images with alpha channel, check if any pixel actually has alpha < 255
|
|
7013
|
-
const { data, info } = await image.raw().toBuffer({ resolveWithObject: true });
|
|
7014
|
-
|
|
7015
|
-
// Determine position of alpha channel based on number of channels
|
|
7016
|
-
const channels = info.channels; // 2 (grayscale+alpha), 4 (RGBA), etc.
|
|
7017
|
-
const alphaOffset = channels === 2 ? 1 : channels === 4 ? 3 : -1;
|
|
7018
|
-
|
|
7019
|
-
if (alphaOffset === -1) {
|
|
7020
|
-
// Unusual channel count – be conservative and assume it might have transparency
|
|
7021
|
-
return true;
|
|
7022
|
-
}
|
|
7023
|
-
|
|
7024
|
-
for (let i = alphaOffset; i < data.length; i += channels) {
|
|
7025
|
-
if (data[i] < 255) {
|
|
7026
|
-
return true; // Found at least one transparent/semi-transparent pixel
|
|
7027
|
-
}
|
|
7028
|
-
}
|
|
7029
|
-
|
|
7030
|
-
return false; // Has alpha channel but all pixels are fully opaque
|
|
7031
|
-
}
|
|
7032
|
-
|
|
7033
|
-
async function imageToPngBlob(filePath) {
|
|
7034
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
7035
|
-
|
|
7036
|
-
let buffer;
|
|
7037
|
-
|
|
7038
|
-
let is_transparent = false;
|
|
7039
|
-
if (ext === '.png') {
|
|
7040
|
-
buffer = await fs.promises.readFile(filePath);
|
|
7041
|
-
is_transparent = await hasTransparentBackground(filePath);
|
|
7042
|
-
} else {
|
|
7043
|
-
console.log(`Converting ${path.basename(filePath)} to PNG...`);
|
|
7044
|
-
buffer = await sharp(filePath).png({ quality: 100, compressionLevel: 9, force: true }).toBuffer();
|
|
7045
|
-
}
|
|
7046
|
-
|
|
7047
|
-
// Create a Blob with correct MIME type
|
|
7048
|
-
|
|
7049
|
-
return { is_transparent, image_blob: new Blob([buffer], { type: 'image/png' }) };
|
|
7050
|
-
}
|
|
7051
|
-
// try {
|
|
7052
7003
|
if (!drive_file.file_url) {
|
|
7053
7004
|
throw new Error('No URL provided in request');
|
|
7054
7005
|
}
|
|
@@ -7064,28 +7015,15 @@ const save_drive_file_to_disk = async function (drive_file) {
|
|
|
7064
7015
|
|
|
7065
7016
|
const tempDir = os.tmpdir();
|
|
7066
7017
|
|
|
7067
|
-
const
|
|
7018
|
+
const tempOutputPath = path.join(tempDir, drive_file.filename + '_' + Date.now());
|
|
7068
7019
|
|
|
7069
7020
|
// 5. Write the downloaded image to disk
|
|
7070
|
-
fs.writeFileSync(
|
|
7021
|
+
fs.writeFileSync(tempOutputPath, imageBuffer);
|
|
7071
7022
|
/////////////////////////////////
|
|
7072
7023
|
|
|
7073
|
-
|
|
7074
|
-
|
|
7075
|
-
|
|
7076
|
-
|
|
7077
|
-
|
|
7078
|
-
const blob = img_ret.image_blob;
|
|
7079
|
-
|
|
7080
|
-
// OpenAI expects a filename for logging/debugging
|
|
7081
|
-
const filename = path.basename(filePath, path.extname(filePath)) + '.png';
|
|
7082
|
-
blob.name = filename; // Optional but helpful
|
|
7083
|
-
// @ts-ignore – name is allowed in Node 20+
|
|
7084
|
-
blob.lastModified = Date.now();
|
|
7085
|
-
|
|
7086
|
-
return { is_transparent: img_ret.is_transparent, image_blob: blob };
|
|
7087
|
-
}),
|
|
7088
|
-
);
|
|
7089
|
-
|
|
7090
|
-
return images[0];
|
|
7024
|
+
return {
|
|
7025
|
+
filename: drive_file.filename,
|
|
7026
|
+
path: tempOutputPath,
|
|
7027
|
+
contentType: 'application/pdf',
|
|
7028
|
+
};
|
|
7091
7029
|
};
|