84 lines
3.3 KiB
JavaScript
84 lines
3.3 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Dedicated on-disk storage for Android APK releases (staging vs published).
|
|
* Files are never executed — only stored and streamed for download.
|
|
*/
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getApkReleasesRoot = getApkReleasesRoot;
|
|
exports.ensureApkStagingDir = ensureApkStagingDir;
|
|
exports.ensureApkPublishedDir = ensureApkPublishedDir;
|
|
exports.resolveApkStoragePath = resolveApkStoragePath;
|
|
exports.sha256File = sha256File;
|
|
exports.moveApkToPublished = moveApkToPublished;
|
|
exports.removeApkIfExists = removeApkIfExists;
|
|
exports.getApkMaxUploadBytes = getApkMaxUploadBytes;
|
|
const fs_1 = __importDefault(require("fs"));
|
|
const path_1 = __importDefault(require("path"));
|
|
const crypto_1 = __importDefault(require("crypto"));
|
|
const config_1 = require("../config");
|
|
const ROOT_REL = path_1.default.join('data', 'apk-releases');
|
|
function getApkReleasesRoot() {
|
|
const root = path_1.default.resolve(process.cwd(), ROOT_REL);
|
|
fs_1.default.mkdirSync(path_1.default.join(root, 'staging'), { recursive: true });
|
|
fs_1.default.mkdirSync(path_1.default.join(root, 'published'), { recursive: true });
|
|
return root;
|
|
}
|
|
function ensureApkStagingDir() {
|
|
const dir = path_1.default.join(getApkReleasesRoot(), 'staging');
|
|
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
return dir;
|
|
}
|
|
function ensureApkPublishedDir() {
|
|
const dir = path_1.default.join(getApkReleasesRoot(), 'published');
|
|
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
return dir;
|
|
}
|
|
/** Absolute path for a relative storage key like `staging/uuid.apk` or `published/uuid.apk`. */
|
|
function resolveApkStoragePath(storageKey) {
|
|
const root = getApkReleasesRoot();
|
|
const normalized = storageKey.replace(/\\/g, '/').replace(/^\/+/, '');
|
|
if (normalized.includes('..') || (!normalized.startsWith('staging/') && !normalized.startsWith('published/'))) {
|
|
throw new Error('Invalid APK storage key');
|
|
}
|
|
const absolute = path_1.default.resolve(root, normalized);
|
|
if (!absolute.startsWith(root)) {
|
|
throw new Error('Invalid APK storage key');
|
|
}
|
|
return absolute;
|
|
}
|
|
function sha256File(filePath) {
|
|
return new Promise((resolve, reject) => {
|
|
const hash = crypto_1.default.createHash('sha256');
|
|
const stream = fs_1.default.createReadStream(filePath);
|
|
stream.on('error', reject);
|
|
stream.on('data', (chunk) => hash.update(chunk));
|
|
stream.on('end', () => resolve(hash.digest('hex')));
|
|
});
|
|
}
|
|
function moveApkToPublished(stagingKey, releaseId) {
|
|
const from = resolveApkStoragePath(stagingKey);
|
|
const publishedKey = `published/${releaseId}.apk`;
|
|
const to = resolveApkStoragePath(publishedKey);
|
|
fs_1.default.mkdirSync(path_1.default.dirname(to), { recursive: true });
|
|
fs_1.default.renameSync(from, to);
|
|
return publishedKey;
|
|
}
|
|
function removeApkIfExists(storageKey) {
|
|
if (!storageKey)
|
|
return;
|
|
try {
|
|
const p = resolveApkStoragePath(storageKey);
|
|
if (fs_1.default.existsSync(p))
|
|
fs_1.default.unlinkSync(p);
|
|
}
|
|
catch {
|
|
// ignore
|
|
}
|
|
}
|
|
function getApkMaxUploadBytes() {
|
|
return config_1.config.mobileReleases.maxUploadBytes;
|
|
}
|
|
//# sourceMappingURL=apkStorageService.js.map
|