mirror of
https://github.com/whyour/qinglong.git
synced 2026-07-01 04:40:38 +08:00
写入文件增加文件锁,避免竞争条件引起文件内容异常
This commit is contained in:
+4
-3
@@ -8,6 +8,7 @@ import { celebrate, Joi } from 'celebrate';
|
||||
import path, { join, parse } from 'path';
|
||||
import ScriptService from '../services/script';
|
||||
import multer from 'multer';
|
||||
import { writeFileWithLock } from '../shared/utils';
|
||||
const route = Router();
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
@@ -156,7 +157,7 @@ export default (app: Router) => {
|
||||
await rmPath(originFilePath);
|
||||
}
|
||||
}
|
||||
await fs.writeFile(filePath, content);
|
||||
await writeFileWithLock(filePath, content);
|
||||
return res.send({ code: 200 });
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
@@ -182,7 +183,7 @@ export default (app: Router) => {
|
||||
path: string;
|
||||
};
|
||||
const filePath = join(config.scriptPath, path, filename);
|
||||
await fs.writeFile(filePath, content);
|
||||
await writeFileWithLock(filePath, content);
|
||||
return res.send({ code: 200 });
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
@@ -261,7 +262,7 @@ export default (app: Router) => {
|
||||
let { filename, content, path } = req.body;
|
||||
const { name, ext } = parse(filename);
|
||||
const filePath = join(config.scriptPath, path, `${name}.swap${ext}`);
|
||||
await fs.writeFile(filePath, content || '', { encoding: 'utf8' });
|
||||
await writeFileWithLock(filePath, content || '');
|
||||
|
||||
const scriptService = Container.get(ScriptService);
|
||||
const result = await scriptService.runScript(filePath);
|
||||
|
||||
Reference in New Issue
Block a user