Skip to content

Commit 5d12d99

Browse files
committed
Serve screenshot immediately.
1 parent 6b6e1fb commit 5d12d99

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

server/server.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,11 @@ class ScreenshotResource(Resource):
249249
@ensure_automator_healthy
250250
@handle_automator_response(server)
251251
def get(self):
252-
"""Get current page screenshot and return a URL to access it"""
252+
"""Get current page screenshot and return a URL to access it or display it directly"""
253253
try:
254+
# Check if save parameter is provided
255+
save = request.args.get('save', '0') == '1'
256+
254257
# Generate a unique filename with timestamp to avoid caching issues
255258
timestamp = int(time.time())
256259
filename = f"current_screen_{timestamp}.png"
@@ -259,11 +262,25 @@ def get(self):
259262
# Take the screenshot
260263
server.automator.driver.save_screenshot(screenshot_path)
261264

262-
# Return the URL to access the screenshot via the /image endpoint
263-
# The POST method is used to serve the image without deleting it
264-
image_url = f"/image/{os.path.splitext(filename)[0]}"
265+
# If save=1, return the URL to access the screenshot via the /image endpoint with POST method
266+
# Otherwise, return the image directly via GET method (which will delete it after serving)
267+
image_id = os.path.splitext(filename)[0]
268+
269+
if save:
270+
# Return URL to access the screenshot (POST method preserves the image)
271+
image_url = f"/image/{image_id}"
272+
return {"screenshot_url": image_url}, 200
273+
else:
274+
# Display the image directly (GET method will delete after serving)
275+
response = send_file(screenshot_path, mimetype="image/png")
276+
# Delete the file after serving
277+
try:
278+
os.remove(screenshot_path)
279+
logger.info(f"Deleted screenshot: {screenshot_path}")
280+
except Exception as e:
281+
logger.error(f"Failed to delete screenshot {screenshot_path}: {e}")
282+
return response, 200
265283

266-
return {"screenshot_url": image_url}, 200
267284
except Exception as e:
268285
logger.error(f"Error getting screenshot: {e}")
269286
logger.error(f"Traceback: {traceback.format_exc()}")

0 commit comments

Comments
 (0)