Skip to content

Commit

Permalink
[add] cancel auto clean unused image and set to API
Browse files Browse the repository at this point in the history
  • Loading branch information
VecHK committed Jan 9, 2024
1 parent d439721 commit 554e18e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 89 deletions.
73 changes: 36 additions & 37 deletions server/app/controller/admin/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const sendToWormhole = require('stream-wormhole');
const path = require('path');
const fs = require('fs');

module.exports = app => {
class ImageController extends app.Controller {
Expand Down Expand Up @@ -43,23 +44,10 @@ module.exports = app => {
}

async getAllAvailablePhoto(ctx) {
const list = await ctx.app.model.Gallery.findAll({
order: [
[ 'index', 'DESC' ],
],
});

const photos_list = await Promise.all(
list.map(gallery => {
return gallery.getPhotos({
order: [
[ 'index', 'ASC' ],
],
});
})
ctx.backData(
200,
await ctx.service.photo.getAvailablePhotoList()
);

ctx.backData(200, photos_list.flat());
}

async refreshThumb(ctx) {
Expand All @@ -79,28 +67,39 @@ module.exports = app => {
ctx.backData(200, result);
}

async refreshMemberThumb(ctx) {
ctx.validate({
member_id: { type: 'id', required: true },
}, ctx.params);

const member = await ctx.service.member.findById(ctx.params.member_id);

await app.serviceClasses.image.removeThumb(member.avatar_thumb);
const result = await app.serviceClasses.image.generateThumb(member.avatar_thumb);
ctx.backData(200, result);
}

async refreshPhotoThumb(ctx) {
ctx.validate({
photo_id: { type: 'id', required: true },
}, ctx.params);

const photo = await ctx.service.photo.findById(ctx.params.photo_id);
async cleanUnusedImage(ctx) {
const [ available_photo_list, member_list ] = await Promise.all([
ctx.service.photo.getAvailablePhotoList(),
ctx.model.Member.findAll({}),
]);

const used_src_list = [
...available_photo_list.map(p => p.src),
...member_list.map(m => m.avatar_src),
].map(f => path.parse(f).name);

const { imageSavePath, imageThumbSavePath } = ctx.app.config;

const file_list = [
...fs.readdirSync(imageSavePath).map(f => path.join(imageSavePath, f)),
...fs.readdirSync(imageThumbSavePath).map(f => path.join(imageThumbSavePath, f)),
];

const clean_list = [];

for (const file of file_list) {
const { name: file_name } = path.parse(file);
const is_used = used_src_list.includes(file_name);
if (!is_used) {
if (fs.lstatSync(file).isFile()) {
fs.unlinkSync(file);
ctx.app.logger.info(`auto clean used image: ${file}`);
clean_list.push(file);
}
}
}

await app.serviceClasses.image.removeThumb(photo.thumb);
const result = await app.serviceClasses.image.generateThumb(photo.thumb);
ctx.backData(200, result);
ctx.backData(200, clean_list);
}
}

Expand Down
5 changes: 3 additions & 2 deletions server/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ module.exports = app => {

setAdminRouter('get', 'statistic', controller.admin.statistic.show);

setAdminRouter('post', 'image/upload', controller.admin.image.upload);
setAdminRouter('post', 'image/refresh-thumb', controller.admin.image.refreshThumb);
setAdminRouter('get', 'image/available-photo', controller.admin.image.getAllAvailablePhoto);
setAdminRouter('post', 'image/refresh-thumb', controller.admin.image.refreshThumb);
setAdminRouter('post', 'image/clean-unused', controller.admin.image.cleanUnusedImage);
setAdminRouter('post', 'image/upload', controller.admin.image.upload);

{
const { create, remove, get, show, edit } = controller.admin.gallery;
Expand Down
41 changes: 0 additions & 41 deletions server/app/schedule/clean-unused-image.js

This file was deleted.

6 changes: 0 additions & 6 deletions server/config/config.unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ module.exports = appInfo => {
// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1570827005712_4094';

// add your middleware config here
config.middleware = [
'backData',
'errorHandler',
];

config.static = {
prefix: path.join(config.apiPrefix, './'),
dir: staticPath,
Expand Down
4 changes: 2 additions & 2 deletions server/test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@ async function fetchListWithQQNum(app, qq_num, expectStatusCode = 200) {
async function uploadImage(
token,
app,
imagePath = default_upload_image_path
image_path = default_upload_image_path
) {
const { body: newImage } = await app.httpRequest()
.post('/admin/image/upload')
.set('Authorization', token)
.field('name', `image-${Date.now()}`)
.attach('image', imagePath)
.attach('image', image_path)
.expect(200);

assert(typeof newImage.src === 'string');
Expand Down
9 changes: 8 additions & 1 deletion server/test/image-upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,14 @@ describe('controller/admin/image', function () {
await uploadImage(token, app),
]

await app.runSchedule('clean-unused-image')
const { body: clean_list } = await app
.httpRequest()
.post('/admin/image/clean-unused')
.set('Authorization', token)
.expect(200)

assert(Array.isArray(clean_list) === true)
assert(clean_list.length !== 0)

{
const file_list =
Expand Down

0 comments on commit 554e18e

Please sign in to comment.