@xuda.io/account_module 1.2.273 → 1.2.275
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 +113 -44
- package/package.json +1 -1
- /package/{download_model.mjs → download_model.js} +0 -0
package/index.mjs
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import _ from 'lodash';
|
|
3
|
-
import { removeBackground } from '@imgly/background-removal-node';
|
|
3
|
+
// import { removeBackground } from '@imgly/background-removal-node';
|
|
4
4
|
import fs from 'node:fs';
|
|
5
5
|
|
|
6
|
+
import os from 'os';
|
|
7
|
+
import { exec } from 'child_process';
|
|
8
|
+
import util from 'util';
|
|
9
|
+
|
|
10
|
+
// Convert exec to a promise for cleaner async/await usage
|
|
11
|
+
const execAsync = util.promisify(exec);
|
|
12
|
+
|
|
6
13
|
global._conf = (
|
|
7
14
|
await import(path.join(process.env.XUDA_HOME, process.env.XUDA_CONFIG), {
|
|
8
15
|
with: { type: 'json' },
|
|
@@ -114,7 +121,7 @@ export const update_account_info = async function (req, job_id, headers) {
|
|
|
114
121
|
}
|
|
115
122
|
|
|
116
123
|
if (account_obj.account_info?.profile_picture) {
|
|
117
|
-
await removeBg({ url: account_obj.account_info?.profile_picture }, job_id, headers);
|
|
124
|
+
await removeBg({ url: account_obj.account_info?.profile_picture, uid }, job_id, headers);
|
|
118
125
|
} else {
|
|
119
126
|
const ai_module = await import(`${module_path}/ai_module/index.mjs`);
|
|
120
127
|
const profile_avatar_ret = ai_module.update_thumbnail('account', account_obj, account_obj.account_project_id, uid, job_id, headers, 'xuda_accounts');
|
|
@@ -1554,61 +1561,123 @@ export const get_user_name = async function (uid) {
|
|
|
1554
1561
|
}
|
|
1555
1562
|
};
|
|
1556
1563
|
|
|
1557
|
-
const removeBg = async function (req, job_id, headers) {
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1564
|
+
// const removeBg = async function (req, job_id, headers) {
|
|
1565
|
+
// // const ASSET_PATH = path.join(process.cwd(), 'assets/imgly-bg');
|
|
1566
|
+
// // const config = {
|
|
1567
|
+
// // publicPath: `file://${ASSET_PATH}/`, // Points to your local folder
|
|
1568
|
+
// // debug: true, // Optional: Logs fetch attempts for debugging
|
|
1569
|
+
// // };
|
|
1563
1570
|
|
|
1564
|
-
|
|
1571
|
+
// const ASSET_PATH = '/var/xuda/assets/imgly-bg'; // <-- your folder
|
|
1565
1572
|
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1573
|
+
// const config = {
|
|
1574
|
+
// publicPath: `file://${ASSET_PATH}/`, // ← this fixes everything
|
|
1575
|
+
// debug: false,
|
|
1576
|
+
// model: 'medium', // or "small" if you want faster/smaller
|
|
1577
|
+
// };
|
|
1578
|
+
// debugger;
|
|
1579
|
+
// const drive_module = await import(`${module_path}/drive_module/index.mjs`);
|
|
1580
|
+
// const { url } = req;
|
|
1581
|
+
// try {
|
|
1582
|
+
// let imageBuffer;
|
|
1583
|
+
// // if (inputPath) {
|
|
1584
|
+
// // imageBuffer = fs.readFileSync(inputPath);
|
|
1585
|
+
// // }
|
|
1586
|
+
|
|
1587
|
+
// if (url) {
|
|
1588
|
+
// const res = await fetch(url);
|
|
1589
|
+
// if (!res.ok) {
|
|
1590
|
+
// throw new Error(`Failed to fetch image URL: ${res.status} ${res.statusText}`);
|
|
1591
|
+
// }
|
|
1592
|
+
// const arrayBuffer = await res.arrayBuffer();
|
|
1593
|
+
// imageBuffer = Buffer.from(arrayBuffer);
|
|
1594
|
+
// }
|
|
1595
|
+
// const blob = await removeBackground(imageBuffer, config);
|
|
1596
|
+
|
|
1597
|
+
// // Convert to buffer and save
|
|
1598
|
+
// const outputBuffer = Buffer.from(await blob.arrayBuffer());
|
|
1599
|
+
|
|
1600
|
+
// const originalname = 'avatar_' + path.basename(url);
|
|
1601
|
+
// const tempPath = path.join(os.tmpdir(), originalname + Date.now());
|
|
1602
|
+
// fs.writeFileSync(tempPath, outputBuffer);
|
|
1603
|
+
|
|
1604
|
+
// const ext = drive_module.getExtensionFromUrl(url);
|
|
1605
|
+
|
|
1606
|
+
// const file_obj = {
|
|
1607
|
+
// originalname,
|
|
1608
|
+
// path: tempPath,
|
|
1609
|
+
// mimetype: `image/${ext}`,
|
|
1610
|
+
// };
|
|
1611
|
+
|
|
1612
|
+
// const drive_ret = await drive_module.upload_drive_file_user({ uid, path: '/Profile Images' }, job_id, headers, file_obj);
|
|
1613
|
+
// console.log(`Background removed! `, drive_ret);
|
|
1614
|
+
// } catch (error) {
|
|
1615
|
+
// console.error('Error:', error);
|
|
1616
|
+
// }
|
|
1617
|
+
// };
|
|
1618
|
+
|
|
1619
|
+
const removeBg = async function (req, job_id, headers) {
|
|
1620
|
+
// 1. Load your drive module
|
|
1572
1621
|
const drive_module = await import(`${module_path}/drive_module/index.mjs`);
|
|
1573
|
-
|
|
1622
|
+
|
|
1623
|
+
const { url, uid } = req;
|
|
1624
|
+
|
|
1574
1625
|
try {
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
throw new Error(`Failed to fetch image URL: ${res.status} ${res.statusText}`);
|
|
1584
|
-
}
|
|
1585
|
-
const arrayBuffer = await res.arrayBuffer();
|
|
1586
|
-
imageBuffer = Buffer.from(arrayBuffer);
|
|
1626
|
+
if (!url) {
|
|
1627
|
+
throw new Error('No URL provided in request');
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
// 3. Download the image to a Buffer
|
|
1631
|
+
const res = await fetch(url);
|
|
1632
|
+
if (!res.ok) {
|
|
1633
|
+
throw new Error(`Failed to fetch image URL: ${res.status} ${res.statusText}`);
|
|
1587
1634
|
}
|
|
1588
|
-
const
|
|
1635
|
+
const arrayBuffer = await res.arrayBuffer();
|
|
1636
|
+
const imageBuffer = Buffer.from(arrayBuffer);
|
|
1637
|
+
|
|
1638
|
+
// 4. Create Temporary File Paths
|
|
1639
|
+
// rembg works with files, so we must save the buffer to disk first.
|
|
1640
|
+
const tempDir = os.tmpdir();
|
|
1641
|
+
const uniqueId = Date.now() + '_' + Math.random().toString(36).substring(7);
|
|
1589
1642
|
|
|
1590
|
-
|
|
1591
|
-
const
|
|
1643
|
+
const inputExt = path.extname(url) || '.jpg';
|
|
1644
|
+
const tempInputPath = path.join(tempDir, `input_${uniqueId}${inputExt}`);
|
|
1645
|
+
const tempOutputPath = path.join(tempDir, `output_${uniqueId}.png`); // Output is always PNG (transparent)
|
|
1592
1646
|
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
fs.writeFileSync(tempPath, outputBuffer);
|
|
1647
|
+
// 5. Write the downloaded image to disk
|
|
1648
|
+
fs.writeFileSync(tempInputPath, imageBuffer);
|
|
1596
1649
|
|
|
1597
|
-
|
|
1650
|
+
// 6. EXECUTE REMBG via CLI
|
|
1651
|
+
// "rembg i [input] [output]"
|
|
1652
|
+
console.log(`Processing with rembg: ${tempInputPath} -> ${tempOutputPath}`);
|
|
1653
|
+
await execAsync(`rembg i "${tempInputPath}" "${tempOutputPath}"`);
|
|
1654
|
+
|
|
1655
|
+
// 7. Prepare File Object for Drive Upload
|
|
1656
|
+
// Note: The output file now exists at tempOutputPath
|
|
1657
|
+
const originalname = 'avatar_' + path.basename(url, inputExt) + '.png';
|
|
1598
1658
|
|
|
1599
1659
|
const file_obj = {
|
|
1600
|
-
originalname,
|
|
1601
|
-
path:
|
|
1602
|
-
mimetype:
|
|
1660
|
+
originalname: originalname,
|
|
1661
|
+
path: tempOutputPath,
|
|
1662
|
+
mimetype: 'image/png', // rembg output is always PNG
|
|
1603
1663
|
};
|
|
1604
1664
|
|
|
1665
|
+
// 8. Upload to Drive
|
|
1605
1666
|
const drive_ret = await drive_module.upload_drive_file_user({ uid, path: '/Profile Images' }, job_id, headers, file_obj);
|
|
1606
|
-
|
|
1667
|
+
|
|
1668
|
+
console.log(`Background removed!`, drive_ret);
|
|
1669
|
+
|
|
1670
|
+
// 9. Clean up Input File (Output file might be needed by upload, so clean input only)
|
|
1671
|
+
try {
|
|
1672
|
+
fs.unlinkSync(tempInputPath);
|
|
1673
|
+
// Optional: fs.unlinkSync(tempOutputPath); // Only if upload_drive_file_user handles the file reading fully
|
|
1674
|
+
} catch (cleanupErr) {
|
|
1675
|
+
console.warn('Warning: Failed to cleanup temp input file', cleanupErr);
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
return drive_ret;
|
|
1607
1679
|
} catch (error) {
|
|
1608
|
-
console.error('Error:', error);
|
|
1680
|
+
console.error('Error processing background removal:', error);
|
|
1681
|
+
throw error;
|
|
1609
1682
|
}
|
|
1610
1683
|
};
|
|
1611
|
-
|
|
1612
|
-
// Step 1: Create a local assets folder (e.g., in your project root)
|
|
1613
|
-
const ASSET_PATH = path.join(process.cwd(), 'assets/imgly-bg'); // e.g., ./assets/imgly-bg
|
|
1614
|
-
fs.mkdirSync(ASSET_PATH, { recursive: true });
|
package/package.json
CHANGED
|
File without changes
|