Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化缩略图刷新 #86

Merged
merged 1 commit into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading