mirror of
https://github.com/certd/certd.git
synced 2025-11-25 09:10:11 +08:00
chore: clear file
This commit is contained in:
@@ -34,10 +34,17 @@ export class FileStore {
|
||||
}
|
||||
|
||||
private buildFilePath(filename: string) {
|
||||
const parentDir = path.join(this.rootDir, this.scope + "", dayjs().format("YYYY-MM-DD"), this.parent + "");
|
||||
const parentDir = path.join(this.rootDir, this.scope + "", this.parent + "", dayjs().format("YYYY-MM-DD"));
|
||||
if (!fs.existsSync(parentDir)) {
|
||||
fs.mkdirSync(parentDir, { recursive: true });
|
||||
}
|
||||
return path.join(parentDir, filename);
|
||||
}
|
||||
|
||||
deleteByParent(scope: string, parent: string) {
|
||||
const dir = path.join(this.rootDir, scope, parent);
|
||||
if (fs.existsSync(dir)) {
|
||||
fs.unlinkSync(dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,3 +2,4 @@ export * from "./executor";
|
||||
export * from "./run-history";
|
||||
export * from "./context";
|
||||
export * from "./storage";
|
||||
export * from "./file-store";
|
||||
|
||||
@@ -78,6 +78,12 @@ export abstract class AbstractTaskPlugin implements ITaskPlugin {
|
||||
this.ctx = ctx;
|
||||
}
|
||||
|
||||
linkFile(file: FileItem) {
|
||||
this._result.files!.push({
|
||||
...file,
|
||||
id: uuidv4(),
|
||||
});
|
||||
}
|
||||
saveFile(filename: string, file: Buffer) {
|
||||
const filePath = this.ctx.fileStore.writeFile(filename, file);
|
||||
this._result.files!.push({
|
||||
|
||||
@@ -71,6 +71,9 @@ const development = {
|
||||
expire: 7 * 24 * 60, //单位秒
|
||||
},
|
||||
},
|
||||
certd: {
|
||||
fileRootDir: '/app/data/files',
|
||||
},
|
||||
} as MidwayConfig;
|
||||
mergeConfig(development, 'development');
|
||||
export default development;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
|
||||
import { Config, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
|
||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { BaseService } from '../../../basic/base-service';
|
||||
@@ -7,6 +7,8 @@ import { PipelineEntity } from '../entity/pipeline';
|
||||
import { HistoryDetail } from '../entity/vo/history-detail';
|
||||
import { HistoryLogService } from './history-log-service';
|
||||
import { FileItem, Pipeline, RunnableCollection } from '@certd/pipeline';
|
||||
import { FileStore } from '@certd/pipeline';
|
||||
import { logger } from '../../../utils/logger';
|
||||
|
||||
/**
|
||||
* 证书申请
|
||||
@@ -18,6 +20,10 @@ export class HistoryService extends BaseService<HistoryEntity> {
|
||||
repository: Repository<HistoryEntity>;
|
||||
@Inject()
|
||||
logService: HistoryLogService;
|
||||
|
||||
@Config('certd')
|
||||
private certdConfig: any;
|
||||
|
||||
getRepository() {
|
||||
return this.repository;
|
||||
}
|
||||
@@ -60,6 +66,11 @@ export class HistoryService extends BaseService<HistoryEntity> {
|
||||
}
|
||||
let shouldDeleteCount = count - keepCount;
|
||||
const deleteCountBatch = 100;
|
||||
const fileStore = new FileStore({
|
||||
rootDir: this.certdConfig.fileRootDir,
|
||||
scope: pipelineId + '',
|
||||
parent: '0',
|
||||
});
|
||||
while (shouldDeleteCount > 0) {
|
||||
const list = await this.repository.find({
|
||||
select: {
|
||||
@@ -76,6 +87,15 @@ export class HistoryService extends BaseService<HistoryEntity> {
|
||||
});
|
||||
await this.repository.remove(list);
|
||||
|
||||
for (const historyEntity of list) {
|
||||
const id = historyEntity.id;
|
||||
try {
|
||||
fileStore.deleteByParent(pipelineId + '', id + '');
|
||||
} catch (e) {
|
||||
logger.error('删除文件失败', e);
|
||||
}
|
||||
}
|
||||
|
||||
shouldDeleteCount -= deleteCountBatch;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
|
||||
import { Config, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
|
||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||
import { In, Repository } from 'typeorm';
|
||||
import { BaseService } from '../../../basic/base-service';
|
||||
@@ -38,6 +38,9 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
||||
@Inject()
|
||||
cron: Cron;
|
||||
|
||||
@Config('certd')
|
||||
private certdConfig: any;
|
||||
|
||||
getRepository() {
|
||||
return this.repository;
|
||||
}
|
||||
@@ -213,6 +216,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
||||
accessService: this.accessService,
|
||||
storage: new DbStorage(userId, this.storageService),
|
||||
emailService: this.emailService,
|
||||
fileRootDir: this.certdConfig.fileRootDir,
|
||||
});
|
||||
try {
|
||||
await executor.init();
|
||||
|
||||
Reference in New Issue
Block a user