Skip to content

Commit

Permalink
[add] auto create thumb/src directory when app's env is 'unitest'
Browse files Browse the repository at this point in the history
  • Loading branch information
VecHK committed Jan 6, 2024
1 parent 9c48a4b commit b82d2d2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
28 changes: 27 additions & 1 deletion server/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const fs = require('fs');

class WarningError extends Error {
constructor(message, status) {
super(...arguments);
Expand All @@ -17,9 +19,31 @@ class AppBootHook {
});
}

async checkDetectImagePoolPath() {
const {
env, imageThumbSavePath, imageSavePath,
} = this.app.config;

if (env === 'unittest') {
if (!fs.existsSync(imageThumbSavePath)) {
await fs.promises.mkdir(imageThumbSavePath, { recursive: true });
}
if (!fs.existsSync(imageSavePath)) {
await fs.promises.mkdir(imageSavePath, { recursive: true });
}
} else {
if (!fs.existsSync(imageThumbSavePath)) {
throw Error('缩略图存放路径不存在!');
}
if (!fs.existsSync(imageSavePath)) {
throw Error('原图存放路径不存在!');
}
}
}

async willReady() {
const { app } = this;
const { env /* , startBeforeGenerateThumb */ } = app.config;
const { env } = app.config;

if (env === 'local') {
await app.model.sync({
Expand All @@ -28,6 +52,8 @@ class AppBootHook {
});
}

await this.checkDetectImagePoolPath();

app.validator.addRule('qq_num', (rule, qq_num) => {
if (!Number.isInteger(Number(qq_num))) {
return '需要是整数格式';
Expand Down
12 changes: 6 additions & 6 deletions server/app/controller/admin/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ const sendToWormhole = require('stream-wormhole');

module.exports = app => {
class ImageController extends app.Controller {
thumbSize(width_raw) {
thumbSize(size_raw) {
const default_size = app.config.default_image_thumb_size;
if (width_raw === undefined) {
if (size_raw === undefined) {
return default_size;
} else {
const width = Math.abs(Number(width_raw));
if (Number.isNaN(width) || !Number.isInteger(width)) {
const size = Math.abs(Number(size_raw));
if (Number.isNaN(size) || !Number.isInteger(size)) {
throw new app.WarningError('指定的尺寸需要是有效的整数', 400);
} else if (width > 9999) {
} else if (size > 9999) {
throw new app.WarningError('指定的尺寸过大', 400);
} else {
return width;
return size;
}
}
}
Expand Down
Empty file removed server/test/static/src/.gitkeep
Empty file.
Empty file removed server/test/static/thumb/.gitkeep
Empty file.

0 comments on commit b82d2d2

Please sign in to comment.