mirror of
				https://github.com/whyour/qinglong.git
				synced 2025-11-04 02:56:07 +08:00 
			
		
		
		
	修复脚本管理数据
This commit is contained in:
		
							parent
							
								
									ee0b47d101
								
							
						
					
					
						commit
						a2ce3304de
					
				| 
						 | 
					@ -1,10 +1,15 @@
 | 
				
			||||||
import { getFileContentByName, getLastModifyFilePath } from '../config/util';
 | 
					import {
 | 
				
			||||||
 | 
					  fileExist,
 | 
				
			||||||
 | 
					  getFileContentByName,
 | 
				
			||||||
 | 
					  getLastModifyFilePath,
 | 
				
			||||||
 | 
					} from '../config/util';
 | 
				
			||||||
import { Router, Request, Response, NextFunction } from 'express';
 | 
					import { Router, Request, Response, NextFunction } from 'express';
 | 
				
			||||||
import { Container } from 'typedi';
 | 
					import { Container } from 'typedi';
 | 
				
			||||||
import { Logger } from 'winston';
 | 
					import { Logger } from 'winston';
 | 
				
			||||||
import config from '../config';
 | 
					import config from '../config';
 | 
				
			||||||
import * as fs from 'fs';
 | 
					import * as fs from 'fs';
 | 
				
			||||||
import { celebrate, Joi } from 'celebrate';
 | 
					import { celebrate, Joi } from 'celebrate';
 | 
				
			||||||
 | 
					import path from 'path';
 | 
				
			||||||
const route = Router();
 | 
					const route = Router();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default (app: Router) => {
 | 
					export default (app: Router) => {
 | 
				
			||||||
| 
						 | 
					@ -16,49 +21,55 @@ export default (app: Router) => {
 | 
				
			||||||
      const logger: Logger = Container.get('logger');
 | 
					      const logger: Logger = Container.get('logger');
 | 
				
			||||||
      try {
 | 
					      try {
 | 
				
			||||||
        const fileList = fs.readdirSync(config.scriptPath, 'utf-8');
 | 
					        const fileList = fs.readdirSync(config.scriptPath, 'utf-8');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let result = [];
 | 
				
			||||||
 | 
					        for (let i = 0; i < fileList.length; i++) {
 | 
				
			||||||
 | 
					          const fileOrDir = fileList[i];
 | 
				
			||||||
 | 
					          const fPath = path.join(config.scriptPath, fileOrDir);
 | 
				
			||||||
 | 
					          const dirStat = fs.statSync(fPath);
 | 
				
			||||||
 | 
					          if (['node_modules'].includes(fileOrDir)) {
 | 
				
			||||||
 | 
					            continue;
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          if (dirStat.isDirectory()) {
 | 
				
			||||||
 | 
					            const childFileList = fs.readdirSync(fPath, 'utf-8');
 | 
				
			||||||
 | 
					            let children = [];
 | 
				
			||||||
 | 
					            for (let j = 0; j < childFileList.length; j++) {
 | 
				
			||||||
 | 
					              const childFile = childFileList[j];
 | 
				
			||||||
 | 
					              const sPath = path.join(config.scriptPath, fileOrDir, childFile);
 | 
				
			||||||
 | 
					              const _fileExist = await fileExist(sPath);
 | 
				
			||||||
 | 
					              if (_fileExist && fs.statSync(sPath).isFile()) {
 | 
				
			||||||
 | 
					                const statObj = fs.statSync(sPath);
 | 
				
			||||||
 | 
					                children.push({
 | 
				
			||||||
 | 
					                  title: childFile,
 | 
				
			||||||
 | 
					                  value: childFile,
 | 
				
			||||||
 | 
					                  key: childFile,
 | 
				
			||||||
 | 
					                  mtime: statObj.mtimeMs,
 | 
				
			||||||
 | 
					                  parent: fileOrDir,
 | 
				
			||||||
 | 
					                });
 | 
				
			||||||
 | 
					              }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            result.push({
 | 
				
			||||||
 | 
					              title: fileOrDir,
 | 
				
			||||||
 | 
					              value: fileOrDir,
 | 
				
			||||||
 | 
					              key: fileOrDir,
 | 
				
			||||||
 | 
					              mtime: dirStat.mtimeMs,
 | 
				
			||||||
 | 
					              disabled: true,
 | 
				
			||||||
 | 
					              children: children.sort((a, b) => b.mtime - a.mtime),
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					          } else {
 | 
				
			||||||
 | 
					            result.push({
 | 
				
			||||||
 | 
					              title: fileOrDir,
 | 
				
			||||||
 | 
					              value: fileOrDir,
 | 
				
			||||||
 | 
					              key: fileOrDir,
 | 
				
			||||||
 | 
					              mtime: dirStat.mtimeMs,
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        res.send({
 | 
					        res.send({
 | 
				
			||||||
          code: 200,
 | 
					          code: 200,
 | 
				
			||||||
          data: fileList
 | 
					          data: result,
 | 
				
			||||||
            .map((x) => {
 | 
					 | 
				
			||||||
              if (fs.lstatSync(config.scriptPath + x).isDirectory()) {
 | 
					 | 
				
			||||||
                const childFileList = fs.readdirSync(
 | 
					 | 
				
			||||||
                  config.scriptPath + x,
 | 
					 | 
				
			||||||
                  'utf-8',
 | 
					 | 
				
			||||||
                );
 | 
					 | 
				
			||||||
                const dirStat = fs.statSync(`${config.scriptPath}${x}`);
 | 
					 | 
				
			||||||
                return {
 | 
					 | 
				
			||||||
                  title: x,
 | 
					 | 
				
			||||||
                  value: x,
 | 
					 | 
				
			||||||
                  key: x,
 | 
					 | 
				
			||||||
                  mtime: dirStat.mtimeMs,
 | 
					 | 
				
			||||||
                  disabled: true,
 | 
					 | 
				
			||||||
                  children: childFileList
 | 
					 | 
				
			||||||
                    .filter(
 | 
					 | 
				
			||||||
                      (y) =>
 | 
					 | 
				
			||||||
                        !fs
 | 
					 | 
				
			||||||
                          .lstatSync(`${config.scriptPath}${x}/${y}`)
 | 
					 | 
				
			||||||
                          .isDirectory(),
 | 
					 | 
				
			||||||
                    )
 | 
					 | 
				
			||||||
                    .map((y) => {
 | 
					 | 
				
			||||||
                      const statObj = fs.statSync(
 | 
					 | 
				
			||||||
                        `${config.scriptPath}${x}/${y}`,
 | 
					 | 
				
			||||||
                      );
 | 
					 | 
				
			||||||
                      return {
 | 
					 | 
				
			||||||
                        title: y,
 | 
					 | 
				
			||||||
                        value: y,
 | 
					 | 
				
			||||||
                        key: y,
 | 
					 | 
				
			||||||
                        mtime: statObj.mtimeMs,
 | 
					 | 
				
			||||||
                        parent: x,
 | 
					 | 
				
			||||||
                      };
 | 
					 | 
				
			||||||
                    })
 | 
					 | 
				
			||||||
                    .sort((a, b) => b.mtime - a.mtime),
 | 
					 | 
				
			||||||
                };
 | 
					 | 
				
			||||||
              } else {
 | 
					 | 
				
			||||||
                const statObj = fs.statSync(config.scriptPath + x);
 | 
					 | 
				
			||||||
                return { title: x, value: x, key: x, mtime: statObj.mtimeMs };
 | 
					 | 
				
			||||||
              }
 | 
					 | 
				
			||||||
            })
 | 
					 | 
				
			||||||
            .sort((a, b) => b.mtime - a.mtime),
 | 
					 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
      } catch (e) {
 | 
					      } catch (e) {
 | 
				
			||||||
        logger.error('🔥 error: %o', e);
 | 
					        logger.error('🔥 error: %o', e);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -220,3 +220,14 @@ export function getPlatform(userAgent: string): 'mobile' | 'desktop' {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return platform as 'mobile' | 'desktop';
 | 
					  return platform as 'mobile' | 'desktop';
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export async function fileExist(file: any) {
 | 
				
			||||||
 | 
					  return new Promise((resolve) => {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      fs.accessSync(file);
 | 
				
			||||||
 | 
					      resolve(true);
 | 
				
			||||||
 | 
					    } catch (error) {
 | 
				
			||||||
 | 
					      resolve(false);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,6 +2,7 @@ import DataStore from 'nedb';
 | 
				
			||||||
import config from '../config';
 | 
					import config from '../config';
 | 
				
			||||||
import Logger from './logger';
 | 
					import Logger from './logger';
 | 
				
			||||||
import fs from 'fs';
 | 
					import fs from 'fs';
 | 
				
			||||||
 | 
					import { fileExist } from '../config/util';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
interface Dbs {
 | 
					interface Dbs {
 | 
				
			||||||
  cronDb: DataStore;
 | 
					  cronDb: DataStore;
 | 
				
			||||||
| 
						 | 
					@ -13,17 +14,6 @@ interface Dbs {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const db: Dbs = {} as any;
 | 
					const db: Dbs = {} as any;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function fileExist(file: any) {
 | 
					 | 
				
			||||||
  return new Promise((resolve) => {
 | 
					 | 
				
			||||||
    try {
 | 
					 | 
				
			||||||
      fs.accessSync(file);
 | 
					 | 
				
			||||||
      resolve(true);
 | 
					 | 
				
			||||||
    } catch (error) {
 | 
					 | 
				
			||||||
      resolve(false);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
async function truncateDb() {
 | 
					async function truncateDb() {
 | 
				
			||||||
  return new Promise(async (resolve) => {
 | 
					  return new Promise(async (resolve) => {
 | 
				
			||||||
    const files = [
 | 
					    const files = [
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -104,7 +104,8 @@ add_cron() {
 | 
				
			||||||
    local detail=""
 | 
					    local detail=""
 | 
				
			||||||
    cd $dir_scripts
 | 
					    cd $dir_scripts
 | 
				
			||||||
    for file in $(cat $list_add); do
 | 
					    for file in $(cat $list_add); do
 | 
				
			||||||
        local file_name=${file/${path}\_/}
 | 
					        local file_name=${file/${path}\//}
 | 
				
			||||||
 | 
					        file_name=${file/${path}\_/}
 | 
				
			||||||
        if [[ -f $file ]]; then
 | 
					        if [[ -f $file ]]; then
 | 
				
			||||||
            cron_line=$(
 | 
					            cron_line=$(
 | 
				
			||||||
                perl -ne "{
 | 
					                perl -ne "{
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user