-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlightboxSlideDownload.ts
More file actions
30 lines (27 loc) · 1.04 KB
/
Copy pathlightboxSlideDownload.ts
File metadata and controls
30 lines (27 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Derive download URL + suggested basename from a typed lightbox slide.
* Keeps UI layers thin — same rules as the app overflow menu.
*/
import type { LightboxSlide } from './lightbox';
import { sanitizeDownloadFilename } from './browserDownload';
export function lightboxSlideDownloadUrl(slide: LightboxSlide | undefined): string | null {
if (!slide) return null;
if (slide.kind === 'image' || slide.kind === 'video') return slide.src;
return null;
}
export function lightboxSlideDownloadBasename(slide: LightboxSlide | undefined): string {
if (!slide) return 'download';
const alt = typeof slide.alt === 'string' ? slide.alt.trim() : '';
if (alt) return sanitizeDownloadFilename(alt);
const url = lightboxSlideDownloadUrl(slide);
if (url && typeof window !== 'undefined') {
try {
const u = new URL(url, window.location.href);
const base = u.pathname.split('/').pop() || 'download';
return sanitizeDownloadFilename(decodeURIComponent(base));
} catch {
return 'download';
}
}
return 'download';
}