aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/Import & Export/ImageUtils.ts
blob: 8d4eefa7e6da86ebe39a1929a439e5913c4fbe48 (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
35
36
37
38
39
40
41
42
import { ClientUtils } from '../../../ClientUtils';
import { Doc } from '../../../fields/Doc';
import { DocData } from '../../../fields/DocSymbols';
import { Id } from '../../../fields/FieldSymbols';
import { Cast, NumCast, StrCast } from '../../../fields/Types';
import { ImageField } from '../../../fields/URLField';
import { Networking } from '../../Network';

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<imgInfo | undefined> => {
        const field = Cast(document.data, ImageField);
        return field ? 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<void> => {
        const a = document.createElement('a');
        a.href = ClientUtils.prepend(`/imageHierarchyExport/${collection[Id]}`);
        a.download = `Dash Export [${StrCast(collection.title)}].zip`;
        a.click();
    };
}