fix(mining-admin-service): 修复 APK 解析和添加 AppVersion migration

1. 修复 package-parser.service.ts:
   - ApkReader.open 需要文件路径,改为先写入临时文件再解析
   - 添加 fs/path/os 模块导入
   - 完成后自动清理临时文件

2. 添加 AppVersion 表 migration:
   - 创建 Platform 枚举类型 (ANDROID, IOS)
   - 创建 app_versions 表
   - 添加必要索引

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-28 22:24:45 -08:00
parent fd64903841
commit b4afc4615c
2 changed files with 51 additions and 1 deletions

View File

@ -0,0 +1,31 @@
-- CreateEnum
CREATE TYPE "Platform" AS ENUM ('ANDROID', 'IOS');
-- CreateTable
CREATE TABLE "app_versions" (
"id" TEXT NOT NULL,
"platform" "Platform" NOT NULL,
"version_code" INTEGER NOT NULL,
"version_name" TEXT NOT NULL,
"build_number" TEXT NOT NULL,
"download_url" TEXT NOT NULL,
"file_size" BIGINT NOT NULL,
"file_sha256" TEXT NOT NULL,
"min_os_version" TEXT,
"changelog" TEXT NOT NULL,
"is_force_update" BOOLEAN NOT NULL DEFAULT false,
"is_enabled" BOOLEAN NOT NULL DEFAULT true,
"release_date" TIMESTAMP(3),
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"created_by" TEXT NOT NULL,
"updated_by" TEXT,
CONSTRAINT "app_versions_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "app_versions_platform_is_enabled_idx" ON "app_versions"("platform", "is_enabled");
-- CreateIndex
CREATE INDEX "app_versions_platform_version_code_idx" ON "app_versions"("platform", "version_code");

View File

@ -1,5 +1,8 @@
import { Injectable, Logger } from '@nestjs/common'
import { Platform } from '../../domain/version-management'
import * as fs from 'fs'
import * as path from 'path'
import * as os from 'os'
export interface ParsedPackageInfo {
packageName: string
@ -28,10 +31,17 @@ export class PackageParserService {
}
private async parseApk(buffer: Buffer): Promise<ParsedPackageInfo | null> {
// ApkReader 需要文件路径,所以先保存到临时文件
const tempDir = os.tmpdir()
const tempFile = path.join(tempDir, `apk_${Date.now()}_${Math.random().toString(36).slice(2)}.apk`)
try {
// 写入临时文件
fs.writeFileSync(tempFile, buffer)
// 动态导入 adbkit-apkreader
const ApkReader = await import('adbkit-apkreader')
const reader = await ApkReader.default.open(buffer)
const reader = await ApkReader.default.open(tempFile)
const manifest = await reader.readManifest()
return {
@ -44,6 +54,15 @@ export class PackageParserService {
} catch (error) {
this.logger.error('Failed to parse APK:', error)
return null
} finally {
// 清理临时文件
try {
if (fs.existsSync(tempFile)) {
fs.unlinkSync(tempFile)
}
} catch (e) {
this.logger.warn('Failed to cleanup temp APK file:', e)
}
}
}