mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 12:56:05 +00:00
add image dimension detection for most common formats
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { type EmitsEvents, EventManager } from "core/events";
|
||||
import { type TSchema, isFile } from "core/utils";
|
||||
import { type TSchema, isFile, detectImageDimensions } from "core/utils";
|
||||
import { isMimeType } from "media/storage/mime-types-tiny";
|
||||
import * as StorageEvents from "./events";
|
||||
import type { FileUploadedEventData } from "./events";
|
||||
import { $console } from "core";
|
||||
|
||||
export type FileListObject = {
|
||||
key: string;
|
||||
@@ -10,7 +11,7 @@ export type FileListObject = {
|
||||
size: number;
|
||||
};
|
||||
|
||||
export type FileMeta = { type: string; size: number };
|
||||
export type FileMeta = { type: string; size: number; width?: number; height?: number };
|
||||
export type FileBody = ReadableStream | File;
|
||||
export type FileUploadPayload = {
|
||||
name: string;
|
||||
@@ -102,7 +103,7 @@ export class Storage implements EmitsEvents {
|
||||
}
|
||||
|
||||
// try to get better meta info
|
||||
if (!isMimeType(info?.meta.type, ["application/octet-stream", "application/json"])) {
|
||||
if (!isMimeType(info.meta.type, ["application/octet-stream", "application/json"])) {
|
||||
const meta = await this.#adapter.getObjectMeta(name);
|
||||
if (!meta) {
|
||||
throw new Error("Failed to get object meta");
|
||||
@@ -110,6 +111,19 @@ export class Storage implements EmitsEvents {
|
||||
info.meta = meta;
|
||||
}
|
||||
|
||||
// try to get width/height for images
|
||||
if (info.meta.type.startsWith("image") && (!info.meta.width || !info.meta.height)) {
|
||||
try {
|
||||
const dim = await detectImageDimensions(file as File);
|
||||
info.meta = {
|
||||
...info.meta,
|
||||
...dim,
|
||||
};
|
||||
} catch (e) {
|
||||
$console.warn("Failed to get image dimensions", e);
|
||||
}
|
||||
}
|
||||
|
||||
const eventData = {
|
||||
file,
|
||||
...info,
|
||||
|
||||
Reference in New Issue
Block a user