From 20540a35be82c34cc3962de4f957d1aa43f8a2b0 Mon Sep 17 00:00:00 2001 From: Sam Wilkins Date: Thu, 26 Sep 2019 22:17:15 -0400 Subject: finally transferred google credential management to database --- src/client/Network.ts | 46 +++++++++++++--------- .../apis/google_docs/GooglePhotosClientUtils.ts | 17 ++++---- src/client/documents/Documents.ts | 22 ++++++----- .../util/Import & Export/DirectoryImportBox.tsx | 5 +-- src/client/views/nodes/DocumentView.tsx | 7 ++-- src/server/apis/google/GoogleApiServerUtils.ts | 27 +++++++------ src/server/database.ts | 32 +++++++++------ src/server/index.ts | 6 +-- 8 files changed, 93 insertions(+), 69 deletions(-) (limited to 'src') diff --git a/src/client/Network.ts b/src/client/Network.ts index cb46105f8..75ccb5e99 100644 --- a/src/client/Network.ts +++ b/src/client/Network.ts @@ -2,24 +2,32 @@ import { Utils } from "../Utils"; import { CurrentUserUtils } from "../server/authentication/models/current_user_utils"; import requestPromise = require('request-promise'); -export async function PostToServer(relativeRoute: string, body?: any) { - let options = { - uri: Utils.prepend(relativeRoute), - method: "POST", - headers: { userId: CurrentUserUtils.id }, - body, - json: true - }; - return requestPromise.post(options); -} +export namespace Identified { + + export async function FetchFromServer(relativeRoute: string) { + return (await fetch(relativeRoute, { headers: { userId: CurrentUserUtils.id } })).text(); + } + + export async function PostToServer(relativeRoute: string, body?: any) { + let options = { + uri: Utils.prepend(relativeRoute), + method: "POST", + headers: { userId: CurrentUserUtils.id }, + body, + json: true + }; + return requestPromise.post(options); + } + + export async function PostFormDataToServer(relativeRoute: string, formData: FormData) { + const parameters = { + method: 'POST', + headers: { userId: CurrentUserUtils.id }, + body: formData, + }; + const response = await fetch(relativeRoute, parameters); + const text = await response.json(); + return text; + } -export async function PostFormDataToServer(relativeRoute: string, formData: FormData) { - const parameters = { - method: 'POST', - headers: { userId: CurrentUserUtils.id }, - body: formData, - }; - const response = await fetch(relativeRoute, parameters); - const text = await response.json(); - return text; } \ No newline at end of file diff --git a/src/client/apis/google_docs/GooglePhotosClientUtils.ts b/src/client/apis/google_docs/GooglePhotosClientUtils.ts index b1b29210a..29cc042b6 100644 --- a/src/client/apis/google_docs/GooglePhotosClientUtils.ts +++ b/src/client/apis/google_docs/GooglePhotosClientUtils.ts @@ -14,11 +14,14 @@ import { NewMediaItemResult, MediaItem } from "../../../server/apis/google/Share import { AssertionError } from "assert"; import { DocumentView } from "../../views/nodes/DocumentView"; import { DocumentManager } from "../../util/DocumentManager"; -import { PostToServer } from "../../Network"; +import { Identified } from "../../Network"; export namespace GooglePhotos { - const endpoint = async () => new Photos(await PostToServer(RouteStore.googlePhotosAccessToken)); + const endpoint = async () => { + const accessToken = await Identified.FetchFromServer(RouteStore.googlePhotosAccessToken); + return new Photos(accessToken); + }; export enum MediaType { ALL_MEDIA = 'ALL_MEDIA', @@ -296,7 +299,7 @@ export namespace GooglePhotos { }; export const WriteMediaItemsToServer = async (body: { mediaItems: any[] }): Promise => { - const uploads = await PostToServer(RouteStore.googlePhotosMediaDownload, body); + const uploads = await Identified.PostToServer(RouteStore.googlePhotosMediaDownload, body); return uploads; }; @@ -316,7 +319,7 @@ export namespace GooglePhotos { album = await Create.Album(album.title); } const media: MediaInput[] = []; - sources.forEach(source => { + for (let source of sources) { const data = Cast(Doc.GetProto(source).data, ImageField); if (!data) { return; @@ -324,11 +327,11 @@ export namespace GooglePhotos { const url = data.url.href; const target = Doc.MakeAlias(source); const description = parseDescription(target, descriptionKey); - DocumentView.makeCustomViewClicked(target, undefined); + await DocumentView.makeCustomViewClicked(target, undefined); media.push({ url, description }); - }); + } if (media.length) { - const uploads: NewMediaItemResult[] = await PostToServer(RouteStore.googlePhotosMediaUpload, { media, album }); + const uploads: NewMediaItemResult[] = await Identified.PostToServer(RouteStore.googlePhotosMediaUpload, { media, album }); return uploads; } }; diff --git a/src/client/documents/Documents.ts b/src/client/documents/Documents.ts index a7a006f47..0369e89a6 100644 --- a/src/client/documents/Documents.ts +++ b/src/client/documents/Documents.ts @@ -337,16 +337,18 @@ export namespace Docs { let extension = path.extname(target); target = `${target.substring(0, target.length - extension.length)}_o${extension}`; } - requestImageSize(target) - .then((size: any) => { - let aspect = size.height / size.width; - if (!inst.proto!.nativeWidth) { - inst.proto!.nativeWidth = size.width; - } - inst.proto!.nativeHeight = Number(inst.proto!.nativeWidth!) * aspect; - inst.proto!.height = NumCast(inst.proto!.width) * aspect; - }) - .catch((err: any) => console.log(err)); + if (target !== "http://www.cs.brown.edu/") { + requestImageSize(target) + .then((size: any) => { + let aspect = size.height / size.width; + if (!inst.proto!.nativeWidth) { + inst.proto!.nativeWidth = size.width; + } + inst.proto!.nativeHeight = Number(inst.proto!.nativeWidth!) * aspect; + inst.proto!.height = NumCast(inst.proto!.width) * aspect; + }) + .catch((err: any) => console.log(err)); + } return inst; } export function PresDocument(initial: List = new List(), options: DocumentOptions = {}) { diff --git a/src/client/util/Import & Export/DirectoryImportBox.tsx b/src/client/util/Import & Export/DirectoryImportBox.tsx index d0291aec4..de5287456 100644 --- a/src/client/util/Import & Export/DirectoryImportBox.tsx +++ b/src/client/util/Import & Export/DirectoryImportBox.tsx @@ -21,7 +21,7 @@ import { GooglePhotos } from "../../apis/google_docs/GooglePhotosClientUtils"; import { SchemaHeaderField } from "../../../new_fields/SchemaHeaderField"; import "./DirectoryImportBox.scss"; import BatchedArray from "array-batcher"; -import { PostFormDataToServer } from "../../Network"; +import { Identified } from "../../Network"; const unsupported = ["text/html", "text/plain"]; interface FileResponse { @@ -114,7 +114,7 @@ export default class DirectoryImportBox extends React.Component formData.append(Utils.GenerateGuid(), file); }); - const responses = await PostFormDataToServer(RouteStore.upload, formData); + const responses = await Identified.PostFormDataToServer(RouteStore.upload, formData); runInAction(() => this.completed += batch.length); return responses as FileResponse[]; }); @@ -279,7 +279,6 @@ export default class DirectoryImportBox extends React.Component }} />