-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(face-validation): integrate face validation service and update t…
…utor profile picture handling
- Loading branch information
Showing
11 changed files
with
199 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ FIREBASE_DATABASE_URL= | |
GCS_BUCKET_NAME= | ||
GROQ_KEY= | ||
DATABASE_URL= | ||
JWT_SECRET= | ||
JWT_SECRET= | ||
FACE_VALIDATION_URL= |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,34 @@ | ||
import { FACE_VALIDATION_ENABLED, FACE_VALIDATION_URL } from "@/config"; | ||
import { | ||
RemoteFaceValidationService, | ||
NoOpFaceValidationService, | ||
} from "./face-validation.service"; | ||
import { logger } from "@middleware/logging.middleware"; | ||
import axios from "axios"; | ||
|
||
export const createFaceValidationService = () => { | ||
if (!FACE_VALIDATION_ENABLED) { | ||
logger.info("Face validation is disabled, using NoOp service"); | ||
return new NoOpFaceValidationService(); | ||
} | ||
|
||
const service = new RemoteFaceValidationService(FACE_VALIDATION_URL!); | ||
|
||
try { | ||
axios | ||
.get(`${FACE_VALIDATION_URL}/health`, { | ||
timeout: 5000, | ||
}) | ||
.catch(() => { | ||
throw new Error("Health check failed"); | ||
}); | ||
|
||
logger.info("Face validation service is healthy and ready"); | ||
return service; | ||
} catch (error) { | ||
logger.warn( | ||
`Face validation service is unavailable: ${error}, falling back to NoOp service`, | ||
); | ||
return new NoOpFaceValidationService(); | ||
} | ||
}; |
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,10 @@ | ||
export interface FaceValidationResponse { | ||
is_valid: boolean; | ||
face_count: number; | ||
message: string; | ||
} | ||
|
||
export interface FaceValidationService { | ||
validateFace(imageBuffer: Buffer): Promise<FaceValidationResponse>; | ||
checkHealth(): Promise<boolean>; | ||
} |
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,80 @@ | ||
import axios from "axios"; | ||
import { | ||
FaceValidationService, | ||
FaceValidationResponse, | ||
} from "./face-validation.interface"; | ||
import { logger } from "@middleware/logging.middleware"; | ||
|
||
export class RemoteFaceValidationService implements FaceValidationService { | ||
constructor(private readonly baseUrl: string) {} | ||
|
||
async validateFace(imageBuffer: Buffer): Promise<FaceValidationResponse> { | ||
try { | ||
const imageBase64 = imageBuffer.toString("base64"); | ||
const response = await axios.post<{ | ||
is_valid: boolean; | ||
face_count: number; | ||
message: string; | ||
}>( | ||
`${this.baseUrl}/validate-face`, | ||
{ image: imageBase64 }, | ||
{ | ||
headers: { "Content-Type": "application/json" }, | ||
timeout: 5000, | ||
}, | ||
); | ||
|
||
return { | ||
is_valid: response.data.is_valid, | ||
face_count: response.data.face_count, | ||
message: response.data.message, | ||
}; | ||
} catch (error) { | ||
logger.error("Face validation service error:", error); | ||
throw new Error("Face validation service unavailable"); | ||
} | ||
} | ||
|
||
checkHealthSync(): boolean { | ||
try { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open("GET", `${this.baseUrl}/health`, false); // Make the request synchronous | ||
xhr.timeout = 5000; | ||
xhr.send(null); | ||
|
||
return xhr.status === 200; | ||
} catch (error) { | ||
logger.warn("Health check failed:", error); | ||
return false; | ||
} | ||
} | ||
|
||
async checkHealth(): Promise<boolean> { | ||
try { | ||
const response = await axios.get(`${this.baseUrl}/health`, { | ||
timeout: 5000, | ||
}); | ||
return response.data.status === "healthy"; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
export class NoOpFaceValidationService implements FaceValidationService { | ||
async validateFace(_imageBuffer: Buffer): Promise<FaceValidationResponse> { | ||
return { | ||
is_valid: true, | ||
face_count: 0, | ||
message: "Face validation is not enabled", | ||
}; | ||
} | ||
|
||
checkHealthSync(): boolean { | ||
return true; | ||
} | ||
|
||
async checkHealth(): Promise<boolean> { | ||
return true; | ||
} | ||
} |
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,10 @@ | ||
export class ValidationError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = "ValidationError"; | ||
} | ||
|
||
static isFaceValidationError(error: unknown): error is ValidationError { | ||
return error instanceof ValidationError; | ||
} | ||
} |
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