Skip to content

Commit

Permalink
Merge pull request #86 from TorzoClub/dev
Browse files Browse the repository at this point in the history
优化缩略图刷新
  • Loading branch information
VecHK authored Dec 24, 2023
2 parents e6f8872 + 24589b8 commit aaa2d38
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
23 changes: 19 additions & 4 deletions server/app/controller/admin/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,34 @@ module.exports = app => {
}

async refreshThumb(ctx) {
const photos_p = app.model.Photo.findAll();
const gallerys = await app.model.Gallery.findAll();
const photos_list = await Promise.all(
gallerys.map(gallery => {
return gallery.getPhotos({
order: [
[ 'index', 'ASC' ],
],
});
})
);

const photos_list_flated = photos_list.flat();

const members_p = app.model.Member.findAll();

await ctx.service.image.removeAllThumbs();

const src_list = [
...(await photos_p).map(p => p.src),
...photos_list_flated.map(p => p.src),
...(await members_p).map(m => m.avatar_src),
];

await ctx.service.image.generateThumbs(src_list);
const [ successes, failures ] = await ctx.service.image.generateThumbs(src_list);

ctx.backData(200, { });
ctx.backData(200, {
successes,
failures,
});
}
}

Expand Down
10 changes: 9 additions & 1 deletion server/app/service/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,17 @@ module.exports = app =>
}

async generateThumbs(src_list, options) {
const successes = [];
const failures = [];
for (const src of src_list) {
await ImageService.generateThumb(src, options);
try {
await ImageService.generateThumb(src, options);
successes.push(src);
} catch (err) {
failures.push(src);
}
}
return [ successes, failures ];
}

async removeAllThumbs() {
Expand Down
22 changes: 20 additions & 2 deletions server/test/image-upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,36 @@ describe('controller/admin/image', () => {

const member = await createMember(token, app, { qq_num: 22122 })
const gallery = await commonCreateGallery(token, app, {})
await createPhoto(token, app, {
const created_photo = await createPhoto(token, app, {
gallery_id: gallery.id,
member_id: member.id,
src: u_img.src,
})

await app.httpRequest()
const res = await app.httpRequest()
.get('/admin/image/refresh-thumb')
.set('Authorization', token)
.expect(200)

const { successes, failures } = res.body

assert(Array.isArray(successes))
assert(successes.length === 2) // 创建了一张照片和一位用户的头像
assert(Array.isArray(failures))
assert(failures.length === 0)
assert(fs.existsSync(thumb_path) === true)

{
fs.unlinkSync(path.join(app.config.imageSavePath, created_photo.src))
const res = await app.httpRequest()
.get('/admin/image/refresh-thumb')
.set('Authorization', token)
.expect(200)

const { successes, failures } = res.body
assert(successes.length === 1)
assert(failures.length === 1)
}
})

// it('check unuse image after refresh thumb', async () => {
Expand Down

0 comments on commit aaa2d38

Please sign in to comment.