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
|
import request = require('request-promise');
import { GoogleApiServerUtils } from './GoogleApiServerUtils';
import * as path from 'path';
import { MediaItemCreationResult } from './SharedTypes';
import { NewMediaItem } from "../../index";
import { BatchedArray, TimeUnit } from 'array-batcher';
import { DashUploadUtils } from '../../DashUploadUtils';
export namespace GooglePhotosUploadUtils {
export interface Paths {
uploadDirectory: string;
credentialsPath: string;
tokenPath: string;
}
export interface MediaInput {
url: string;
description: string;
}
const prepend = (extension: string) => `https://photoslibrary.googleapis.com/v1/${extension}`;
const headers = (type: string) => ({
'Content-Type': `application/${type}`,
'Authorization': Bearer,
});
let Bearer: string;
export const initialize = async (information: GoogleApiServerUtils.CredentialInformation) => {
const token = await GoogleApiServerUtils.RetrieveAccessToken(information);
Bearer = `Bearer ${token}`;
};
export const DispatchGooglePhotosUpload = async (url: string) => {
if (!DashUploadUtils.imageFormats.includes(path.extname(url))) {
return undefined;
}
const body = await request(url, { encoding: null });
const parameters = {
method: 'POST',
headers: {
...headers('octet-stream'),
'X-Goog-Upload-File-Name': path.basename(url),
'X-Goog-Upload-Protocol': 'raw'
},
uri: prepend('uploads'),
body
};
return new Promise<any>((resolve, reject) => request(parameters, (error, _response, body) => {
if (error) {
console.log(error);
return reject(error);
}
resolve(body);
}));
};
export const CreateMediaItems = async (newMediaItems: NewMediaItem[], album?: { id: string }): Promise<MediaItemCreationResult> => {
const newMediaItemResults = await BatchedArray.from(newMediaItems, { batchSize: 50 }).batchedMapPatientInterval(
{ magnitude: 100, unit: TimeUnit.Milliseconds },
async (batch: NewMediaItem[]) => {
const parameters = {
method: 'POST',
headers: headers('json'),
uri: prepend('mediaItems:batchCreate'),
body: { newMediaItems: batch } as any,
json: true
};
album && (parameters.body.albumId = album.id);
return (await new Promise<MediaItemCreationResult>((resolve, reject) => {
request(parameters, (error, _response, body) => {
if (error) {
reject(error);
} else {
resolve(body);
}
});
})).newMediaItemResults;
}
);
return { newMediaItemResults };
};
}
|