Fix remote image urls not showing up in image grids (#430)
All checks were successful
Test / build (push) Successful in 22s
All checks were successful
Test / build (push) Successful in 22s
- Update the Image Grid helper to download images that are urls instead of trying (and failing) to load them locally #425 Reviewed-on: #430 Reviewed-by: VylpesTester <tester@vylpes.com> Co-authored-by: Ethan Lane <ethan@vylpes.com> Co-committed-by: Ethan Lane <ethan@vylpes.com>
This commit is contained in:
parent
ce0bc15c02
commit
ad34cc7b7f
4 changed files with 424 additions and 350 deletions
|
@ -3,7 +3,8 @@ import path from "path";
|
|||
import AppLogger from "../client/appLogger";
|
||||
import {existsSync} from "fs";
|
||||
import Inventory from "../database/entities/app/Inventory";
|
||||
import Jimp from "jimp";
|
||||
import { Bitmap, Jimp } from "jimp";
|
||||
import axios from "axios";
|
||||
|
||||
interface CardInput {
|
||||
id: string;
|
||||
|
@ -29,14 +30,24 @@ export default class ImageHelper {
|
|||
|
||||
const filePath = path.join(process.env.DATA_DIR!, "cards", card.path);
|
||||
|
||||
const exists = existsSync(filePath);
|
||||
let bitmap: Bitmap;
|
||||
|
||||
if (!exists) {
|
||||
if (existsSync(filePath)) {
|
||||
const data = await Jimp.read(filePath);
|
||||
|
||||
bitmap = data.bitmap;
|
||||
} else if (card.path.startsWith("http://") || card.path.startsWith("https://")) {
|
||||
const response = await axios.get(card.path, { responseType: "arraybuffer" });
|
||||
const buffer = Buffer.from(response.data);
|
||||
const data = await Jimp.fromBuffer(buffer);
|
||||
|
||||
bitmap = data.bitmap;
|
||||
} else {
|
||||
AppLogger.LogError("ImageHelper/GenerateCardImageGrid", `Failed to load image from path ${card.path}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const imageData = await Jimp.read(filePath);
|
||||
const imageData = Jimp.fromBitmap(bitmap);
|
||||
|
||||
if (userId != null) {
|
||||
const claimed = await Inventory.FetchOneByCardNumberAndUserId(userId, card.id);
|
||||
|
@ -46,7 +57,7 @@ export default class ImageHelper {
|
|||
}
|
||||
}
|
||||
|
||||
const image = await loadImage(await imageData.getBufferAsync("image/png"));
|
||||
const image = await loadImage(await imageData.getBuffer("image/png"));
|
||||
|
||||
const x = i % gridWidth;
|
||||
const y = Math.floor(i / gridWidth);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue