-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from desci-labs/gd-integration
Google APIs integration, import via gdrive
- Loading branch information
Showing
10 changed files
with
632 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Request, Response } from 'express'; | ||
|
||
import { logger as parentLogger } from '../../../logger.js'; | ||
import { processS3DataToIpfs } from '../../../services/data/processing.js'; | ||
import { GoogleApiService } from '../../../services/googleApiService.js'; | ||
import { ErrorResponse, UpdateResponse } from '../update.js'; | ||
|
||
interface GoogleImportReqBody { | ||
uuid: string; | ||
contextPath: string; | ||
googleFileId: string; | ||
gAuthAccessToken: string; // We can change this to use the oauth backend flow in the future | ||
} | ||
|
||
export const googleImport = async ( | ||
req: Request<any, any, GoogleImportReqBody>, | ||
res: Response<UpdateResponse | ErrorResponse>, | ||
) => { | ||
const owner = (req as any).user; | ||
const node = (req as any).node; | ||
|
||
const { uuid, contextPath, googleFileId, gAuthAccessToken } = req.body; | ||
if (contextPath === undefined) return res.status(400).json({ error: 'contextPath is required' }); | ||
if (googleFileId === undefined) return res.status(400).json({ error: 'googleFileId is required' }); | ||
if (gAuthAccessToken === undefined) return res.status(400).json({ error: 'gAuthAccessToken is required' }); | ||
|
||
const logger = parentLogger.child({ | ||
module: 'DATA::GoogleImportController', | ||
uuid: uuid, | ||
user: owner.id, | ||
contextPath: contextPath, | ||
googleFileId, | ||
}); | ||
const googleService = new GoogleApiService(gAuthAccessToken); | ||
// googleService.exchangeCodeForToken(gAuthAccessToken); | ||
const fileMd = await googleService.getFileMetadata(googleFileId); | ||
const fileStream = await googleService.getFileStream(googleFileId); | ||
// debugger; | ||
const files = [{ originalname: '/' + fileMd.name, content: fileStream, size: fileMd.size }]; | ||
const { ok, value } = await processS3DataToIpfs({ | ||
files, | ||
user: owner, | ||
node, | ||
contextPath, | ||
}); | ||
if (ok) { | ||
const { | ||
rootDataCid: newRootCidString, | ||
manifest: updatedManifest, | ||
manifestCid: persistedManifestCid, | ||
tree: tree, | ||
date: date, | ||
} = value as UpdateResponse; | ||
return res.status(200).json({ | ||
rootDataCid: newRootCidString, | ||
manifest: updatedManifest, | ||
manifestCid: persistedManifestCid, | ||
tree: tree, | ||
date: date, | ||
}); | ||
} else { | ||
if (!('message' in value)) return res.status(500); | ||
logger.error({ value }, 'processing error occured'); | ||
return res.status(value.status).json({ status: value.status, error: value.message }); | ||
} | ||
// return res.status(400); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Readable } from 'stream'; | ||
|
||
import { GaxiosResponse } from 'gaxios'; | ||
import { google, drive_v3 } from 'googleapis'; | ||
|
||
import { logger as parentLogger } from '../logger.js'; | ||
|
||
export class GoogleApiService { | ||
private oauth2Client; | ||
private driveClient: drive_v3.Drive; | ||
private logger; | ||
|
||
constructor(accessToken: string) { | ||
this.oauth2Client = new google.auth.OAuth2({ | ||
clientId: process.env.GOOGLE_CLIENT_ID, | ||
// clientSecret: process.env.GOOGLE_CLIENT_SECRET, Unnecessary unless we switch to 2step server-side OAuth flow | ||
}); | ||
this.oauth2Client.setCredentials({ access_token: accessToken }); | ||
this.driveClient = google.drive({ version: 'v3', auth: this.oauth2Client }); | ||
this.logger = parentLogger.child({ module: 'Services::GoogleApiService' }); | ||
} | ||
|
||
async getFileMetadata(docId: string): Promise<drive_v3.Schema$File> { | ||
try { | ||
const fileMetadata = await this.driveClient.files.get({ fileId: docId, fields: 'id, name, mimeType, size' }); | ||
|
||
return fileMetadata.data; | ||
} catch (error) { | ||
this.logger.error({ docId, error }, 'Failed to get file metadata'); | ||
throw error; | ||
} | ||
} | ||
|
||
async getFileStream(docId: string): Promise<Readable> { | ||
try { | ||
const response = await this.driveClient.files.get({ fileId: docId, alt: 'media' }, { responseType: 'stream' }); | ||
|
||
return response.data; | ||
} catch (error) { | ||
this.logger.error({ docId, error }, 'Failed to get file stream'); | ||
throw error; | ||
} | ||
} | ||
|
||
async authenticateWithAccessToken(accessToken: string): Promise<void> { | ||
try { | ||
this.oauth2Client.setCredentials({ access_token: accessToken }); | ||
this.logger.info('Successfully authenticated with access token'); | ||
} catch (error) { | ||
this.logger.error({ error }, 'Failed to authenticate with access token'); | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Can be used later if we switch to 2-step, server-side OAuth flow, useful if we need >60 minutes of access. | ||
*/ | ||
async exchangeCodeForToken(code: string): Promise<void> { | ||
try { | ||
const { tokens } = await this.oauth2Client.getToken(code); | ||
debugger; | ||
this.oauth2Client.setCredentials(tokens); | ||
this.logger.info('Successfully exchanged code for tokens'); | ||
} catch (error) { | ||
this.logger.error({ error }, 'Failed to exchange code for tokens'); | ||
throw error; | ||
} | ||
} | ||
} |
Oops, something went wrong.