Wifiv2 / src /utils /portalPaymentStorage.js
Mbonea's picture
Serve portal payment assets under portal route
742fd5b
Raw
History Blame Contribute Delete
1.13 kB
const fs = require('fs');
const path = require('path');
const PAYMENT_STORAGE_ROOT = process.env.PORTAL_PAYMENT_STORAGE_ROOT || '/data/wifiplatform/storage/payment';
const PUBLIC_PAYMENT_ASSET_PREFIX = '/portal/assets/payment';
function ensureDir(dirPath) {
fs.mkdirSync(dirPath, { recursive: true });
return dirPath;
}
function ensurePaymentStorageRoot() {
return ensureDir(PAYMENT_STORAGE_ROOT);
}
function getDevicePaymentDir(deviceId) {
return path.join(PAYMENT_STORAGE_ROOT, String(deviceId));
}
function ensureDevicePaymentDir(deviceId) {
return ensureDir(getDevicePaymentDir(deviceId));
}
function makePaymentAssetUrl(deviceId, fileName) {
return `${PUBLIC_PAYMENT_ASSET_PREFIX}/${encodeURIComponent(String(deviceId))}/${encodeURIComponent(fileName)}`;
}
function resolvePaymentAssetPath(deviceId, fileName) {
const safeName = path.basename(String(fileName || ''));
return path.join(getDevicePaymentDir(deviceId), safeName);
}
module.exports = {
PAYMENT_STORAGE_ROOT,
ensurePaymentStorageRoot,
ensureDevicePaymentDir,
getDevicePaymentDir,
makePaymentAssetUrl,
resolvePaymentAssetPath,
};