import fs from 'fs' import path from 'path' import type { Request, Response } from 'express' import { errorResponse, successResponse } from '../utils/response' import { getPrintAgentLatestManifest, getXPrinterOfficialLatestManifest, listPrintAgentReleaseManifests, listXPrinterArchiveManifests, resolvePrintAgentLatestBinary, resolvePrintAgentReleaseBinary, resolveXPrinterArchiveBinary } from '../services/printDownloadService' const getBaseUrl = (req: Request) => { const protocol = (req.get('x-forwarded-proto') || req.protocol || 'http').split(',')[0] const host = req.get('x-forwarded-host') || req.get('host') return `${protocol}://${host}` } const sendBinaryDownload = ( res: Response, filePath: string, downloadName: string, missingMessage: string ) => { if (!fs.existsSync(filePath)) { return errorResponse(res, missingMessage, 404) } return res.download(path.resolve(filePath), downloadName) } export const getPrintAgentLatestDownload = async (req: Request, res: Response) => { try { const { manifest, filePath } = resolvePrintAgentLatestBinary() return sendBinaryDownload(res, filePath, manifest.filename, '最新打印助手安装包尚未上传') } catch (error: any) { return errorResponse(res, error.message || '读取打印助手安装包失败', 404) } } export const getPrintAgentReleaseList = async (req: Request, res: Response) => { try { const baseUrl = getBaseUrl(req) const latestManifest = getPrintAgentLatestManifest() const releases = listPrintAgentReleaseManifests().map((manifest) => ({ ...manifest, download_url: `${baseUrl}/downloads/print-agent/releases/${manifest.version}` })) successResponse( res, { latest_version: latestManifest.version, latest_download_url: `${baseUrl}/downloads/print-agent/latest`, releases }, '获取成功' ) } catch (error: any) { errorResponse(res, error.message || '读取打印助手版本列表失败', 404) } } export const getPrintAgentReleaseDownload = async (req: Request, res: Response) => { try { const version = String(req.params.version || '').trim() if (!version) { return errorResponse(res, '版本号不能为空', 422) } const { manifest, filePath } = resolvePrintAgentReleaseBinary(version) return sendBinaryDownload( res, filePath, manifest.filename, `打印助手 ${version} 安装包尚未上传` ) } catch (error: any) { return errorResponse(res, error.message || '读取打印助手安装包失败', 404) } } export const getXPrinterOfficialLatestController = async (req: Request, res: Response) => { try { const manifest = getXPrinterOfficialLatestManifest() successResponse(res, manifest, '获取成功') } catch (error: any) { errorResponse(res, error.message || '读取芯烨官方驱动信息失败', 404) } } export const getXPrinterArchiveListController = async (req: Request, res: Response) => { try { const baseUrl = getBaseUrl(req) const archives = listXPrinterArchiveManifests().map((manifest) => ({ ...manifest, download_url: `${baseUrl}/downloads/printer-driver/xprinter/archive/${manifest.version}` })) successResponse(res, archives, '获取成功') } catch (error: any) { errorResponse(res, error.message || '读取芯烨归档驱动失败', 404) } } export const getXPrinterArchiveDownloadController = async (req: Request, res: Response) => { try { const version = String(req.params.version || '').trim() if (!version) { return errorResponse(res, '版本号不能为空', 422) } const { manifest, filePath } = resolveXPrinterArchiveBinary(version) return sendBinaryDownload( res, filePath, manifest.filename, `芯烨归档驱动 ${version} 尚未上传` ) } catch (error: any) { return errorResponse(res, error.message || '读取芯烨归档驱动失败', 404) } }