feat: 站点个性化设置

This commit is contained in:
xiaojunnuo
2024-10-05 01:46:25 +08:00
parent ce9a9862f1
commit 11a9fe9014
57 changed files with 710 additions and 763 deletions
@@ -0,0 +1,2 @@
export * from './service/plus-service.js';
export * from './service/file-service.js';
@@ -0,0 +1,86 @@
import { Provide } from '@midwayjs/core';
import dayjs from 'dayjs';
import path from 'path';
import fs from 'fs';
import { cache, logger, utils } from '@certd/pipeline';
import { NotFoundException, ParamException, PermissionException } from '../../../basic/index.js';
export type UploadFileItem = {
filename: string;
tmpFilePath: string;
};
const uploadRootDir = './data/upload';
export const uploadTmpFileCacheKey = 'tmpfile_key_';
/**
*/
@Provide()
export class FileService {
async saveFile(userId: number, tmpCacheKey: any, permission: 'public' | 'private') {
if (tmpCacheKey.startsWith(`/${permission}`)) {
//已经保存过,不需要再次保存
return tmpCacheKey;
}
let fileName = '';
let tmpFilePath = tmpCacheKey;
if (uploadTmpFileCacheKey && tmpCacheKey.startsWith(uploadTmpFileCacheKey)) {
const tmpFile: UploadFileItem = cache.get(tmpCacheKey);
if (!tmpFile) {
throw new ParamException('文件已过期,请重新上传');
}
tmpFilePath = tmpFile.tmpFilePath;
fileName = tmpFile.filename || path.basename(tmpFilePath);
}
if (!tmpFilePath || !fs.existsSync(tmpFilePath)) {
throw new Error('文件不存在,请重新上传');
}
const date = dayjs().format('YYYY_MM_DD');
const random = Math.random().toString(36).substring(7);
const userIdMd5 = Buffer.from(Buffer.from(userId + '').toString('base64')).toString('hex');
const key = `/${permission}/${userIdMd5}/${date}/${random}_${fileName}`;
let savePath = path.join(uploadRootDir, key);
savePath = path.resolve(savePath);
const parentDir = path.dirname(savePath);
if (!fs.existsSync(parentDir)) {
fs.mkdirSync(parentDir, { recursive: true });
}
// eslint-disable-next-line node/no-unsupported-features/node-builtins
const copyFile = utils.promises.promisify(fs.copyFile);
await copyFile(tmpFilePath, savePath);
try {
fs.unlinkSync(tmpFilePath);
} catch (e) {
logger.error(e);
}
return key;
}
getFile(key: string, userId?: number) {
if (!key) {
throw new ParamException('参数错误');
}
if (key.indexOf('..') >= 0) {
//安全性判断
throw new ParamException('参数错误');
}
if (!key.startsWith('/')) {
throw new ParamException('参数错误');
}
const keyArr = key.split('/');
const permission = keyArr[1];
const userIdMd5 = keyArr[2];
if (permission !== 'public') {
//非公开文件需要验证用户
const userIdStr = Buffer.from(Buffer.from(userIdMd5, 'hex').toString('base64')).toString();
const userIdInt: number = parseInt(userIdStr, 10);
if (userId == null || userIdInt !== userId) {
throw new PermissionException('无访问权限');
}
}
const filePath = path.join(uploadRootDir, key);
if (!fs.existsSync(filePath)) {
throw new NotFoundException('文件不存在');
}
return filePath;
}
}
@@ -0,0 +1,78 @@
import { Config, Init, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { AppKey, http, PlusRequestService, verify } from '@certd/pipeline';
import { logger } from '@certd/pipeline';
import { SysInstallInfo, SysLicenseInfo, SysSettingsService } from '../../settings/index.js';
@Provide()
@Scope(ScopeEnum.Singleton)
export class PlusService {
@Inject()
sysSettingsService: SysSettingsService;
@Config('plus.server.baseUrls')
plusServerBaseUrls: string[];
plusRequestService: PlusRequestService;
@Init()
async init() {
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
this.plusRequestService = new PlusRequestService({
plusServerBaseUrls: this.plusServerBaseUrls,
http: http,
logger,
subjectId: installInfo.siteId,
});
}
async requestWithoutSign(config: any) {
return await this.plusRequestService.requestWithoutSign(config);
}
async request(config: any) {
return await this.plusRequestService.request(config);
}
async active(formData: { code: any; appKey: string; subjectId: string }) {
return await this.plusRequestService.requestWithoutSign({
url: '/activation/active',
method: 'post',
data: formData,
});
}
async updateLicense(license: string) {
let licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
if (!licenseInfo) {
licenseInfo = new SysLicenseInfo();
}
licenseInfo.license = license;
await this.sysSettingsService.saveSetting(licenseInfo);
const verifyRes = await this.verify();
if (!verifyRes.isPlus) {
const message = verifyRes.message || '授权码校验失败';
logger.error(message);
throw new Error(message);
}
}
async verify() {
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
return await verify({
subjectId: installInfo.siteId,
license: licenseInfo.license,
plusRequestService: this.plusRequestService,
bindUrl: installInfo?.bindUrl,
});
}
async bindUrl(subjectId: string, url: string) {
return await this.plusRequestService.request({
url: '/activation/subject/urlBind',
data: {
subjectId,
appKey: AppKey,
url,
},
});
}
}