aboutsummaryrefslogtreecommitdiff
path: root/src/server/apis
diff options
context:
space:
mode:
authorSam Wilkins <samwilkins333@gmail.com>2019-09-26 13:43:55 -0400
committerSam Wilkins <samwilkins333@gmail.com>2019-09-26 13:43:55 -0400
commit204cf12ced54581b97fff1ba11be429be88b6ade (patch)
treee6d6ecff5db3692ef1f492c6c1866b0f34c5c156 /src/server/apis
parent8cbc191b560684f4da32ca0320115974cad41808 (diff)
parent9b27f1ace4655f71a67ad68e1f6f6bba82f41e46 (diff)
Merge branch 'googlephotos_sharing' of https://github.com/browngraphicslab/Dash-Web into googlephotos_sharing_master
Diffstat (limited to 'src/server/apis')
-rw-r--r--src/server/apis/google/CustomizedWrapper/filters.js46
-rw-r--r--src/server/apis/google/GoogleApiServerUtils.ts32
-rw-r--r--src/server/apis/google/GooglePhotosUploadUtils.ts147
-rw-r--r--src/server/apis/google/existing_uploads.json0
4 files changed, 18 insertions, 207 deletions
diff --git a/src/server/apis/google/CustomizedWrapper/filters.js b/src/server/apis/google/CustomizedWrapper/filters.js
deleted file mode 100644
index 576a90b75..000000000
--- a/src/server/apis/google/CustomizedWrapper/filters.js
+++ /dev/null
@@ -1,46 +0,0 @@
-'use strict';
-
-const DateFilter = require('../common/date_filter');
-const MediaTypeFilter = require('./media_type_filter');
-const ContentFilter = require('./content_filter');
-
-class Filters {
- constructor(includeArchivedMedia = false) {
- this.includeArchivedMedia = includeArchivedMedia;
- }
-
- setDateFilter(dateFilter) {
- this.dateFilter = dateFilter;
- return this;
- }
-
- setContentFilter(contentFilter) {
- this.contentFilter = contentFilter;
- return this;
- }
-
- setMediaTypeFilter(mediaTypeFilter) {
- this.mediaTypeFilter = mediaTypeFilter;
- return this;
- }
-
- setIncludeArchivedMedia(includeArchivedMedia) {
- this.includeArchivedMedia = includeArchivedMedia;
- return this;
- }
-
- toJSON() {
- return {
- dateFilter: this.dateFilter instanceof DateFilter ? this.dateFilter.toJSON() : this.dateFilter,
- mediaTypeFilter: this.mediaTypeFilter instanceof MediaTypeFilter ?
- this.mediaTypeFilter.toJSON() :
- this.mediaTypeFilter,
- contentFilter: this.contentFilter instanceof ContentFilter ?
- this.contentFilter.toJSON() :
- this.contentFilter,
- includeArchivedMedia: this.includeArchivedMedia
- };
- }
-}
-
-module.exports = Filters; \ No newline at end of file
diff --git a/src/server/apis/google/GoogleApiServerUtils.ts b/src/server/apis/google/GoogleApiServerUtils.ts
index e0bd8a800..684a8081b 100644
--- a/src/server/apis/google/GoogleApiServerUtils.ts
+++ b/src/server/apis/google/GoogleApiServerUtils.ts
@@ -8,7 +8,7 @@ import { GaxiosResponse } from "gaxios";
import request = require('request-promise');
import * as qs from 'query-string';
import Photos = require('googlephotos');
-
+import { Database } from "../../database";
/**
* Server side authentication for Google Api queries.
*/
@@ -35,9 +35,9 @@ export namespace GoogleApiServerUtils {
Slides = "Slides"
}
- export interface CredentialPaths {
+ export interface CredentialInformation {
credentialsPath: string;
- tokenPath: string;
+ userId: string;
}
export type ApiResponse = Promise<GaxiosResponse>;
@@ -48,7 +48,7 @@ export namespace GoogleApiServerUtils {
export type Endpoint = { get: ApiHandler, create: ApiHandler, batchUpdate: ApiHandler };
export type EndpointParameters = GlobalOptions & { version: "v1" };
- export const GetEndpoint = (sector: string, paths: CredentialPaths) => {
+ export const GetEndpoint = (sector: string, paths: CredentialInformation) => {
return new Promise<Opt<Endpoint>>(resolve => {
RetrieveCredentials(paths).then(authentication => {
let routed: Opt<Endpoint>;
@@ -66,28 +66,28 @@ export namespace GoogleApiServerUtils {
});
};
- export const RetrieveCredentials = (paths: CredentialPaths) => {
+ export const RetrieveCredentials = (information: CredentialInformation) => {
return new Promise<TokenResult>((resolve, reject) => {
- readFile(paths.credentialsPath, async (err, credentials) => {
+ readFile(information.credentialsPath, async (err, credentials) => {
if (err) {
reject(err);
return console.log('Error loading client secret file:', err);
}
- authorize(parseBuffer(credentials), paths.tokenPath).then(resolve, reject);
+ authorize(parseBuffer(credentials), information.userId).then(resolve, reject);
});
});
};
- export const RetrieveAccessToken = (paths: CredentialPaths) => {
+ export const RetrieveAccessToken = (information: CredentialInformation) => {
return new Promise<string>((resolve, reject) => {
- RetrieveCredentials(paths).then(
+ RetrieveCredentials(information).then(
credentials => resolve(credentials.token.access_token!),
error => reject(`Error: unable to authenticate Google Photos API request.\n${error}`)
);
});
};
- export const RetrievePhotosEndpoint = (paths: CredentialPaths) => {
+ export const RetrievePhotosEndpoint = (paths: CredentialInformation) => {
return new Promise<any>((resolve, reject) => {
RetrieveAccessToken(paths).then(
token => resolve(new Photos(token)),
@@ -101,20 +101,20 @@ export namespace GoogleApiServerUtils {
* Create an OAuth2 client with the given credentials, and returns the promise resolving to the authenticated client
* @param {Object} credentials The authorization client credentials.
*/
- export function authorize(credentials: any, token_path: string): Promise<TokenResult> {
+ export function authorize(credentials: any, userId: string): Promise<TokenResult> {
const { client_secret, client_id, redirect_uris } = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);
return new Promise<TokenResult>((resolve, reject) => {
- readFile(token_path, (err, token) => {
- // Check if we have previously stored a token.
- if (err) {
- return getNewToken(oAuth2Client, token_path).then(resolve, reject);
+ Database.Auxiliary.FetchGoogleAuthenticationToken(userId).then(token => {
+ // Check if we have previously stored a token for this userId.
+ if (!token) {
+ return getNewToken(oAuth2Client, userId).then(resolve, reject);
}
let parsed: Credentials = parseBuffer(token);
if (parsed.expiry_date! < new Date().getTime()) {
- return refreshToken(parsed, client_id, client_secret, oAuth2Client, token_path).then(resolve, reject);
+ return refreshToken(parsed, client_id, client_secret, oAuth2Client, userId).then(resolve, reject);
}
oAuth2Client.setCredentials(parsed);
resolve({ token: parsed, client: oAuth2Client });
diff --git a/src/server/apis/google/GooglePhotosUploadUtils.ts b/src/server/apis/google/GooglePhotosUploadUtils.ts
index 4dc252577..507a868a3 100644
--- a/src/server/apis/google/GooglePhotosUploadUtils.ts
+++ b/src/server/apis/google/GooglePhotosUploadUtils.ts
@@ -1,16 +1,10 @@
import request = require('request-promise');
import { GoogleApiServerUtils } from './GoogleApiServerUtils';
-import * as fs from 'fs';
-import { Utils } from '../../../Utils';
import * as path from 'path';
-import { Opt } from '../../../new_fields/Doc';
-import * as sharp from 'sharp';
import { MediaItemCreationResult } from './SharedTypes';
import { NewMediaItem } from "../../index";
import BatchedArray, { FixedBatcher, TimeUnit, Interval } from "array-batcher";
-const uploadDirectory = path.join(__dirname, "../../public/files/");
-
export namespace GooglePhotosUploadUtils {
export interface Paths {
@@ -31,12 +25,9 @@ export namespace GooglePhotosUploadUtils {
});
let Bearer: string;
- let Paths: Paths;
- export const initialize = async (paths: Paths) => {
- Paths = paths;
- const { tokenPath, credentialsPath } = paths;
- const token = await GoogleApiServerUtils.RetrieveAccessToken({ tokenPath, credentialsPath });
+ export const initialize = async (information: GoogleApiServerUtils.CredentialInformation) => {
+ const token = await GoogleApiServerUtils.RetrieveAccessToken(information);
Bearer = `Bearer ${token}`;
};
@@ -87,138 +78,4 @@ export namespace GooglePhotosUploadUtils {
return { newMediaItemResults };
};
-}
-
-export namespace DownloadUtils {
-
- export interface Size {
- width: number;
- suffix: string;
- }
-
- export const Sizes: { [size: string]: Size } = {
- SMALL: { width: 100, suffix: "_s" },
- MEDIUM: { width: 400, suffix: "_m" },
- LARGE: { width: 900, suffix: "_l" },
- };
-
- const gifs = [".gif"];
- const pngs = [".png"];
- const jpgs = [".jpg", ".jpeg"];
- const imageFormats = [...pngs, ...jpgs, ...gifs];
- const videoFormats = [".mov", ".mp4"];
-
- const size = "content-length";
- const type = "content-type";
-
- export interface UploadInformation {
- mediaPaths: string[];
- fileNames: { [key: string]: string };
- contentSize?: number;
- contentType?: string;
- }
-
- const generate = (prefix: string, url: string) => `${prefix}upload_${Utils.GenerateGuid()}${path.extname(url).toLowerCase()}`;
- const sanitize = (filename: string) => filename.replace(/\s+/g, "_");
-
- export interface InspectionResults {
- isLocal: boolean;
- stream: any;
- normalizedUrl: string;
- contentSize?: number;
- contentType?: string;
- }
-
- export const InspectImage = async (url: string): Promise<InspectionResults> => {
- const { isLocal, stream, normalized: normalizedUrl } = classify(url);
- const results = {
- isLocal,
- stream,
- normalizedUrl
- };
- if (isLocal) {
- return results;
- }
- const metadata = (await new Promise<any>((resolve, reject) => {
- request.head(url, async (error, res) => {
- if (error) {
- return reject(error);
- }
- resolve(res);
- });
- })).headers;
- return {
- contentSize: parseInt(metadata[size]),
- contentType: metadata[type],
- ...results
- };
- };
-
- export const UploadImage = async (metadata: InspectionResults, filename?: string, prefix = ""): Promise<Opt<UploadInformation>> => {
- const { isLocal, stream, normalizedUrl, contentSize, contentType } = metadata;
- const resolved = filename ? sanitize(filename) : generate(prefix, normalizedUrl);
- let extension = path.extname(normalizedUrl) || path.extname(resolved);
- extension && (extension = extension.toLowerCase());
- let information: UploadInformation = {
- mediaPaths: [],
- fileNames: { clean: resolved },
- contentSize,
- contentType,
- };
- return new Promise<UploadInformation>(async (resolve, reject) => {
- const resizers = [
- { resizer: sharp().rotate(), suffix: "_o" },
- ...Object.values(Sizes).map(size => ({
- resizer: sharp().resize(size.width, undefined, { withoutEnlargement: true }).rotate(),
- suffix: size.suffix
- }))
- ];
- let nonVisual = false;
- if (pngs.includes(extension)) {
- resizers.forEach(element => element.resizer = element.resizer.png());
- } else if (jpgs.includes(extension)) {
- resizers.forEach(element => element.resizer = element.resizer.jpeg());
- } else if (![...imageFormats, ...videoFormats].includes(extension.toLowerCase())) {
- nonVisual = true;
- }
- if (imageFormats.includes(extension)) {
- for (let resizer of resizers) {
- const suffix = resizer.suffix;
- let mediaPath: string;
- await new Promise<void>(resolve => {
- const filename = resolved.substring(0, resolved.length - extension.length) + suffix + extension;
- information.mediaPaths.push(mediaPath = uploadDirectory + filename);
- information.fileNames[suffix] = filename;
- stream(normalizedUrl).pipe(resizer.resizer).pipe(fs.createWriteStream(mediaPath))
- .on('close', resolve)
- .on('error', reject);
- });
- }
- }
- if (!isLocal || nonVisual) {
- await new Promise<void>(resolve => {
- stream(normalizedUrl).pipe(fs.createWriteStream(uploadDirectory + resolved)).on('close', resolve);
- });
- }
- resolve(information);
- });
- };
-
- const classify = (url: string) => {
- const isLocal = /Dash-Web(\\|\/)src(\\|\/)server(\\|\/)public(\\|\/)files/g.test(url);
- return {
- isLocal,
- stream: isLocal ? fs.createReadStream : request,
- normalized: isLocal ? path.normalize(url) : url
- };
- };
-
- export const createIfNotExists = async (path: string) => {
- if (await new Promise<boolean>(resolve => fs.exists(path, resolve))) {
- return true;
- }
- return new Promise<boolean>(resolve => fs.mkdir(path, error => resolve(error === null)));
- };
-
- export const Destroy = (mediaPath: string) => new Promise<boolean>(resolve => fs.unlink(mediaPath, error => resolve(error === null)));
} \ No newline at end of file
diff --git a/src/server/apis/google/existing_uploads.json b/src/server/apis/google/existing_uploads.json
deleted file mode 100644
index e69de29bb..000000000
--- a/src/server/apis/google/existing_uploads.json
+++ /dev/null