Skip to content

Commit

Permalink
added boundary validations if present in camera relative move
Browse files Browse the repository at this point in the history
  • Loading branch information
khavinshankar authored Oct 21, 2024
1 parent 126bc78 commit f3d5994
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/Validators/cameraValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,41 @@ export const setPresetValidators = [
.withMessage("presetName must be 1 to 20 character long."),
];

export const boundaryValidator = [
body("boundary")
.optional()
.isObject()
.withMessage("boundary must be an object."),

body("boundary.x0")
.if(body("boundary").exists())
.exists({ checkNull: true })
.withMessage("boundary.x0 is required.")
.isFloat()
.withMessage("boundary.x0 must be a number."),

body("boundary.x1")
.if(body("boundary").exists())
.exists({ checkNull: true })
.withMessage("boundary.x1 is required.")
.isFloat()
.withMessage("boundary.x1 must be a number."),

body("boundary.y0")
.if(body("boundary").exists())
.exists({ checkNull: true })
.withMessage("boundary.y0 is required.")
.isFloat()
.withMessage("boundary.y0 must be a number."),

body("boundary.y1")
.if(body("boundary").exists())
.exists({ checkNull: true })
.withMessage("boundary.y1 is required.")
.isFloat()
.withMessage("boundary.y1 must be a number."),
];

export const camMoveValidator = [
...baseCameraParamsValidators,
body("x")
Expand All @@ -83,4 +118,5 @@ export const camMoveValidator = [
.withMessage("zoom is required.")
.isFloat({ max: 1, min: -1 })
.withMessage("zoom must be number."),
...boundaryValidator,
];
26 changes: 25 additions & 1 deletion src/controller/CameraController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,31 @@ export class CameraController {

static relativeMove = catchAsync(async (req: Request, res: Response) => {
const camParams = this._getCamParams(req.body);
const { x, y, zoom } = req.body;
const { x, y, zoom, boundary } = req.body;

if (boundary) {
const status = await CameraUtils.getStatus({ camParams });
const { x: currentX, y: currentY } = status.position;
const { x0, x1, y0, y1 } = boundary;

const targetX = currentX + (x ?? 0);
const targetY = currentY + (y ?? 0);

const xMin = Math.min(x0, x1);
const xMax = Math.max(x0, x1);
const yMin = Math.min(y0, y1);
const yMax = Math.max(y0, y1);

if (
!(xMin <= targetX && targetX <= xMax) ||
!(yMin <= targetY && targetY <= yMax)
) {
return res.status(400).json({
status: "error",
message: "The requested position is out of the allowed boundary.",
});
}
}

await CameraUtils.relativeMove({ camParams, x, y, zoom });

Expand Down

0 comments on commit f3d5994

Please sign in to comment.