aboutsummaryrefslogtreecommitdiff
path: root/src/server/index.ts
diff options
context:
space:
mode:
authorMohammad Amoush <muhammedamoush@gmail.com>2019-11-09 16:29:20 -0500
committerMohammad Amoush <muhammedamoush@gmail.com>2019-11-09 16:29:20 -0500
commitee4910b1a90284c71ebdaa5fbd9243148ae113f6 (patch)
tree112747bb7dd4d6ea0fee90d51e65c2c1e60e0e7b /src/server/index.ts
parentf0aa70db1b0f88a7f42d1e9f86466b08a4522676 (diff)
initial
Diffstat (limited to 'src/server/index.ts')
-rw-r--r--src/server/index.ts123
1 files changed, 0 insertions, 123 deletions
diff --git a/src/server/index.ts b/src/server/index.ts
index 25697e71f..9a5099d0d 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -585,89 +585,11 @@ function routeSetter(router: RouteManager) {
}
});
- router.addSupervisedRoute({
- method: Method.GET,
- subscription: RouteStore.readGoogleAccessToken,
- onValidation: async ({ user, res }) => {
- const userId = user.id;
- const token = await GoogleApiServerUtils.retrieveAccessToken(userId);
- if (!token) {
- return res.send(GoogleApiServerUtils.generateAuthenticationUrl());
- }
- return res.send(token);
- }
- });
-
- router.addSupervisedRoute({
- method: Method.POST,
- subscription: RouteStore.writeGoogleAccessToken,
- onValidation: async ({ user, req, res }) => {
- res.send(await GoogleApiServerUtils.processNewUser(user.id, req.body.authenticationCode));
- }
- });
-
- const authenticationError = "Unable to authenticate Google credentials before uploading to Google Photos!";
- const mediaError = "Unable to convert all uploaded bytes to media items!";
- interface GooglePhotosUploadFailure {
- batch: number;
- index: number;
- url: string;
- reason: string;
- }
-
- router.addSupervisedRoute({
- method: Method.POST,
- subscription: RouteStore.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 }) => {
- 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 });
- const uploadToken = await GooglePhotosUploadUtils.DispatchGooglePhotosUpload(token, url).catch(fail);
- if (!uploadToken) {
- fail(`${path.extname(url)} is not an accepted extension`);
- } else {
- collector.push({
- description,
- simpleMediaItem: { uploadToken }
- });
- }
- }
- }
- );
-
- 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)
- );
- }
- });
-
interface MediaItem {
baseUrl: string;
filename: string;
}
const prefix = "google_photos_";
-
- const downloadError = "Encountered an error while executing downloads.";
- const requestError = "Unable to execute download: the body's media items were malformed.";
const deletionPermissionError = "Cannot perform specialized delete outside of the development environment!";
router.addSupervisedRoute({
@@ -681,49 +603,4 @@ function routeSetter(router: RouteManager) {
res.redirect(RouteStore.delete);
}
});
-
- router.addSupervisedRoute({
- method: Method.GET,
- subscription: "/deleteWithGoogleCredentials",
- onValidation: async ({ res, isRelease }) => {
- if (isRelease) {
- return _permission_denied(res, deletionPermissionError);
- }
- await Database.Auxiliary.GoogleAuthenticationToken.DeleteAll();
- res.redirect(RouteStore.delete);
- }
- });
-
- const UploadError = (count: number) => `Unable to upload ${count} images to Dash's server`;
- router.addSupervisedRoute({
- method: Method.POST,
- subscription: RouteStore.googlePhotosMediaDownload,
- onValidation: async ({ req, res }) => {
- const contents: { mediaItems: MediaItem[] } = req.body;
- let failed = 0;
- if (contents) {
- const completed: Opt<DashUploadUtils.UploadInformation>[] = [];
- for (let item of contents.mediaItems) {
- const { contentSize, ...attributes } = await DashUploadUtils.InspectImage(item.baseUrl);
- const found: Opt<DashUploadUtils.UploadInformation> = await Database.Auxiliary.QueryUploadHistory(contentSize!);
- if (!found) {
- const upload = await DashUploadUtils.UploadInspectedImage({ contentSize, ...attributes }, item.filename, prefix).catch(error => _error(res, downloadError, error));
- if (upload) {
- completed.push(upload);
- await Database.Auxiliary.LogUpload(upload);
- } else {
- failed++;
- }
- } else {
- completed.push(found);
- }
- }
- if (failed) {
- return _error(res, UploadError(failed));
- }
- return _success(res, completed);
- }
- _invalid(res, requestError);
- }
- });
} \ No newline at end of file