import { Doc } from '../../../fields/Doc'; import { ImageField } from '../../../fields/URLField'; import { Cast, StrCast, NumCast } from '../../../fields/Types'; import { Networking } from '../../Network'; import { Id } from '../../../fields/FieldSymbols'; import { Utils } from '../../../Utils'; import { DocData } from '../../../fields/DocSymbols'; export namespace ImageUtils { export type imgInfo = { contentSize: number; nativeWidth: number; nativeHeight: number; source: string; exifData: { error: string | undefined; data: string }; }; export const ExtractImgInfo = async (document: Doc): Promise => { const field = Cast(document.data, ImageField); return field ? await Networking.PostToServer('/inspectImage', { source: field.url.href }) : undefined; }; export const AssignImgInfo = (document: Doc, data?: imgInfo) => { if (data) { data.nativeWidth && (document._height = (NumCast(document._width) * data.nativeHeight) / data.nativeWidth); const proto = document[DocData]; const field = Doc.LayoutFieldKey(document); proto[`${field}_nativeWidth`] = data.nativeWidth; proto[`${field}_nativeHeight`] = data.nativeHeight; proto[`${field}_path`] = data.source; proto[`${field}_exif`] = JSON.stringify(data.exifData.data); proto[`${field}_contentSize`] = data.contentSize ? data.contentSize : undefined; } return document; }; export const ExportHierarchyToFileSystem = async (collection: Doc): Promise => { const a = document.createElement('a'); a.href = Utils.prepend(`/imageHierarchyExport/${collection[Id]}`); a.download = `Dash Export [${StrCast(collection.title)}].zip`; a.click(); }; }