diff --git a/server/src/nest/memories/memories.helpers.ts b/server/src/nest/memories/memories.helpers.ts new file mode 100644 index 000000000..2b5ce00c4 --- /dev/null +++ b/server/src/nest/memories/memories.helpers.ts @@ -0,0 +1,161 @@ +import { pipeline } from 'node:stream/promises'; +import { Readable } from 'node:stream'; +import { Response } from 'express'; +import { safeFetch, SsrfBlockedError, type SafeFetchOptions } from '../../utils/ssrfGuard'; + +/** + * The shared vocabulary of the memories domain: the ServiceResult envelope, the + * provider-agnostic asset shapes, and the asset proxy. + * + * No database and no DI, so the provider services, the resolver and the + * controllers can all name these without importing each other — the same reason + * notifications/notification-events.ts exists. + */ + +// helpers for handling return types + +type ServiceError = { success: false; error: { message: string; status: number } }; +export type ServiceResult = { success: true; data: T } | ServiceError; + + +export function fail(error: string, status: number): ServiceError { + return { success: false, error: { message: error, status } }; +} + + +export function success(data: T): ServiceResult { + return { success: true, data: data }; +} + + +export function mapDbError(error: Error, fallbackMessage: string): ServiceError { + if (error && /unique|constraint/i.test(error.message)) { + return fail('Resource already exists', 409); + } + return fail(error.message, 500); +} + + +export function handleServiceResult(res: Response, result: ServiceResult): void { + if ('error' in result) { + res.status(result.error.status).json({ error: result.error.message }); + } + else { + res.json(result.data); + } +} + +// ---------------------------------------------- +// types used across memories services +export type Selection = { + provider: string; + asset_ids: string[]; + passphrase?: string; +}; + +export type StatusResult = { + connected: true; + user: { name: string } +} | { + connected: false; + error: string +}; + +export type SyncAlbumResult = { + added: number; + total: number +}; + + +export type AlbumsList = { + albums: Array<{ id: string; albumName: string; assetCount: number; passphrase?: string }> +}; + +export type Asset = { + id: string; + takenAt: string; + mediaType?: string; + city?: string | null; + country?: string | null; + lat?: number | null; + lng?: number | null; +}; + +export type AssetsList = { + assets: Asset[], + total: number, + hasMore: boolean +}; + + +export type AssetInfo = { + id: string; + takenAt: string | null; + city: string | null; + country: string | null; + state?: string | null; + camera?: string | null; + lens?: string | null; + focalLength?: string | number | null; + aperture?: string | number | null; + shutter?: string | number | null; + iso?: string | number | null; + lat?: number | null; + lng?: number | null; + orientation?: number | null; + description?: string | null; + width?: number | null; + height?: number | null; + fileSize?: number | null; + fileName?: string | null; +} + +/** + * Proxy an upstream asset straight to the client. + * + * It writes status, headers and body onto the Express response itself and is + * NOT a candidate for StreamableFile: it forwards the upstream's 206 and + * Content-Range so a