aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/Import & Export/ImageUtils.ts
blob: 9c32ca25a4a96c7cdbe5380771ef2ee93c8dfe67 (plain)
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
31
32
33
34
import { ClientUtils } from '../../../ClientUtils';
import { Doc } from '../../../fields/Doc';
import { Id } from '../../../fields/FieldSymbols';
import { Cast, NumCast, StrCast } from '../../../fields/Types';
import { ImageField } from '../../../fields/URLField';
import { Upload } from '../../../server/SharedMediaTypes';
import { Networking } from '../../Network';

export namespace ImageUtils {
    export const ExtractImgInfo = async (document: Doc) => {
        const field = Cast(document.data, ImageField);
        return field ? (Networking.PostToServer('/inspectImage', { source: field.url.href }) as Promise<Upload.InspectionResults>) : undefined;
    };

    export const AssignImgInfo = (document: Doc, data?: Upload.InspectionResults) => {
        if (data) {
            data.nativeWidth && (document._height = (NumCast(document._width) * data.nativeHeight) / data.nativeWidth);
            const field = '$' + Doc.LayoutFieldKey(document);
            document[`${field}_nativeWidth`] = data.nativeWidth;
            document[`${field}_nativeHeight`] = data.nativeHeight;
            document[`${field}_path`] = data.source;
            document[`${field}_exif`] = JSON.stringify(data.exifData.data);
            document[`${field}_contentSize`] = data.contentSize ? data.contentSize : undefined;
        }
        return document;
    };

    export const ExportHierarchyToFileSystem = async (collection: Doc): Promise<void> => {
        const a = document.createElement('a');
        a.href = ClientUtils.prepend(`/imageHierarchyExport/${collection[Id]}`);
        a.download = `Dash Export [${StrCast(collection.title)}].zip`;
        a.click();
    };
}