Skip to content

Commit 3cd1b51

Browse files
authored
emoji-safe encoding? (#22)
1 parent 529a674 commit 3cd1b51

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

app/api/bucket/[bucket]/[key]/route.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getBucket } from '@/utils/cf';
22
import { getSettingsRecord } from '@/utils/db/queries';
3+
import { decode } from '@/utils/encoding';
34

45
export const runtime = 'edge';
56

@@ -14,7 +15,7 @@ export const GET = async (
1415
return new Response('Unable to read bucket', { status: 400 });
1516
}
1617

17-
const path = atob(key);
18+
const path = decode(key);
1819

1920
const [, start, end] = /^bytes=(\d+)-(\d+)?$/.exec(req.headers.get('range') ?? '') ?? [];
2021

@@ -73,7 +74,7 @@ export const POST = async (
7374
return new Response('Unable to read bucket', { status: 400 });
7475
}
7576

76-
const path = atob(key);
77+
const path = decode(key);
7778

7879
const object = await bucket.head(path);
7980

components/object-explorer/object-preview-inner.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { addLeadingSlash } from '@/utils';
22
import type { FileType } from '@/utils';
33
import { memo } from 'react';
44
import { twMerge } from 'tailwind-merge';
5+
import { encode } from '@/utils/encoding';
56
import { useLocation } from '../providers';
67
import { getFileIcon } from './file-icons';
78

@@ -21,7 +22,7 @@ export const ObjectPreviewInner = memo(
2122
const { currentBucket } = useLocation();
2223
if (!currentBucket || !path || !itemType) return null;
2324

24-
const itemApiSrc = `/api/bucket/${currentBucket?.raw}${addLeadingSlash(btoa(path))}`;
25+
const itemApiSrc = `/api/bucket/${currentBucket?.raw}${addLeadingSlash(encode(path))}`;
2526

2627
switch (itemType) {
2728
case 'image': {

components/object-explorer/object-preview.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
MediaTimeRange,
1515
MediaVolumeRange,
1616
} from 'media-chrome/react';
17+
import { encode } from '@/utils/encoding';
1718
import { useLocation, useObjectExplorer } from '../providers';
1819

1920
export const ObjectPreview = (): JSX.Element => {
@@ -31,7 +32,7 @@ export const ObjectPreview = (): JSX.Element => {
3132
const [objectStr, setObjectStr] = useState<string | null>(null);
3233

3334
const rawPreviewKey = selectedObjects.keys().next().value;
34-
const previewKey = rawPreviewKey ? btoa(rawPreviewKey) : undefined;
35+
const previewKey = rawPreviewKey ? encode(rawPreviewKey) : undefined;
3536

3637
useEffect(() => {
3738
if (!isPreviewActive || !currentBucket) return;

utils/encoding.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const encode = (v: string) => btoa(encodeURIComponent(v));
2+
3+
export const decode = (v: string) => atob(decodeURIComponent(v));

0 commit comments

Comments
 (0)