aboutsummaryrefslogtreecommitdiff
path: root/src/client/apis/google_docs/GooglePhotosClientUtils.ts
blob: 1e4c120bc1b8d2697ac618e79ac87012b9eced56 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import { AssertionError } from "assert";
import { EditorState } from "prosemirror-state";
import { Doc, DocListCastAsync, Opt } from "../../../new_fields/Doc";
import { Id } from "../../../new_fields/FieldSymbols";
import { RichTextField } from "../../../new_fields/RichTextField";
import { RichTextUtils } from "../../../new_fields/RichTextUtils";
import { Cast, StrCast } from "../../../new_fields/Types";
import { ImageField } from "../../../new_fields/URLField";
import { MediaItem, NewMediaItemResult } from "../../../server/apis/google/SharedTypes";
import { Utils } from "../../../Utils";
import { Docs, DocumentOptions } from "../../documents/Documents";
import { Networking } from "../../Network";
import { FormattedTextBox } from "../../views/nodes/formattedText/FormattedTextBox";
import GoogleAuthenticationManager from "../GoogleAuthenticationManager";
import Photos = require('googlephotos');

export namespace GooglePhotos {

    const endpoint = async () => new Photos(await GoogleAuthenticationManager.Instance.fetchOrGenerateAccessToken());

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

    export type AlbumReference = { id: string } | { title: string };

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

    export const 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',
        ARTS: 'ARTS',
        CRAFTS: 'CRAFTS',
        FASHION: 'FASHION',
        HOUSES: 'HOUSES',
        GARDENS: 'GARDENS',
        FLOWERS: 'FLOWERS',
        HOLIDAYS: 'HOLIDAYS'
    };

    export namespace Export {

        export interface AlbumCreationResult {
            albumId: string;
            mediaItems: MediaItem[];
        }

        export interface AlbumCreationOptions {
            collection: Doc;
            title?: string;
            descriptionKey?: string;
            tag?: boolean;
        }

        export const CollectionToAlbum = async (options: AlbumCreationOptions): Promise<Opt<AlbumCreationResult>> => {
            const { collection, title, descriptionKey, tag } = options;
            const dataDocument = Doc.GetProto(collection);
            const images = ((await DocListCastAsync(dataDocument.data)) || []).filter(doc => Cast(doc.data, ImageField));
            if (!images || !images.length) {
                return undefined;
            }
            const resolved = title ? title : (StrCast(collection.title) || `Dash Collection (${collection[Id]}`);
            const { id, productUrl } = await Create.Album(resolved);
            const response = await Transactions.UploadImages(images, { id }, descriptionKey);
            if (response) {
                const { results, failed } = response;
                let index: Opt<number>;
                while ((index = failed.pop()) !== undefined) {
                    Doc.RemoveDocFromList(dataDocument, "data", images.splice(index, 1)[0]);
                }
                const mediaItems: MediaItem[] = results.map(item => item.mediaItem);
                if (mediaItems.length !== images.length) {
                    throw new AssertionError({ actual: mediaItems.length, expected: images.length });
                }
                const idMapping = new Doc;
                for (let i = 0; i < images.length; i++) {
                    const image = Doc.GetProto(images[i]);
                    const mediaItem = mediaItems[i];
                    if (!mediaItem) {
                        continue;
                    }
                    image.googlePhotosId = mediaItem.id;
                    image.googlePhotosAlbumUrl = productUrl;
                    image.googlePhotosUrl = mediaItem.productUrl || mediaItem.baseUrl;
                    idMapping[mediaItem.id] = image;
                }
                collection.googlePhotosAlbumUrl = productUrl;
                collection.googlePhotosIdMapping = idMapping;
                if (tag) {
                    await Query.TagChildImages(collection);
                }
                collection.albumId = id;
                Transactions.AddTextEnrichment(collection, `Find me at ${Utils.prepend(`/doc/${collection[Id]}?sharing=true`)}`);
                return { albumId: id, mediaItems };
            }
        };

    }

    export namespace Import {

        export type CollectionConstructor = (data: Array<Doc>, options: DocumentOptions, ...args: any) => Doc;

        export const CollectionFromSearch = async (constructor: CollectionConstructor, requested: Opt<Partial<Query.SearchOptions>>): Promise<Doc> => {
            await GoogleAuthenticationManager.Instance.fetchOrGenerateAccessToken();
            const response = await Query.ContentSearch(requested);
            const uploads = await Transactions.WriteMediaItemsToServer(response);
            const children = uploads.map((upload: Transactions.UploadInformation) => {
                const document = Docs.Create.ImageDocument(Utils.fileUrl(upload.fileNames.clean));
                document.fillColumn = true;
                document.contentSize = upload.contentSize;
                return document;
            });
            const options = { _width: 500, _height: 500 };
            return constructor(children, options);
        };

    }

    export namespace Query {

        const delimiter = ", ";
        const comparator = (a: string, b: string) => (a < b) ? -1 : (a > b ? 1 : 0);

        export const TagChildImages = async (collection: Doc) => {
            await GoogleAuthenticationManager.Instance.fetchOrGenerateAccessToken();
            const idMapping = await Cast(collection.googlePhotosIdMapping, Doc);
            if (!idMapping) {
                throw new Error("Appending image metadata requires that the targeted collection have already been mapped to an album!");
            }
            const tagMapping = new Map<string, string>();
            const images = (await DocListCastAsync(collection.data))!.map(Doc.GetProto);
            images?.forEach(image => tagMapping.set(image[Id], ContentCategories.NONE));
            const values = Object.values(ContentCategories).filter(value => value !== ContentCategories.NONE);
            for (const value of values) {
                const searched = (await ContentSearch({ included: [value] }))?.mediaItems?.map(({ id }) => id);
                console.log("Searching " + value);
                console.log(searched);
                searched?.forEach(async id => {
                    const image = await Cast(idMapping[id], Doc);
                    if (image) {
                        const key = image[Id];
                        const tags = tagMapping.get(key);
                        !tags?.includes(value) && tagMapping.set(key, tags + delimiter + value);
                    }
                });
            }
            images?.forEach(image => {
                const concatenated = tagMapping.get(image[Id])!;
                const tags = concatenated.split(delimiter);
                if (tags.length > 1) {
                    const cleaned = concatenated.replace(ContentCategories.NONE + delimiter, "");
                    image.googlePhotosTags = cleaned.split(delimiter).sort(comparator).join(delimiter);
                } else {
                    image.googlePhotosTags = ContentCategories.NONE;
                }
            });
        };

        interface DateRange {
            after: Date;
            before: Date;
        }

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

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

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

        export const AlbumSearch = async (albumId: string, pageSize = 100): Promise<MediaItem[]> => {
            const photos = await endpoint();
            const mediaItems: MediaItem[] = [];
            let nextPageTokenStored: Opt<string> = undefined;
            const found = 0;
            do {
                const response: any = await photos.mediaItems.search(albumId, pageSize, nextPageTokenStored);
                mediaItems.push(...response.mediaItems);
                nextPageTokenStored = response.nextPageToken;
            } while (found);
            return mediaItems;
        };

        export const ContentSearch = async (requested: Opt<Partial<SearchOptions>>): Promise<SearchResponse> => {
            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<SearchResponse>(resolve => {
                photos.mediaItems.search(filters, options.pageSize || 100).then(resolve);
            });
        };

        export const GetImage = async (mediaItemId: string): Promise<Transactions.MediaItem> => {
            return (await endpoint()).mediaItems.get(mediaItemId);
        };

    }

    namespace Create {

        export const Album = async (title: string) => {
            return (await endpoint()).albums.create(title);
        };

    }

    export namespace Transactions {

        export interface UploadInformation {
            mediaPaths: string[];
            fileNames: { [key: string]: string };
            contentSize?: number;
            contentType?: string;
        }

        export interface MediaItem {
            id: string;
            filename: string;
            baseUrl: string;
        }

        export const ListAlbums = async () => (await endpoint()).albums.list();

        export const AddTextEnrichment = async (collection: Doc, content?: string) => {
            const photos = await endpoint();
            const albumId = StrCast(collection.albumId);
            if (albumId && albumId.length) {
                const enrichment = new photos.TextEnrichment(content || Utils.prepend("/doc/" + collection[Id]));
                const position = new photos.AlbumPosition(photos.AlbumPosition.POSITIONS.FIRST_IN_ALBUM);
                const enrichmentItem = await photos.albums.addEnrichment(albumId, enrichment, position);
                if (enrichmentItem) {
                    return enrichmentItem.id;
                }
            }
        };

        export const WriteMediaItemsToServer = async (body: { mediaItems: any[] }): Promise<UploadInformation[]> => {
            const uploads = await Networking.PostToServer("/googlePhotosMediaGet", body);
            return uploads;
        };

        export const UploadThenFetch = async (sources: Doc[], album?: AlbumReference, descriptionKey = "caption") => {
            const response = await UploadImages(sources, album, descriptionKey);
            if (!response) {
                return undefined;
            }
            const baseUrls: string[] = await Promise.all(response.results.map(item => {
                return new Promise<string>(resolve => Query.GetImage(item.mediaItem.id).then(item => resolve(item.baseUrl)));
            }));
            return baseUrls;
        };

        export interface ImageUploadResults {
            results: NewMediaItemResult[];
            failed: number[];
        }

        export const UploadImages = async (sources: Doc[], album?: AlbumReference, descriptionKey = "caption"): Promise<Opt<ImageUploadResults>> => {
            await GoogleAuthenticationManager.Instance.fetchOrGenerateAccessToken();
            if (album && "title" in album) {
                album = await Create.Album(album.title);
            }
            const media: MediaInput[] = [];
            for (const source of sources) {
                const data = Cast(Doc.GetProto(source).data, ImageField);
                if (!data) {
                    return;
                }
                const url = data.url.href;
                const target = Doc.MakeAlias(source);
                const description = parseDescription(target, descriptionKey);
                await Doc.makeCustomViewClicked(target, Docs.Create.FreeformDocument);
                media.push({ url, description });
            }
            if (media.length) {
                const results = await Networking.PostToServer("/googlePhotosMediaPost", { media, album });
                return results;
            }
        };

        const parseDescription = (document: Doc, descriptionKey: string) => {
            let description: string = Utils.prepend(`/doc/${document[Id]}?sharing=true`);
            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;
        };

    }

}