aboutsummaryrefslogtreecommitdiff
path: root/src/client/apis/google_docs/GooglePhotosClientUtils.ts
blob: bb5d23971ec709e18568068022199d3c4127b5f6 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { PostToServer, Utils } from "../../../Utils";
import { RouteStore } from "../../../server/RouteStore";
import { ImageField } from "../../../new_fields/URLField";
import { StrCast, Cast } from "../../../new_fields/Types";
import { Doc, Opt } from "../../../new_fields/Doc";
import { Id } from "../../../new_fields/FieldSymbols";
import requestImageSize = require('../../util/request-image-size');
import Photos = require('googlephotos');
import { RichTextField } from "../../../new_fields/RichTextField";
import { RichTextUtils } from "../../../new_fields/RichTextUtils";
import { EditorState } from "prosemirror-state";
import { FormattedTextBox } from "../../views/nodes/FormattedTextBox";
import { Docs } from "../../documents/Documents";

export namespace GooglePhotosClientUtils {

    export enum ContentCategories {
        NONE = 'NONE',
        LANDSCAPES = 'LANDSCAPES',
        RECEIPTS = 'RECEIPTS',
        CITYSCAPES = 'CITYSCAPES',
        LANDMARKS = 'LANDMARKS',
        SELFIES = 'SELFIES',
        PEOPLE = 'PEOPLE',
        PETS = 'PETS',
        WEDDINGS = 'WEDDINGS',
        BIRTHDAYS = 'BIRTHDAYS',
        DOCUMENTS = 'DOCUMENTS',
        TRAVEL = 'TRAVEL',
        ANIMALS = 'ANIMALS',
        FOOD = 'FOOD',
        SPORT = 'SPORT',
        NIGHT = 'NIGHT',
        PERFORMANCES = 'PERFORMANCES',
        WHITEBOARDS = 'WHITEBOARDS',
        SCREENSHOTS = 'SCREENSHOTS',
        UTILITY = 'UTILITY'
    }

    export enum MediaType {
        ALL_MEDIA = 'ALL_MEDIA',
        PHOTO = 'PHOTO',
        VIDEO = 'VIDEO'
    }

    export type AlbumReference = { id: string } | { title: string };
    export const endpoint = () => fetch(Utils.prepend(RouteStore.googlePhotosAccessToken)).then(async response => new Photos(await response.text()));

    export interface MediaInput {
        url: string;
        description: string;
    }

    export const UploadImageDocuments = async (sources: Doc[], album?: AlbumReference, descriptionKey = "caption") => {
        if (album && "title" in album) {
            album = await (await endpoint()).albums.create(album.title);
        }
        const media: MediaInput[] = [];
        sources.forEach(document => {
            const data = Cast(Doc.GetProto(document).data, ImageField);
            data && media.push({
                url: data.url.href,
                description: parseDescription(document, descriptionKey),
            });
        });
        if (media.length) {
            return PostToServer(RouteStore.googlePhotosMediaUpload, { media, album });
        }
    };

    const parseDescription = (document: Doc, descriptionKey: string) => {
        let description: string = Utils.prepend("/doc/" + document[Id]);
        const target = document[descriptionKey];
        if (typeof target === "string") {
            description = target;
        } else if (target instanceof RichTextField) {
            description = RichTextUtils.ToPlainText(EditorState.fromJSON(FormattedTextBox.Instance.config, JSON.parse(target.Data)));
        }
        return description;
    };

    export interface DateRange {
        after: Date;
        before: Date;
    }
    export interface SearchOptions {
        pageSize: number;
        included: ContentCategories[];
        excluded: ContentCategories[];
        date: Opt<Date | DateRange>;
        includeArchivedMedia: boolean;
        type: MediaType;
    }

    const DefaultSearchOptions: SearchOptions = {
        pageSize: 20,
        included: [],
        excluded: [],
        date: undefined,
        includeArchivedMedia: true,
        type: MediaType.ALL_MEDIA
    };

    export interface SearchResponse {
        mediaItems: any[];
        nextPageToken: string;
    }

    export const Search = async (requested: Opt<Partial<SearchOptions>>) => {
        const options = requested || DefaultSearchOptions;
        const photos = await endpoint();
        const filters = new photos.Filters(options.includeArchivedMedia === undefined ? true : options.includeArchivedMedia);

        const included = options.included || [];
        const excluded = options.excluded || [];
        const contentFilter = new photos.ContentFilter();
        included.length && included.forEach(category => contentFilter.addIncludedContentCategories(category));
        excluded.length && excluded.forEach(category => contentFilter.addExcludedContentCategories(category));
        filters.setContentFilter(contentFilter);

        const date = options.date;
        if (date) {
            const dateFilter = new photos.DateFilter();
            if (date instanceof Date) {
                dateFilter.addDate(date);
            } else {
                dateFilter.addRange(date.after, date.before);
            }
            filters.setDateFilter(dateFilter);
        }

        filters.setMediaTypeFilter(new photos.MediaTypeFilter(options.type || MediaType.ALL_MEDIA));

        return new Promise<Doc>(resolve => {
            photos.mediaItems.search(filters, options.pageSize || 20).then(async (response: SearchResponse) => {
                response && resolve(Docs.Create.StackingDocument((await PostToServer(RouteStore.googlePhotosMediaDownload, response)).map((download: any) => {
                    let document = Docs.Create.ImageDocument(Utils.prepend(`/files/${download.fileName}`));
                    document.contentSize = download.contentSize;
                    return document;
                }), { width: 500, height: 500 }));
            });
        });
    };

}