Skip to content

Commit fc87897

Browse files
committed
dev: parallelize app icon generation
1 parent 925ebd5 commit fc87897

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

src/backend/src/services/AppIconService.js

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -92,41 +92,45 @@ class AppIconService extends BaseService {
9292
// });
9393

9494
// Writing icons as the system user
95-
await svc_su.sudo(async () => {
96-
for ( const size of ICON_SIZES ) {
97-
const filename = `${data.app_uid}-${size}.png`;
98-
console.log('FILENAME', filename);
99-
const data_url = data.data_url;
100-
const base64 = data_url.split(',')[1];
101-
const input = Buffer.from(base64, 'base64');
102-
103-
// NOTE: A stream would be more ideal than a buffer here
104-
// but we have no way of knowing the output size
105-
// before we finish processing the image.
106-
const output = await this.modules.sharp(input)
107-
.resize(size)
108-
.png()
109-
.toBuffer();
110-
111-
const sys_actor = await svc_su.get_system_actor();
112-
const hl_write = new HLWrite();
113-
await hl_write.run({
114-
destination_or_parent: dir_app_icons,
115-
specified_name: filename,
116-
overwrite: true,
117-
actor: sys_actor,
118-
user: sys_actor.type.user,
119-
no_thumbnail: true,
120-
file: {
121-
size: output.length,
122-
name: filename,
123-
mimetype: 'image/png',
124-
type: 'image/png',
125-
stream: buffer_to_stream(output),
126-
},
127-
});
128-
}
129-
})
95+
const icon_jobs = [];
96+
for ( const size of ICON_SIZES ) {
97+
icon_jobs.push((async () => {
98+
await svc_su.sudo(async () => {
99+
const filename = `${data.app_uid}-${size}.png`;
100+
console.log('FILENAME', filename);
101+
const data_url = data.data_url;
102+
const base64 = data_url.split(',')[1];
103+
const input = Buffer.from(base64, 'base64');
104+
105+
// NOTE: A stream would be more ideal than a buffer here
106+
// but we have no way of knowing the output size
107+
// before we finish processing the image.
108+
const output = await this.modules.sharp(input)
109+
.resize(size)
110+
.png()
111+
.toBuffer();
112+
113+
const sys_actor = await svc_su.get_system_actor();
114+
const hl_write = new HLWrite();
115+
await hl_write.run({
116+
destination_or_parent: dir_app_icons,
117+
specified_name: filename,
118+
overwrite: true,
119+
actor: sys_actor,
120+
user: sys_actor.type.user,
121+
no_thumbnail: true,
122+
file: {
123+
size: output.length,
124+
name: filename,
125+
mimetype: 'image/png',
126+
type: 'image/png',
127+
stream: buffer_to_stream(output),
128+
},
129+
});
130+
})
131+
})());
132+
}
133+
await Promise.all(icon_jobs);
130134
});
131135
}
132136

0 commit comments

Comments
 (0)