diff --git a/server/app/controller/admin/image.js b/server/app/controller/admin/image.js index 43f2f5e..2c2887e 100644 --- a/server/app/controller/admin/image.js +++ b/server/app/controller/admin/image.js @@ -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, + }); } } diff --git a/server/app/service/image.js b/server/app/service/image.js index 8d1e708..f806df0 100644 --- a/server/app/service/image.js +++ b/server/app/service/image.js @@ -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() { diff --git a/server/test/image-upload.test.js b/server/test/image-upload.test.js index 0dcbfbe..73f17dd 100644 --- a/server/test/image-upload.test.js +++ b/server/test/image-upload.test.js @@ -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 () => {