aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/ApiManagers')
-rw-r--r--src/server/ApiManagers/GeneralGoogleManager.ts47
-rw-r--r--src/server/ApiManagers/GooglePhotosManager.ts27
2 files changed, 56 insertions, 18 deletions
diff --git a/src/server/ApiManagers/GeneralGoogleManager.ts b/src/server/ApiManagers/GeneralGoogleManager.ts
index cb37b0dce..89efebf78 100644
--- a/src/server/ApiManagers/GeneralGoogleManager.ts
+++ b/src/server/ApiManagers/GeneralGoogleManager.ts
@@ -1,12 +1,20 @@
import ApiManager, { Registration } from "./ApiManager";
import { Method, _permission_denied } from "../RouteManager";
-import { uploadDirectory } from "..";
-import { path } from "animejs";
-import { RouteStore } from "../RouteStore";
import { GoogleApiServerUtils } from "../apis/google/GoogleApiServerUtils";
import { Database } from "../database";
+import RouteSubscriber from "../RouteSubscriber";
const deletionPermissionError = "Cannot perform specialized delete outside of the development environment!";
+const ServicesApiKeyMap = new Map<string, string | undefined>([
+ ["face", process.env.FACE],
+ ["vision", process.env.VISION],
+ ["handwriting", process.env.HANDWRITING]
+]);
+const EndpointHandlerMap = new Map<GoogleApiServerUtils.Action, GoogleApiServerUtils.ApiRouter>([
+ ["create", (api, params) => api.create(params)],
+ ["retrieve", (api, params) => api.get(params)],
+ ["update", (api, params) => api.batchUpdate(params)],
+]);
export default class GeneralGoogleManager extends ApiManager {
@@ -14,7 +22,7 @@ export default class GeneralGoogleManager extends ApiManager {
register({
method: Method.GET,
- subscription: RouteStore.readGoogleAccessToken,
+ subscription: "/readGoogleAccessToken",
onValidation: async ({ user, res }) => {
const userId = user.id;
const token = await GoogleApiServerUtils.retrieveAccessToken(userId);
@@ -27,7 +35,7 @@ export default class GeneralGoogleManager extends ApiManager {
register({
method: Method.POST,
- subscription: RouteStore.writeGoogleAccessToken,
+ subscription: "/writeGoogleAccessToken",
onValidation: async ({ user, req, res }) => {
res.send(await GoogleApiServerUtils.processNewUser(user.id, req.body.authenticationCode));
}
@@ -41,7 +49,34 @@ export default class GeneralGoogleManager extends ApiManager {
return _permission_denied(res, deletionPermissionError);
}
await Database.Auxiliary.GoogleAuthenticationToken.DeleteAll();
- res.redirect(RouteStore.delete);
+ res.redirect("/delete");
+ }
+ });
+
+ register({
+ method: Method.GET,
+ subscription: new RouteSubscriber("/cognitiveServices").add('requestedservice'),
+ onValidation: ({ req, res }) => {
+ let service = req.params.requestedservice;
+ res.send(ServicesApiKeyMap.get(service));
+ }
+ });
+
+ register({
+ method: Method.POST,
+ subscription: new RouteSubscriber("/googleDocs").add("sector", "action"),
+ onValidation: async ({ req, res, user }) => {
+ let sector: GoogleApiServerUtils.Service = req.params.sector as GoogleApiServerUtils.Service;
+ let action: GoogleApiServerUtils.Action = req.params.action as GoogleApiServerUtils.Action;
+ const endpoint = await GoogleApiServerUtils.GetEndpoint(GoogleApiServerUtils.Service[sector], user.id);
+ let handler = EndpointHandlerMap.get(action);
+ if (endpoint && handler) {
+ handler(endpoint, req.body)
+ .then(response => res.send(response.data))
+ .catch(exception => res.send(exception));
+ return;
+ }
+ res.send(undefined);
}
});
}
diff --git a/src/server/ApiManagers/GooglePhotosManager.ts b/src/server/ApiManagers/GooglePhotosManager.ts
index b5e9caa38..1f6051c28 100644
--- a/src/server/ApiManagers/GooglePhotosManager.ts
+++ b/src/server/ApiManagers/GooglePhotosManager.ts
@@ -1,16 +1,12 @@
import ApiManager, { Registration } from "./ApiManager";
import { Method, _error, _success, _invalid } from "../RouteManager";
-import { uploadDirectory, NewMediaItem } from "..";
-import { path } from "animejs";
-import { RouteStore } from "../RouteStore";
+import * as path from "path";
import { GoogleApiServerUtils } from "../apis/google/GoogleApiServerUtils";
import { BatchedArray, TimeUnit } from "array-batcher";
import { GooglePhotosUploadUtils } from "../apis/google/GooglePhotosUploadUtils";
-import { MediaItem } from "../apis/google/SharedTypes";
import { Opt } from "../../new_fields/Doc";
import { DashUploadUtils } from "../DashUploadUtils";
import { Database } from "../database";
-import { prefix } from "@fortawesome/free-solid-svg-icons";
const authenticationError = "Unable to authenticate Google credentials before uploading to Google Photos!";
const mediaError = "Unable to convert all uploaded bytes to media items!";
@@ -23,6 +19,17 @@ interface GooglePhotosUploadFailure {
url: string;
reason: string;
}
+interface MediaItem {
+ baseUrl: string;
+ filename: string;
+}
+interface NewMediaItem {
+ description: string;
+ simpleMediaItem: {
+ uploadToken: string;
+ };
+}
+const prefix = "google_photos_";
export default class GooglePhotosManager extends ApiManager {
@@ -30,20 +37,18 @@ export default class GooglePhotosManager extends ApiManager {
register({
method: Method.POST,
- subscription: RouteStore.googlePhotosMediaUpload,
+ subscription: "/googlePhotosMediaUpload",
onValidation: async ({ user, req, res }) => {
const { media } = req.body;
-
const token = await GoogleApiServerUtils.retrieveAccessToken(user.id);
if (!token) {
return _error(res, authenticationError);
}
-
let failed: GooglePhotosUploadFailure[] = [];
const batched = BatchedArray.from<GooglePhotosUploadUtils.UploadSource>(media, { batchSize: 25 });
const newMediaItems = await batched.batchedMapPatientInterval<NewMediaItem>(
{ magnitude: 100, unit: TimeUnit.Milliseconds },
- async (batch, collector, { completedBatches }) => {
+ async (batch: any, collector: any, { completedBatches }: any) => {
for (let index = 0; index < batch.length; index++) {
const { url, description } = batch[index];
const fail = (reason: string) => failed.push({ reason, batch: completedBatches + 1, index, url });
@@ -59,13 +64,11 @@ export default class GooglePhotosManager extends ApiManager {
}
}
);
-
const failedCount = failed.length;
if (failedCount) {
console.error(`Unable to upload ${failedCount} image${failedCount === 1 ? "" : "s"} to Google's servers`);
console.log(failed.map(({ reason, batch, index, url }) => `@${batch}.${index}: ${url} failed:\n${reason}`).join('\n\n'));
}
-
return GooglePhotosUploadUtils.CreateMediaItems(token, newMediaItems, req.body.album).then(
results => _success(res, { results, failed }),
error => _error(res, mediaError, error)
@@ -75,7 +78,7 @@ export default class GooglePhotosManager extends ApiManager {
register({
method: Method.POST,
- subscription: RouteStore.googlePhotosMediaDownload,
+ subscription: "/googlePhotosMediaDownload",
onValidation: async ({ req, res }) => {
const contents: { mediaItems: MediaItem[] } = req.body;
let failed = 0;