From cadc7b9548169e29759effb62330e09ee0142865 Mon Sep 17 00:00:00 2001 From: kyle hoell Date: Wed, 25 Nov 2020 13:15:27 -0500 Subject: [PATCH] started cleaning up code --- backend/db/utils/googleFileUtils/index.ts | 34 ++++++++++++++++++++ backend/models/user.ts | 2 +- backend/services/GoogleFileService/index.ts | 29 +++++++---------- backend/utils/convertDriveListToMongoList.ts | 2 +- backend/utils/convertDriveToMongo.ts | 2 +- backend/utils/createQueryGoogle.ts | 6 ++++ 6 files changed, 54 insertions(+), 21 deletions(-) create mode 100644 backend/db/utils/googleFileUtils/index.ts diff --git a/backend/db/utils/googleFileUtils/index.ts b/backend/db/utils/googleFileUtils/index.ts new file mode 100644 index 0000000..0a66969 --- /dev/null +++ b/backend/db/utils/googleFileUtils/index.ts @@ -0,0 +1,34 @@ +import {UserInterface} from "../../../models/user"; +import env from "../../../enviroment/env"; +import { google } from "googleapis"; +import getGoogleAuth from "../../../db/googleAuth"; +import createQueryGoogle, {googleQueryType} from "../../../utils/createQueryGoogle"; + +const fields = 'id, name, size, modifiedTime, hasThumbnail, parents, mimeType, thumbnailLink, webViewLink, shared'; + +class GoogleDbUtil { + + constructor() { + + } + + getList = async(query: googleQueryType, user: UserInterface) => { + + const oauth2Client = await getGoogleAuth(user); + + const limit = query.limit; + + let parent = query.parent === "/" ? "root" : query.parent; + + const {queryBuilder, orderBy} = createQueryGoogle(query, parent) + + const previosPageToken = query.pageToken; + + const drive = google.drive({version:"v3", auth: oauth2Client}); + const files = await drive.files.list({pageSize: limit, fields: `nextPageToken, files(${fields})`, q: queryBuilder, orderBy, pageToken: previosPageToken}); + + return files + } +} + +export default GoogleDbUtil; \ No newline at end of file diff --git a/backend/models/user.ts b/backend/models/user.ts index 93ac254..1e70704 100644 --- a/backend/models/user.ts +++ b/backend/models/user.ts @@ -205,7 +205,7 @@ export interface UserInterface extends Document { changeEncryptionKey: (randomKey: Buffer) => void; generateEmailVerifyToken: () => string; generatePasswordResetToken: () => string; - encryptDriveIDandKey: (ID:string, key: string) => void; + encryptDriveIDandKey: (ID:string, key: string) => Promise; decryptDriveIDandKey: () => {clientID: string, clientKey: string}; encryptDriveTokenData: (token: Object) => void; decryptDriveTokenData: () => any; diff --git a/backend/services/GoogleFileService/index.ts b/backend/services/GoogleFileService/index.ts index eb0cbc9..581cfa5 100644 --- a/backend/services/GoogleFileService/index.ts +++ b/backend/services/GoogleFileService/index.ts @@ -13,6 +13,10 @@ import uuid from "uuid"; import getBusboyData from "../ChunkService/utils/getBusboyData"; import axios from "axios"; import awaitUploadGoogle from "../ChunkService/utils/awaitUploadGoogle"; +import {googleQueryType} from "../../utils/createQueryGoogle"; +import GoogleDbUtils from "../../db/utils/googleFileUtils"; + +const googleDbUtils = new GoogleDbUtils(); interface RequestType extends Request { user?: UserInterface, @@ -30,22 +34,11 @@ class GoogleFileService { } - getList = async(user: UserInterface, query: any) => { + getList = async(user: UserInterface, query: googleQueryType) => { - const oauth2Client = await getGoogleAuth(user); + const files = await googleDbUtils.getList(query, user); - const limit: any = query.limit as any; - - let parent = query.parent === "/" ? "root" : query.parent; - - const {queryBuilder, orderBy} = createQueryGoogle(query, parent) - - const previosPageToken: any = query.pageToken; - - const drive = google.drive({version:"v3", auth: oauth2Client}); - const files = await drive.files.list({pageSize: limit, fields: `nextPageToken, files(${fields})`, q: queryBuilder, orderBy, pageToken: previosPageToken}); - - const nextPageToken: any = files.data.nextPageToken; + const nextPageToken = files.data.nextPageToken; const userID = user._id; @@ -54,22 +47,22 @@ class GoogleFileService { return convertedFiles; } - getMongoGoogleList = async(user: UserInterface, query: any) => { + getMongoGoogleList = async(user: UserInterface, query: googleQueryType) => { const oauth2Client = await getGoogleAuth(user); - const limit: any = query.limit as any; + const limit = query.limit; let parent = query.parent === "/" ? "root" : query.parent; const {queryBuilder, orderBy} = createQueryGoogle(query, parent) - const previosPageToken: any = query.pageToken; + const previosPageToken = query.pageToken; const drive = google.drive({version:"v3", auth: oauth2Client}); const files = await drive.files.list({pageSize: limit, fields: `nextPageToken, files(${fields})`, q: queryBuilder, orderBy, pageToken: previosPageToken}); - const nextPageToken: any = files.data.nextPageToken; + const nextPageToken = files.data.nextPageToken; const userID = user._id; diff --git a/backend/utils/convertDriveListToMongoList.ts b/backend/utils/convertDriveListToMongoList.ts index ad46cfd..b5e5e61 100644 --- a/backend/utils/convertDriveListToMongoList.ts +++ b/backend/utils/convertDriveListToMongoList.ts @@ -1,6 +1,6 @@ import convertDriveToMongo from "./convertDriveToMongo"; -const convertDriveListToMongoList = (driveObjs: any, ownerID:string, pageToken?: string) => { +const convertDriveListToMongoList = (driveObjs: any, ownerID:string, pageToken?: string | null | undefined) => { let convertedObjs = []; diff --git a/backend/utils/convertDriveToMongo.ts b/backend/utils/convertDriveToMongo.ts index ed7121d..b02b099 100644 --- a/backend/utils/convertDriveToMongo.ts +++ b/backend/utils/convertDriveToMongo.ts @@ -1,6 +1,6 @@ import videoChecker from "./videoChecker"; -const convertDriveToMongo = (driveObj: any, ownerID: string, pageToken?: string) => { +const convertDriveToMongo = (driveObj: any, ownerID: string, pageToken?: string | undefined | null) => { let convertedObj: any = {}; convertedObj._id = driveObj.id; diff --git a/backend/utils/createQueryGoogle.ts b/backend/utils/createQueryGoogle.ts index 5097d01..5d8b88e 100644 --- a/backend/utils/createQueryGoogle.ts +++ b/backend/utils/createQueryGoogle.ts @@ -25,4 +25,10 @@ const createQueryGoogle = (query: any, parent: any) => { return {queryBuilder, orderBy} } +export interface googleQueryType { + limit: number, + parent: string, + pageToken: string, +} + export default createQueryGoogle; \ No newline at end of file