punimtag/viewer-frontend/lib/photo-utils.ts
Tanya b6a9765315
Some checks failed
CI / skip-ci-check (push) Successful in 1m27s
CI / skip-ci-check (pull_request) Successful in 1m27s
CI / lint-and-type-check (pull_request) Has been cancelled
CI / python-lint (pull_request) Has been cancelled
CI / test-backend (pull_request) Has been cancelled
CI / build (pull_request) Has been cancelled
CI / secret-scanning (pull_request) Has been cancelled
CI / dependency-scan (pull_request) Has been cancelled
CI / sast-scan (pull_request) Has been cancelled
CI / workflow-summary (pull_request) Has been cancelled
CI / lint-and-type-check (push) Successful in 2m4s
CI / python-lint (push) Successful in 1m53s
CI / test-backend (push) Successful in 2m37s
CI / build (push) Failing after 2m13s
CI / secret-scanning (push) Successful in 1m40s
CI / dependency-scan (push) Successful in 1m34s
CI / sast-scan (push) Successful in 2m42s
CI / workflow-summary (push) Successful in 1m26s
chore: Update project configuration and enhance code quality
This commit modifies the `.gitignore` file to exclude Python library directories while ensuring the viewer-frontend's `lib` directory is not ignored. It also updates the `package.json` to activate the virtual environment during backend tests, improving the testing process. Additionally, the CI workflow is enhanced to prevent duplicate runs for branches with open pull requests. Various components in the viewer frontend are updated to ensure consistent naming conventions and improve type safety. These changes contribute to a cleaner codebase and a more efficient development workflow.
2026-01-07 12:29:17 -05:00

60 lines
1.8 KiB
TypeScript

import { Photo } from '@prisma/client';
/**
* Determines if a path is a URL (http/https) or a file system path
*/
export function isUrl(path: string): boolean {
return path.startsWith('http://') || path.startsWith('https://');
}
/**
* Check if photo is a video
*/
export function isVideo(photo: Photo): boolean {
// Handle both camelCase (Prisma client) and snake_case (direct DB access)
return (photo as any).mediaType === 'video' || (photo as any).media_type === 'video';
}
/**
* Gets the appropriate image source URL
* - URLs (SharePoint, CDN, etc.) → use directly
* - File system paths → use API proxy
* - Videos → use thumbnail endpoint (for grid display)
*/
export function getImageSrc(photo: Photo, options?: { watermark?: boolean; thumbnail?: boolean }): string {
// For videos, use thumbnail endpoint if requested (for grid display)
if (options?.thumbnail && isVideo(photo)) {
return `/api/photos/${photo.id}/image?thumbnail=true`;
}
if (isUrl(photo.path)) {
if (typeof window !== 'undefined' && process.env.NODE_ENV === 'development') {
console.log(`✅ Photo ${photo.id}: Using DIRECT access for URL:`, photo.path);
}
return photo.path;
}
const params = new URLSearchParams();
if (options?.watermark) {
params.set('watermark', 'true');
}
const query = params.toString();
if (typeof window !== 'undefined' && process.env.NODE_ENV === 'development') {
console.log(`📁 Photo ${photo.id}: Using API PROXY for file path:`, photo.path);
}
return `/api/photos/${photo.id}/image${query ? `?${query}` : ''}`;
}
/**
* Gets the appropriate video source URL
*/
export function getVideoSrc(photo: Photo): string {
if (isUrl(photo.path)) {
return photo.path;
}
return `/api/photos/${photo.id}/image`;
}