-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(system-server): add /system/oem_mode/upload_splash endpoint to change boot splash #14865
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b78bbeb
feat(system-server): add endpoint to enable/disable OEM Mode.
vegano1 5af14aa
feat(robot-server): use the /system/oem_mode/enable endpoint when ena…
vegano1 43585eb
load system server settings from dotenv file if provided
vegano1 2eb299d
use RobotTypeEnum instead of str to check robot_type
vegano1 131078b
add aiohttp==3.8.1
vegano1 33244cf
Merge branch 'edge' into PLAT-276-add-oem-mode-endpoint
vegano1 e7014ec
format
vegano1 b0adf34
format
vegano1 9b242ef
add tavern test
vegano1 9194afe
save
vegano1 ec8065c
feat(system-server): add /system/oem_mode/upload_splash endpoint
vegano1 a893fe5
add show_if true for enableOEMMode
vegano1 ba04696
Merge branch 'PLAT-276-add-oem-mode-endpoint' into PLAT-277-add-oem-s…
vegano1 7b8a8e1
only accept PNG files
vegano1 9539f30
add tavern tests
vegano1 b90396a
Merge branch 'edge' into PLAT-277-add-oem-splash-logo-endpoint
vegano1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
"""Router for /system/register endpoint.""" | ||
|
||
from fastapi import APIRouter, Depends, status, Response | ||
import os | ||
import filetype # type: ignore[import-untyped] | ||
from fastapi import ( | ||
APIRouter, | ||
Depends, | ||
status, | ||
Response, | ||
UploadFile, | ||
File, | ||
HTTPException, | ||
) | ||
|
||
from .models import EnableOEMMode | ||
from ...settings import SystemServerSettings, get_settings, save_settings | ||
|
||
|
@@ -35,3 +46,87 @@ async def enable_oem_mode_endpoint( | |
except Exception: | ||
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR | ||
return response | ||
|
||
|
||
@oem_mode_router.post( | ||
"/system/oem_mode/upload_splash", | ||
summary="Upload an image to be used as the boot up splash screen.", | ||
responses={ | ||
status.HTTP_201_CREATED: {"message": "OEM Mode splash screen uploaded"}, | ||
status.HTTP_400_BAD_REQUEST: {"message": "OEM Mode splash screen not set"}, | ||
status.HTTP_413_REQUEST_ENTITY_TOO_LARGE: { | ||
"message": "File is larger than 5mb" | ||
}, | ||
status.HTTP_415_UNSUPPORTED_MEDIA_TYPE: {"message": "Invalid file type"}, | ||
status.HTTP_500_INTERNAL_SERVER_ERROR: { | ||
"message": "OEM Mode splash unhandled exception." | ||
}, | ||
}, | ||
) | ||
async def upload_splash_image( | ||
response: Response, | ||
file: UploadFile = File(...), | ||
settings: SystemServerSettings = Depends(get_settings), | ||
) -> Response: | ||
"""Router for /system/oem_mode/upload_splash endpoint.""" | ||
# Make sure oem mode is enabled before this request | ||
if not settings.oem_mode_enabled: | ||
raise HTTPException( | ||
status_code=status.HTTP_403_FORBIDDEN, | ||
detail="OEM Mode needs to be enabled to upload splash image.", | ||
) | ||
|
||
# Get the file info | ||
file_info = filetype.guess(file.file) | ||
if file_info is None: | ||
raise HTTPException( | ||
status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE, | ||
detail="Unable to determine file type", | ||
) | ||
|
||
# Only accept PNG files | ||
accepted_file_types = ["image/png", "png"] | ||
content_type = file_info.extension.lower() | ||
if ( | ||
file.content_type not in accepted_file_types | ||
or content_type not in accepted_file_types | ||
): | ||
raise HTTPException( | ||
status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE, | ||
detail="Unsupported file type", | ||
) | ||
|
||
file_size = 0 | ||
for chunk in file.file: | ||
file_size += len(chunk) | ||
if file_size > 5 * 1024 * 1024: # 5MB | ||
raise HTTPException( | ||
status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, | ||
detail="File is larger than 5mb.", | ||
) | ||
|
||
# TODO: Validate image dimensions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This TODO can be removed since we will no longer be validating dimensions on this side. |
||
|
||
# return the pointer back to the starting point so that the next read starts from the starting point | ||
await file.seek(0) | ||
|
||
try: | ||
# Remove the old image if exists | ||
if settings.oem_mode_splash_custom: | ||
os.unlink(settings.oem_mode_splash_custom) | ||
|
||
# file is valid, save to final location | ||
filepath = f"{settings.persistence_directory}/{file.filename}" | ||
with open(filepath, "wb+") as f: | ||
f.write(file.file.read()) | ||
|
||
# store the file location to settings and save the dotenv | ||
settings.oem_mode_splash_custom = filepath | ||
success = save_settings(settings) | ||
response.status_code = ( | ||
status.HTTP_201_CREATED if success else status.HTTP_400_BAD_REQUEST | ||
) | ||
except Exception: | ||
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR | ||
|
||
return response |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.6 KB
system-server/tests/integration/resources/oem_mode_wrong_dimensions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+34.5 KB
system-server/tests/integration/resources/oem_mode_wrong_image_type.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Status responses all make sense and are error codes in line with what would reasonably be expected on receiving end.