Skip to content

Commit f4e4ba9

Browse files
feat: improve codes (#922)
1 parent 2d4ab20 commit f4e4ba9

5 files changed

Lines changed: 49 additions & 50 deletions

File tree

src/frontend/src/lib/functions/bytes.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@ export const B_VALS = {
88

99
export type ByteUnit = keyof typeof B_VALS;
1010

11-
export function formatBytes(bytes: number): { val: number; unit: ByteUnit } {
12-
if (!bytes || bytes === 0) return { val: 0, unit: 'MB' };
13-
const units: ByteUnit[] = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
11+
const BYTE_UNITS = Object.keys(B_VALS) as ByteUnit[];
12+
13+
export const formatBytes = (bytes: number): { val: number; unit: ByteUnit } => {
14+
if (!bytes) return { val: 0, unit: 'MB' };
15+
1416
const i = Math.floor(Math.log(bytes) / Math.log(1024));
17+
1518
return {
16-
val: parseFloat((bytes / Math.pow(1024, i)).toFixed(2)),
17-
unit: units[i]
19+
val: +(bytes / 1024 ** i).toFixed(2),
20+
unit: BYTE_UNITS[i]
1821
};
19-
}
22+
};
23+
24+
export const bytesToNumber = (value: number, unit: ByteUnit) => Math.floor(value * B_VALS[unit]);
2025

21-
export function bytesToNumber(value: number, unit: ByteUnit): number {
22-
return Math.floor(value * B_VALS[unit]);
23-
}
2426
export const formatFileSize = (bytes: number): string => {
25-
if (bytes === 0) return '0 Bytes';
26-
const k = 1024;
27-
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
28-
const i = Math.floor(Math.log(bytes) / Math.log(k));
29-
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
27+
if (!bytes) return '0 Bytes';
28+
29+
const i = Math.floor(Math.log(bytes) / Math.log(1024));
30+
31+
return `${+(bytes / 1024 ** i).toFixed(2)} ${BYTE_UNITS[i]}`;
3032
};

src/frontend/src/lib/functions/mime.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,11 @@ const mimeMap: Record<string, string> = {
6969

7070
export function getMimeType(name: string): string {
7171
const ext = name.split('.').at(-1)?.toLowerCase() ?? '';
72-
73-
return mimeMap[ext] || 'application/octet-stream';
72+
return mimeMap[ext] ?? 'application/octet-stream';
7473
}
7574

7675
export function detectMimeFromBytes(bytes: Uint8Array): string | null {
77-
const has = (offset: number, ...header: number[]) =>
76+
const has = (offset: number, ...header: number[]): boolean =>
7877
offset + header.length <= bytes.length &&
7978
header.every((value, index) => bytes[offset + index] === value);
8079

src/frontend/src/routes/og/+server.ts renamed to src/frontend/src/routes/og/(base)/+server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { RequestHandler } from '@sveltejs/kit';
2-
import { buildOgResponse } from './og-response';
3-
import type { OgConfig } from './og-types';
2+
import { buildOgResponse } from '../og-response';
3+
import type { OgConfig } from '../og-types';
44

55
const baseOgConfig: OgConfig = {
66
label: 'Private by design',
Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,28 @@
1-
import type { OgConfig } from './og-types';
1+
import type { OgConfig, OgDisplay, OgInputs } from './og-types';
22

3-
export type OgInputs = {
4-
label?: string | null;
5-
title?: string | null;
6-
description?: string | null;
7-
filename?: string | null;
8-
size?: string | null;
9-
fileCount?: number | null;
10-
};
11-
12-
export type OgDisplay = {
13-
label: string;
14-
title: string;
15-
subtitle: string;
16-
footerTags?: string[];
17-
};
18-
19-
function trimOr(value: string | null | undefined, fallback: string) {
20-
const cleaned = value?.trim();
21-
return cleaned ? cleaned : fallback;
22-
}
3+
const trimOr = (value: string | null | undefined, fallback: string) => value?.trim() || fallback;
234

24-
export function buildOgDisplay(config: OgConfig, inputs: OgInputs): OgDisplay {
5+
export const buildOgDisplay = (config: OgConfig, inputs: OgInputs): OgDisplay => {
256
const label = config.labelFromQuery ? trimOr(inputs.label, config.label) : config.label;
267

27-
const title = config.usesFileMeta
28-
? trimOr(inputs.filename, 'Encrypted File').slice(0, 42)
29-
: trimOr(inputs.title, config.title ?? 'Chithi').slice(0, 42);
8+
const title = (
9+
config.usesFileMeta
10+
? trimOr(inputs.filename, 'Encrypted File')
11+
: trimOr(inputs.title, config.title || 'Chithi')
12+
).slice(0, 42);
13+
14+
const fileCount = Number.isFinite(inputs.fileCount) ? inputs.fileCount : null;
3015

31-
const fileCount =
32-
typeof inputs.fileCount === 'number' && Number.isFinite(inputs.fileCount)
33-
? inputs.fileCount
34-
: null;
3516
const fileCountLabel = fileCount && fileCount > 0 ? ` | Files: ${fileCount}` : '';
17+
3618
const subtitle = config.usesFileMeta
3719
? `Size: ${trimOr(inputs.size, 'Unknown size')}${fileCountLabel}`
38-
: trimOr(inputs.description, config.description ?? '').slice(0, 90);
20+
: trimOr(inputs.description, config.description || '').slice(0, 90);
3921

4022
return {
4123
label,
4224
title,
4325
subtitle,
4426
footerTags: config.footerTags
4527
};
46-
}
28+
};

src/frontend/src/routes/og/og-types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,19 @@ export type OgConfig = {
66
labelFromQuery?: boolean;
77
usesFileMeta?: boolean;
88
};
9+
10+
export type OgInputs = {
11+
label?: string | null;
12+
title?: string | null;
13+
description?: string | null;
14+
filename?: string | null;
15+
size?: string | null;
16+
fileCount?: number | null;
17+
};
18+
19+
export type OgDisplay = {
20+
label: string;
21+
title: string;
22+
subtitle: string;
23+
footerTags?: string[];
24+
};

0 commit comments

Comments
 (0)