refactor avatar resizing
This commit is contained in:
parent
736b8498af
commit
61d24ddd7f
@ -111,6 +111,35 @@ export class TimelineReader {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
private static resizeAvatarPromiseMaker(
|
||||
avatarFilenameBase: string,
|
||||
baseSize: number,
|
||||
maxPixelDensity: number,
|
||||
accountUrl: string,
|
||||
formats: string[],
|
||||
avatar: ArrayBuffer
|
||||
): Promise<void>[] {
|
||||
const sharpAvatar = sharp(avatar);
|
||||
const promises: Promise<void>[] = [];
|
||||
for (let i = 1; i <= maxPixelDensity; i++) {
|
||||
promises.push(
|
||||
...formats.map((f) =>
|
||||
TimelineReader.resizeAvatar(avatarFilenameBase, baseSize * i, `${i}x.${f}`, sharpAvatar)
|
||||
.then(
|
||||
(fn) =>
|
||||
({
|
||||
accountUrl: accountUrl,
|
||||
file: fn,
|
||||
sizeDescriptor: `${i}x`
|
||||
} as AccountAvatar)
|
||||
)
|
||||
.then(saveAvatar)
|
||||
)
|
||||
);
|
||||
}
|
||||
return promises;
|
||||
}
|
||||
|
||||
private static async saveAvatar(account: Account) {
|
||||
try {
|
||||
const existingAvatars = await getAvatars(account.url, 1);
|
||||
@ -132,98 +161,16 @@ export class TimelineReader {
|
||||
}
|
||||
const avatarResponse = await fetch(account.avatar);
|
||||
const avatar = await avatarResponse.arrayBuffer();
|
||||
const sharpAvatar = sharp(avatar);
|
||||
await Promise.all([
|
||||
TimelineReader.resizeAvatar(avatarFilenameBase, 50, '1x.webp', sharpAvatar)
|
||||
.then(
|
||||
(fn) =>
|
||||
({
|
||||
accountUrl: account.url,
|
||||
file: fn,
|
||||
sizeDescriptor: '1x'
|
||||
} as AccountAvatar)
|
||||
await Promise.all(
|
||||
TimelineReader.resizeAvatarPromiseMaker(
|
||||
avatarFilenameBase,
|
||||
50,
|
||||
3,
|
||||
account.url,
|
||||
['webp', 'avif', 'jpeg'],
|
||||
avatar
|
||||
)
|
||||
.then(saveAvatar),
|
||||
TimelineReader.resizeAvatar(avatarFilenameBase, 100, '2x.webp', sharpAvatar)
|
||||
.then(
|
||||
(fn) =>
|
||||
({
|
||||
accountUrl: account.url,
|
||||
file: fn,
|
||||
sizeDescriptor: '2x'
|
||||
} as AccountAvatar)
|
||||
)
|
||||
.then(saveAvatar),
|
||||
TimelineReader.resizeAvatar(avatarFilenameBase, 150, '3x.webp', sharpAvatar)
|
||||
.then(
|
||||
(fn) =>
|
||||
({
|
||||
accountUrl: account.url,
|
||||
file: fn,
|
||||
sizeDescriptor: '3x'
|
||||
} as AccountAvatar)
|
||||
)
|
||||
.then(saveAvatar),
|
||||
TimelineReader.resizeAvatar(avatarFilenameBase, 50, '1x.avif', sharpAvatar)
|
||||
.then(
|
||||
(fn) =>
|
||||
({
|
||||
accountUrl: account.url,
|
||||
file: fn,
|
||||
sizeDescriptor: '1x'
|
||||
} as AccountAvatar)
|
||||
)
|
||||
.then(saveAvatar),
|
||||
TimelineReader.resizeAvatar(avatarFilenameBase, 100, '2x.avif', sharpAvatar)
|
||||
.then(
|
||||
(fn) =>
|
||||
({
|
||||
accountUrl: account.url,
|
||||
file: fn,
|
||||
sizeDescriptor: '2x'
|
||||
} as AccountAvatar)
|
||||
)
|
||||
.then(saveAvatar),
|
||||
TimelineReader.resizeAvatar(avatarFilenameBase, 150, '3x.avif', sharpAvatar)
|
||||
.then(
|
||||
(fn) =>
|
||||
({
|
||||
accountUrl: account.url,
|
||||
file: fn,
|
||||
sizeDescriptor: '3x'
|
||||
} as AccountAvatar)
|
||||
)
|
||||
.then(saveAvatar),
|
||||
TimelineReader.resizeAvatar(avatarFilenameBase, 50, '1x.jpeg', sharpAvatar)
|
||||
.then((fn) => {
|
||||
return {
|
||||
accountUrl: account.url,
|
||||
file: fn,
|
||||
sizeDescriptor: '1x'
|
||||
} as AccountAvatar;
|
||||
})
|
||||
.then(saveAvatar),
|
||||
TimelineReader.resizeAvatar(avatarFilenameBase, 100, '2x.jpeg', sharpAvatar)
|
||||
.then(
|
||||
(fn) =>
|
||||
({
|
||||
accountUrl: account.url,
|
||||
file: fn,
|
||||
sizeDescriptor: '2x'
|
||||
} as AccountAvatar)
|
||||
)
|
||||
.then(saveAvatar),
|
||||
TimelineReader.resizeAvatar(avatarFilenameBase, 150, '3x.jpeg', sharpAvatar)
|
||||
.then(
|
||||
(fn) =>
|
||||
({
|
||||
accountUrl: account.url,
|
||||
file: fn,
|
||||
sizeDescriptor: '3x'
|
||||
} as AccountAvatar)
|
||||
)
|
||||
.then(saveAvatar)
|
||||
]);
|
||||
);
|
||||
} catch (e) {
|
||||
console.error('Could not resize and save avatar for', account.acct, account.avatar, e);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user