Refine storage file size reporting

This commit is contained in:
Sam Becker 2025-03-15 11:33:59 -05:00
parent 2b9d12d43d
commit 3e0f2202d3
3 changed files with 10 additions and 4 deletions

View File

@ -6,6 +6,7 @@ import {
PutObjectCommand,
} from '@aws-sdk/client-s3';
import { StorageListResponse, generateStorageId } from '.';
import { formatBytesToMB } from '@/utility/number';
const AWS_S3_BUCKET = process.env.NEXT_PUBLIC_AWS_S3_BUCKET ?? '';
const AWS_S3_REGION = process.env.NEXT_PUBLIC_AWS_S3_REGION ?? '';
@ -74,7 +75,7 @@ export const awsS3List = async (
url: urlForKey(Key),
fileName: Key ?? '',
uploadedAt: LastModified,
size: Size ? `${Size.toFixed(2)}MB` : undefined,
size: Size ? formatBytesToMB(Size) : undefined,
})) ?? []);
export const awsS3Delete = async (Key: string) => {

View File

@ -7,6 +7,7 @@ import {
} from '@aws-sdk/client-s3';
import { StorageListResponse, generateStorageId } from '.';
import { removeUrlProtocol } from '@/utility/url';
import { formatBytesToMB } from '@/utility/number';
const CLOUDFLARE_R2_BUCKET =
process.env.NEXT_PUBLIC_CLOUDFLARE_R2_BUCKET ?? '';
@ -94,7 +95,7 @@ export const cloudflareR2List = async (
url: urlForKey(Key),
fileName: Key ?? '',
uploadedAt: LastModified,
size: Size ? `${Size.toFixed(2)}MB` : undefined,
size: Size ? formatBytesToMB(Size) : undefined,
})) ?? []);
export const cloudflareR2Delete = async (Key: string) => {

View File

@ -85,5 +85,9 @@ export const formatNumberToFraction = (number: number) => {
}
};
export const formatBytesToMB = (bytes: number) =>
`${(bytes / 1024 / 1024).toFixed(2)}MB`;
export const formatBytesToMB = (
bytes: number,
byteSize = 1000,
precision = 1,
) =>
`${(bytes / byteSize / byteSize).toFixed(precision)}MB`;