@@ -6,6 +6,7 @@ import { Injectable, Logger, OnModuleInit } from '@nestjs/common'
66
77import { ConfigsService } from '~/modules/configs/configs.service'
88import { pickImagesFromMarkdown } from '~/utils/pic.util'
9+ import { AsyncQueue } from '~/utils/queue.util'
910import { requireDepsWithInstall } from '~/utils/tool.util'
1011
1112import { HttpService } from './helper.http.service'
@@ -41,8 +42,8 @@ export class ImageService implements OnModuleInit {
4142 ( originImages ?? [ ] ) . map ( ( image ) => [ image . src , { ...image } ] ) ,
4243 )
4344
44- const task = [ ] as Promise < ImageModel > [ ]
45- for ( const src of newImages ) {
45+ const queue = new AsyncQueue ( 2 )
46+ const imageProcessingTasks = newImages . map ( ( src ) => async ( ) => {
4647 const originImage = oldImagesMap . get ( src )
4748 const keys = new Set ( Object . keys ( originImage || { } ) )
4849
@@ -55,44 +56,43 @@ export class ImageService implements OnModuleInit {
5556 )
5657 ) {
5758 result . push ( originImage )
58- continue
59+ return
5960 }
60- const promise = new Promise < ImageModel > ( ( resolve ) => {
61+
62+ try {
6163 this . logger . log ( `Get --> ${ src } ` )
62- this . getOnlineImageSizeAndMeta ( src )
63- . then ( ( { size, accent, blurHash } ) => {
64- const filename = src . split ( '/' ) . pop ( )
65- this . logger . debug (
66- `[${ filename } ]: height: ${ size . height } , width: ${ size . width } , accent: ${ accent } ` ,
67- )
68-
69- resolve ( { ...size , accent, src, blurHash } )
70- } )
71- . catch ( ( error ) => {
72- this . logger . error ( `GET --> ${ src } ${ error . message } ` )
73-
74- const oldRecord = oldImagesMap . get ( src )
75- if ( oldRecord ) {
76- resolve ( oldRecord )
77- } else
78- resolve ( {
79- width : undefined ,
80- height : undefined ,
81- type : undefined ,
82- accent : undefined ,
83- src : undefined ,
84- blurHash : undefined ,
85- } )
64+ const { size, accent, blurHash } =
65+ await this . getOnlineImageSizeAndMeta ( src )
66+ const filename = src . split ( '/' ) . pop ( )
67+ this . logger . debug (
68+ `[${ filename } ]: height: ${ size . height } , width: ${ size . width } , accent: ${ accent } ` ,
69+ )
70+
71+ result . push ( { ...size , accent, src, blurHash } )
72+ } catch ( error ) {
73+ this . logger . error ( `GET --> ${ src } ${ error . message } ` )
74+
75+ const oldRecord = oldImagesMap . get ( src )
76+ if ( oldRecord ) {
77+ result . push ( oldRecord )
78+ } else {
79+ result . push ( {
80+ width : undefined ,
81+ height : undefined ,
82+ type : undefined ,
83+ accent : undefined ,
84+ src : undefined ,
85+ blurHash : undefined ,
8686 } )
87- } )
87+ }
88+ }
89+ } )
8890
89- task . push ( promise )
90- }
91- const images = await Promise . all ( task )
92- result . push ( ...images )
91+ // Add all tasks to the queue and wait for completion
92+ const wait = queue . addMultiple ( imageProcessingTasks )
93+ await wait ( )
9394
9495 // 老图片不要过滤,记录到列头
95-
9696 if ( originImages ) {
9797 for ( const oldImageRecord of originImages ) {
9898 const src = oldImageRecord . src
0 commit comments