-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55f17f4
commit 2722e7d
Showing
7 changed files
with
67 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
import OnboardingServices from '../services/onboarding.services'; | ||
|
||
export default class OnboardingController { | ||
static async downloadImage(req: Request, res: Response, next: NextFunction) { | ||
try { | ||
const { fileId } = req.params; | ||
console.log('FILE ID:', fileId); | ||
|
||
const imageData = await OnboardingServices.downloadImage(fileId); | ||
|
||
// Set the appropriate headers for the HTTP response | ||
res.setHeader('content-type', String(imageData.type)); | ||
res.setHeader('content-length', imageData.buffer.length); | ||
|
||
// Send the Buffer as the response body | ||
res.send(imageData.buffer); | ||
} catch (error: unknown) { | ||
return next(error); | ||
} | ||
} | ||
} |
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,8 @@ | ||
import express from 'express'; | ||
import OnboardingController from '../controllers/onboarding.controller'; | ||
|
||
const onboardingRouter = express.Router(); | ||
|
||
onboardingRouter.get('/image/:fileId', OnboardingController.downloadImage); | ||
|
||
export default onboardingRouter; |
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,12 @@ | ||
import { NotFoundException } from '../utils/errors.utils'; | ||
import { downloadImageFile } from '../utils/google-integration.utils'; | ||
|
||
export default class OnboardingServices { | ||
static async downloadImage(fileId: string) { | ||
const fileData = await downloadImageFile(fileId); | ||
console.log('FILE DATA RECEIVED'); | ||
|
||
if (!fileData) throw new NotFoundException('Image File', fileId); | ||
return fileData; | ||
} | ||
} |
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,17 @@ | ||
import axios from 'axios'; | ||
import { apiUrls } from '../utils/urls'; | ||
|
||
/** | ||
* API Call to download a google image | ||
* @param fileId file id to be downloaded | ||
* @returns an image blob | ||
*/ | ||
export const downloadGoogleImage = async (fileId: string): Promise<Blob> => { | ||
const response = await axios.get(apiUrls.imageById(fileId), { | ||
responseType: 'arraybuffer' // Set the response type to 'arraybuffer' to receive the image as a Buffer | ||
}); | ||
console.log('ID IN API:', fileId); | ||
const imageBuffer = new Uint8Array(response.data); | ||
const imageBlob = new Blob([imageBuffer], { type: response.headers['content-type'] }); | ||
return imageBlob; | ||
}; |
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