assets
Assets REST API ResourcesLink
Provides a collection of Flask‑RESTful resources for exposing plant‑database assets over HTTP. The module enables serving and managing files, images, point clouds, meshes, curve skeletons, sequence data, and whole‑dataset archives, making it easy to build a REST API that gives programmatic access to plant scan data.
Key FeaturesLink
- Resource classes:
File,DatasetFile,Image,PointCloud,PointCloudGroundTruth,Mesh,CurveSkeleton,Sequence,Archive. - Safety: automatic input sanitization, directory‑traversal protection, and rate‑limiting decorators on every endpoint.
- Flexible output: optional resizing, thumbnail generation, base‑64 encoding, and on‑the‑fly down‑sampling for large assets (point clouds, meshes, images).
- Archive handling: creation, validation, and extraction of ZIP archives with robust error handling and temporary‑file cleanup.
- Utility helpers:
is_within_directoryandis_directory_in_archivefor safe path checks inside the filesystem and ZIP files.
Usage ExamplesLink
Hereafter is a minimal working example that:
- Creates a
Flaskapp - Sets up a local test database with a JSON Web Token session manager
- Registers the
LoginandLogoutresources to a REST API - Starts the app
>>> import logging
>>> from flask import Flask
>>> from flask_restful import Api
>>> from plantdb.server.api.assets import FilePath
>>> from plantdb.commons.auth.session import JWTSessionManager
>>> from plantdb.commons.fsdb.core import FSDB
>>> from plantdb.commons.test_database import setup_test_database
>>> # Create a Flask application
>>> app = Flask(__name__)
>>> # Create a logger
>>> logger = logging.getLogger("plantdb.assets")
>>> logger.setLevel(logging.INFO)
>>> # Initialize a test database with a JWTSessionManager
>>> db_path = setup_test_database('real_plant')
>>> mgr = JWTSessionManager()
>>> db = FSDB(db_path, session_manager=mgr)
>>> db.connect()
>>> # RESTful API and resource registration
>>> api = Api(app)
>>> api.add_resource(File, '/files/<path:path>', resource_class_kwargs={"db": db})
>>> # Start the API
>>> app.run(host='0.0.0.0', port=5000)
It may be used as follows (in another Python REPL):
>>> import requests
>>> import toml
>>> # Request a TOML configuration file
>>> response = requests.get("http://127.0.0.1:5000/files/real_plant/scan.toml")
>>> cfg = toml.loads(response.content.decode())
>>> print(cfg['ScanPath']['class_name'])
Circle
Archive
Link
Bases: Resource
A RESTful resource class for managing dataset archives.
This class provides functionality to serve and upload dataset archives through HTTP GET and POST methods. It handles ZIP file creation, validation, and extraction while maintaining security and proper cleanup of temporary files.
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. |
logger |
Logger
|
The logger used to record operations and errors. |
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/assets.py
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 | |
get
Link
get(scan_id, **kwargs)
Create and serve a ZIP archive for the specified scan dataset.
This method creates a temporary ZIP archive containing all files from the specified scan directory (excluding 'webcache' directories) and serves it as a downloadable file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Unique identifier for the scan dataset to be archived. |
required |
Returns:
| Type | Description |
|---|---|
Response or tuple
|
If successful, returns a Flask response object with the ZIP file for download.
If unsuccessful, returns a tuple (dict, int) containing an error message and
HTTP status code |
Notes
- The scan_id is sanitized before processing
- 'webcache' directories are automatically excluded from the archive
- Temporary files are created with 'fsdb_rest_api_' prefix
- Clean-up is handled automatically after the request
Examples:
>>> import os
>>> import requests
>>> import shutil
>>> import tempfile
>>> from io import BytesIO
>>> from pathlib import Path
>>> from zipfile import ZipFile
>>> from plantdb.server.test_rest_api import TestRestApiServer
>>> # Create a test database and start the Flask App serving a REST API
>>> server = TestRestApiServer(test=True)
>>> server.start()
>>> # Get the archive for the 'real_plant' dataset with
>>> zip_file = requests.get("http://127.0.0.1:5000/archive/real_plant", stream=True)
>>> # EXAMPLE 1 - Write the archive to disk:
>>> # Create a unique temporary file name with .zip extension
>>> temp_zip_handle, temp_zip_path = tempfile.mkstemp(suffix='.zip')
>>> os.close(temp_zip_handle) # Close the file handle immediately
>>> # Write to disk
>>> with open(temp_zip_path, 'wb') as zip_f: zip_f.write(zip_file.content)
>>> print(f"Successfully wrote to {temp_zip_path}")
>>> # EXAMPLE 2 - Extract the archive:
>>> # Create a temporary path to extract the archived data
>>> tmp_dir = Path(tempfile.mkdtemp())
>>> # Open the zip file and extract non-existing files
>>> extracted_files = []
>>> with ZipFile(BytesIO(zip_file.content), 'r') as zip_obj:
... for file in zip_obj.namelist():
... file_path = tmp_dir / file
... zip_obj.extract(file, path=tmp_dir)
... extracted_files.append(file)
...
>>> # Print the list of extracted files
>>> print(extracted_files)
>>> shutil.rmtree(tmp_dir) # Remove the temporary directory (and its contents)
>>> # Stop the test server
>>> server.stop()
Source code in plantdb/server/api/assets.py
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 | |
post
Link
post(scan_id, **kwargs)
Handle ZIP file upload and extraction for a scan dataset.
This method processes an uploaded ZIP file, validates its contents and structure, and extracts it to the appropriate location in the database. It includes various security checks and ensures safe extraction of files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Unique identifier for the scan dataset where the ZIP contents will be extracted. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple containing (dict, int) where the dict contains either:
- On success: {'success': message, 'files': list_of_extracted_files}
- On failure: {'message': error_message}
The integer represents the HTTP status code ( |
Notes
- Performs the following validations:
- Checks for ZIP file presence
- Validates MIME type (must be 'application/zip')
- Verifies file extension (.zip)
- Tests ZIP file integrity
- Validates filename encodings
- Prevents path traversal attacks
- Only extracts files that don't already exist
- Automatically cleans up temporary files
Examples:
>>> import requests
>>> from pathlib import Path
>>> from tempfile import gettempdir
>>> from plantdb.server.test_rest_api import TestRestApiServer
>>> # Create a test database and start the Flask App serving a REST API
>>> server = TestRestApiServer(test=True)
>>> server.start()
>>> zip_file = Path(gettempdir()) / 'real_plant.zip' # should be in the temporary directory from the TestRestApiServer setup
>>> print(zip_file.exists())
True
>>> # You need to be logged to be able to POST archives
>>> r = requests.post('http://127.0.0.1:5000/login', json={'username': 'admin', 'password': 'admin'})
>>> jwt_token = r.json()['access_token'] # get the JSON Web Token
>>> # Upload it as a new dataset named 'real_plant_test'
>>> new_dataset = 'real_plant_test'
>>> with open(zip_file, 'rb') as zip_f:
... files = {'zip_file': (str(zip_file), zip_f, 'application/zip')}
... response = requests.post(f'http://127.0.0.1:5000/archive/{new_dataset}', files=files, headers={'Authorization': 'Bearer ' + jwt_token})
>>> print(response.json())
>>> _ = requests.get(f"http://127.0.0.1:5000/refresh?scan_id={new_dataset}")
>>> r = requests.get("http://127.0.0.1:5000/scans")
>>> scans_list = r.json()
>>> print(new_dataset in scans_list)
True
>>> server.stop()
Source code in plantdb/server/api/assets.py
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 | |
CurveSkeleton
Link
Bases: Resource
A RESTful resource that provides access to curve skeleton data for plant scans.
This class implements a REST API endpoint that serves curve skeleton data stored in JSON format. It handles GET requests to retrieve skeleton data for a specific scan ID.
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. |
logger |
Logger
|
The logger instance for this resource. |
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/assets.py
968 969 970 971 972 973 974 975 976 977 978 979 | |
get
Link
get(scan_id, **kwargs)
Retrieve the curve skeleton data for a specific scan.
This method handles GET requests to fetch curve skeleton data. It performs validation of the scan ID, retrieves the appropriate fileset, and returns the skeleton data in JSON format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Identifier for the plant scan to retrieve skeleton data for. Must contain only alphanumeric characters, underscores, dashes, or periods. |
required |
Returns:
| Type | Description |
|---|---|
Union[dict, Tuple[dict, int]]
|
On success: Dictionary containing the curve skeleton data On failure: Tuple of (error_dict, http_status_code) |
Raises:
| Type | Description |
|---|---|
ScanNotFoundError
|
If the requested scan ID doesn't exist in the database |
FilesetNotFoundError
|
If the CurveSkeleton fileset is not found for the scan |
FileNotFoundError
|
If the CurveSkeleton file is missing from the fileset |
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
Notes
- The scan_id is sanitized before processing to ensure security
- Returns HTTP 400 status code for all error conditions with appropriate error messages
- The skeleton data is expected to be in JSON format in the database
Examples:
>>> # Start the REST API server
>>> # Then in a Python console:
>>> import requests
>>> # Fetch skeleton data for a valid scan
>>> response = requests.get("http://127.0.0.1:5000/skeleton/Col-0_E1_1")
>>> skeleton_data = response.json()
>>> print(list(skeleton_data.keys()))
['angles', 'internodes', 'metadata']
>>> # Example with invalid scan ID
>>> response = requests.get("http://127.0.0.1:5000/skeleton/invalid_id")
>>> print(response.status_code)
400
>>> print(response.json())
{'message': "Scan 'invalid_id' not found!"}
Source code in plantdb/server/api/assets.py
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 | |
DatasetFile
Link
Bases: Resource
A RESTful resource handler for file upload operations in a plant database system.
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. Used for validating scan IDs and determining file storage paths. |
logger |
Logger
|
The logger used to record operations and errors. |
Notes
File operations are performed with proper error handling and cleanup of partial uploads in case of failures.
See Also
plantdb.server.api.scan.ScansList : Resource for managing scan listings plantdb.server.api.scan.File : Resource for file retrieval operations
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/assets.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
post
Link
post(scan_id, **kwargs)
Handle POST request to upload and save a file to the server.
This endpoint processes file uploads and saves them to the specified location. It supports both full file uploads and chunked uploads based on the provided headers. The method ensures data integrity by validating the received file size against the Content-Length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Unique identifier for the scan associated with the file upload. Used to determine the base storage path for the file. |
required |
Returns:
| Type | Description |
|---|---|
Response
|
JSON response with status code and message:
|
Notes
Required HTTP headers:
- 'Content-Disposition': Contains file information
- 'Content-Length': Size of the file in bytes
- 'X-File-Path': Relative path where the file should be saved
- 'X-Chunk-Size' (optional): Size of chunks for streamed upload
The method will automatically create any necessary directories in the path. Partial uploads are automatically cleaned up if they fail.
Raises:
| Type | Description |
|---|---|
Exception
|
When database access fails or file operations encounter errors. All exceptions are caught and returned as HTTP 400 or 500 responses. |
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
See Also
plantdb.commons.io.write_stream plantdb.commons.io.write_data
Source code in plantdb/server/api/assets.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
FilePath
Link
Bases: Resource
A RESTful resource class for serving files via HTTP GET requests.
This class implements a REST API endpoint that serves files from a specified database location.
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. |
logger |
Logger
|
The logger used to record operations and errors. |
Notes
The class requires proper initialization with a database instance that provides a valid path() method for file location resolution.
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/assets.py
164 165 166 167 168 169 170 171 172 173 174 175 | |
get
Link
get(path)
Serve a file from the database directory via HTTP.
This method handles GET requests by serving the requested file from
the configured database directory. It uses Flask's send_from_directory
to safely serve the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Relative path to the requested file within the database directory. This path will be resolved against the database root path. |
required |
Returns:
| Type | Description |
|---|---|
Response
|
A Flask response object containing the requested file or an appropriate error response if the file is not found. |
Raises:
| Type | Description |
|---|---|
NotFound
|
If the requested file does not exist. |
Forbidden
|
If the file access is forbidden. |
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
Notes
The file serving is handled securely through Flask's send_from_directory,
which prevents directory traversal attacks and handles file access permissions.
Examples:
>>> # Start the REST API server (in test mode)
>>> # fsdb_rest_api --test
>>> import requests
>>> import toml
>>> # Request a TOML configuration file
>>> response = requests.get("http://127.0.0.1:5000/files/real_plant_analyzed/pipeline.toml")
>>> cfg = toml.loads(response.content.decode())
>>> print(cfg['Undistorted'])
{'upstream_task': 'ImagesFilesetExists'}
>>> # Request a JSON file
>>> response = requests.get("http://127.0.0.1:5000/files/real_plant_analyzed/files.json")
>>> scan_files = response.json()
>>> print([fs['id'] for fs in scan_files['filesets']])
['images', 'AnglesAndInternodes_1_0_2_0_6_0_6dd64fc595', 'TreeGraph__False_CurveSkeleton_c304a2cc71', 'CurveSkeleton__TriangleMesh_0393cb5708', 'TriangleMesh_9_most_connected_t_open3d_00e095c359', 'PointCloud_1_0_1_0_10_0_7ee836e5a9', 'Voxels___x____300__450__colmap_camera_False_2a093f0ccc', 'Masks_1__0__1__0____channel____rgb_5619aa428d', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7']
Source code in plantdb/server/api/assets.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
Image
Link
Bases: Resource
RESTful resource for serving and resizing images on demand.
This class handles HTTP GET requests for images stored in the database, with optional resizing capabilities. It serves both original and thumbnail versions of images based on the request parameters.
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. |
logger |
Logger
|
The logger used to record operations and errors. |
Notes
The class sanitizes all input parameters to prevent path traversal attacks and ensure valid file access.
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/assets.py
389 390 391 392 393 394 395 396 397 398 399 400 | |
get
Link
get(scan_id, fileset_id, file_id, **kwargs)
Retrieve and serve an image from the database.
Handles image retrieval requests, optionally resizing the image based on the 'size' query parameter. Supports both original size and thumbnail versions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Identifier for the scan containing the image. |
required |
|
str
|
Identifier for the fileset within the scan. |
required |
|
str
|
Identifier for the specific image file. |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
size |
str or float
|
Query parameter controlling downsampling.
Accepted values:
* |
as_base64 |
str
|
Query parameter indicating whether to return the image encoded in base64. Accepts 'true', '1', 'yes' (case‑insensitive) to enable. Defaults to 'false', which streams the image file. If set, returns the image in base64 under the 'image' JSON dictionary entry and mimetype under 'content-type'. |
Returns:
| Type | Description |
|---|---|
Response
|
HTTP response containing the image data with 'content-type' mimetype. |
Raises:
| Type | Description |
|---|---|
NotFound
|
If the requested image file doesn't exist. |
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
See Also
plantdb.server.webcache.image_path : Image path resolution function with caching and resizing options.
Examples:
>>> # In a terminal, start a (test) REST API with `fsdb_rest_api --test`, then:
>>> import numpy as np
>>> import requests
>>> import pybase64
>>> from io import BytesIO
>>> from PIL import Image
>>> # Example #1 - Get the first image as a thumbnail (default):
>>> response = requests.get("http://127.0.0.1:5000/image/real_plant_analyzed/images/00000_rgb", stream=True)
>>> img = Image.open(BytesIO(response.content))
>>> image.show()
>>> np.asarray(img).shape
(113, 150, 3)
>>> # Example #2 - Get the first image in original size:
>>> response = requests.get("http://127.0.0.1:5000/image/real_plant_analyzed/images/00000_rgb", stream=True, params={"size": "orig"})
>>> img = Image.open(BytesIO(response.content))
>>> image.show()
>>> np.asarray(img).shape
(1080, 1440, 3)
>>> # Example #3 - Get a base64 encoded image:
>>> response = requests.get("http://127.0.0.1:5000/image/real_plant_analyzed/images/00000_rgb", stream=True, params={"size": "orig", "as_base64": 'true'})
>>> print(response.json()['content-type'])
'image/jpeg'
>>> b64_string = response.json()['image']
>>> print(b64_string[:30]) # print the first 30 characters
'/9j/4AAQSkZJRgABAQAAAQABAAD/2w'
>>> image_data = pybase64.b64decode(b64_string)
>>> image = Image.open(BytesIO(image_data))
>>> image.show()
Source code in plantdb/server/api/assets.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | |
wants_base64
staticmethod
Link
wants_base64(request)
Return True when the query string contains as_base64 with a truthy value.
Source code in plantdb/server/api/assets.py
402 403 404 405 406 407 408 | |
Mesh
Link
Bases: Resource
RESTful resource for serving triangular mesh data via HTTP.
This class implements a REST endpoint that provides access to triangular mesh data stored in a database. It supports GET requests and can optionally handle mesh size parameters.
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. |
logger |
Logger
|
The logger used to record operations and errors. |
Notes
The mesh data is served in PLY format as an octet-stream.
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/assets.py
836 837 838 839 840 841 842 843 844 845 846 847 | |
get
Link
get(scan_id, fileset_id, file_id, **kwargs)
Retrieve and serve a triangular mesh file.
This method handles GET requests for mesh data, supporting optional size parameters. It sanitizes input parameters and serves the mesh file from the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Identifier for the scan containing the mesh. |
required |
|
str
|
Identifier for the fileset within the scan. |
required |
|
str
|
Identifier for the specific mesh file. |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
coords |
str
|
Query parameter indicating whether to return the vertices coordinates and triangle IDs as JSON. Accepts 'true', '1', 'yes' (case‑insensitive) to enable. Defaults to 'false', which streams the PLY file. If set, returns the data as list under the 'vertices' & 'triangles' JSON dictionary entry. |
Returns:
| Type | Description |
|---|---|
Response
|
HTTP response containing the mesh data as an octet-stream. |
Raises:
| Type | Description |
|---|---|
NotFound
|
If the requested mesh file doesn't exist |
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
Notes
- In the URL, you can use the
sizeparameter to retrieve a resized mesh. - The 'size' parameter currently only supports 'orig' value
- All identifiers are sanitized before use
- The mesh is served as a binary PLY file
See Also
plantdb.server.core.security.sanitize_name : Function used to validate input parameters plantdb.server.webcache.mesh_path : Function to retrieve mesh file path
Examples:
>>> # In a terminal, start a (test) REST API with `fsdb_rest_api --test`, then:
>>> import requests
>>> from plyfile import PlyData
>>> from io import BytesIO
>>> # Request a mesh file
>>> url = "http://127.0.0.1:5000/mesh/real_plant_analyzed/TriangleMesh_9_most_connected_t_open3d_00e095c359/TriangleMesh"
>>> response = requests.get(url)
>>> # Parse the PLY data
>>> mesh_data = PlyData.read(BytesIO(response.content))
>>> # Access vertex coordinates
>>> vertices = mesh_data['vertex']
>>> x_coords = list(vertices['x'])
Source code in plantdb/server/api/assets.py
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 | |
PointCloud
Link
Bases: Resource
RESTful resource for serving and optionally downsampling point cloud data.
This class handles HTTP GET requests for point cloud data stored in PLY format, with support for different sampling densities. It can serve both original and preview versions of point clouds, or custom downsampling based on voxel size.
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. |
logger |
Logger
|
The logger used to record operations and errors. |
Notes
The class sanitizes all input parameters to prevent path traversal attacks and ensures valid file access. Point clouds are served in PLY format with 'application/octet-stream' mimetype.
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/assets.py
556 557 558 559 560 561 562 563 564 565 566 567 | |
get
Link
get(scan_id, fileset_id, file_id, **kwargs)
Retrieve and serve a point cloud from the database.
Handles point cloud retrieval requests with optional downsampling based on the 'size' query parameter. Supports original size, preview, and custom voxel-based downsampling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Identifier for the scan containing the point cloud. |
required |
|
str
|
Identifier for the fileset within the scan. |
required |
|
str
|
Identifier for the specific point cloud file. |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
size |
str or float
|
Query parameter controlling downsampling. Accepted values: * 'orig' - serve the original point cloud. * 'preview' - serve a precomputed preview (default). * A float value - perform on‑the‑fly voxel downsampling using the specified voxel size. If an invalid string is supplied, the default 'preview' is used. |
coords |
str
|
Query parameter indicating whether to return the point coordinates as JSON. Accepts 'true', '1', 'yes' (case‑insensitive) to enable. Defaults to 'false', which streams the PLY file. If set, returns the data as list under the 'coordinates' JSON dictionary entry. |
Returns:
| Type | Description |
|---|---|
Response
|
HTTP response containing the PLY data with 'application/octet-stream' mimetype. |
Raises:
| Type | Description |
|---|---|
NotFound
|
If the requested point-cloud file doesn't exist. |
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
Notes
- All input parameters are sanitized before use
See Also
plantdb.server.core.security.sanitize_name : Input sanitization & validation function. plantdb.server.webcache.pointcloud_path : Point cloud path resolution function with caching and downsampling options.
Examples:
>>> # In a terminal, start a (test) REST API with `fsdb_rest_api --test`, then:
>>> import requests
>>> from plyfile import PlyData
>>> from io import BytesIO
>>> # Get original point cloud:
>>> response = requests.get("http://127.0.0.1:5000/pointcloud/real_plant_analyzed/PointCloud_1_0_1_0_10_0_7ee836e5a9/PointCloud")
>>> pcd_data = PlyData.read(BytesIO(response.content))
>>> # Access point X-coordinates:
>>> list(pcd_data['vertex']['x'])
>>> # Get preview (downsampled) version
>>> response = requests.get("http://127.0.0.1:5000/pointcloud/real_plant_analyzed/PointCloud_1_0_1_0_10_0_7ee836e5a9/PointCloud", params={"size": "preview"})
>>> # Get custom downsampled version (voxel size 0.01)
>>> response = requests.get("http://127.0.0.1:5000/pointcloud/real_plant_analyzed/PointCloud_1_0_1_0_10_0_7ee836e5a9/PointCloud", params={"size": "0.01"})
>>> # Send the coordinates (read the file on the server-side)
>>> response = requests.get("http://127.0.0.1:5000/pointcloud/real_plant_analyzed/PointCloud_1_0_1_0_10_0_7ee836e5a9/PointCloud", params={"size": "preview", 'coords': 'true'})
>>> coordinates = np.array(response.json()['coordinates'])
Source code in plantdb/server/api/assets.py
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 | |
PointCloudGroundTruth
Link
Bases: Resource
A RESTful resource for serving ground-truth point-cloud data.
This class handles HTTP GET requests for point-cloud data, with optional downsampling capabilities based on the requested size parameter.
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. |
logger |
Logger
|
The logger instance for this resource. |
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/assets.py
703 704 705 706 707 708 709 710 711 712 713 714 | |
get
Link
get(scan_id, fileset_id, file_id, **kwargs)
Retrieve and serve a ground-truth point-cloud file.
Fetches the requested point-cloud data from the cache, potentially downsampling it based on the size parameter provided in the query string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Identifier for the scan to retrieve. |
required |
|
str
|
Identifier for the fileset within the scan. |
required |
|
str
|
Identifier for the specific point-cloud file. |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
size |
str or float
|
Query parameter controlling downsampling. Accepted values: * 'orig' - serve the original point cloud. * 'preview' - serve a precomputed preview (default). * A float value - perform on‑the‑fly voxel downsampling using the specified voxel size. If an invalid string is supplied, the default 'preview' is used. |
coords |
str
|
Query parameter indicating whether to return the point coordinates as JSON. Accepts 'true', '1', 'yes' (case‑insensitive) to enable. Defaults to 'false', which streams the PLY file. If set, returns the data as list under the 'coordinates' JSON dictionary entry. |
Returns:
| Type | Description |
|---|---|
Response
|
HTTP response containing the point-cloud data as an octet-stream. |
Raises:
| Type | Description |
|---|---|
NotFound
|
If the requested point-cloud file doesn't exist. |
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
Notes
- In the URL, you can use the 'size' parameter to specify the size of the point-cloud:
- 'orig': Original size
- 'preview': Preview size (default)
- A float value: Custom voxel size for downsampling
- All identifiers are sanitized before use
- Invalid size parameters default to 'preview'
- Response mimetype is 'application/octet-stream'
Source code in plantdb/server/api/assets.py
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 | |
Sequence
Link
Bases: Resource
A RESTful resource class that serves angle and internode sequences data.
This class provides a REST API endpoint to retrieve angle and internode sequence data for plant scans. It handles data retrieval from a database and supports filtering by sequence type (angles, internodes, or fruit_points).
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. |
logger |
Logger
|
The logger instance for this resource. |
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/assets.py
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 | |
get
Link
get(scan_id, **kwargs)
Retrieve angle and internode sequences data for a given scan.
This method serves as a REST API endpoint to fetch angle, internode, and fruit point sequence data from plant scans. It can return either all sequence data or specific sequence types based on the query parameter 'type'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Unique identifier for the plant scan. Must contain only alphanumeric characters, underscores, dashes, or periods. |
required |
Returns:
| Type | Description |
|---|---|
Union[dict, list, tuple[dict, int]]
|
If successful and type='all' (default): Dictionary containing all sequence data with the following keys: 'angles', 'internodes', 'fruit_points', 'manual_angles', 'manual_internodes' If successful and type in ['angles', 'internodes', 'fruit_points', 'manual_angles', 'manual_internodes']: List of sequence values for the specified type If error: Tuple of (error_dict, HTTP_status_code) |
Raises:
| Type | Description |
|---|---|
ScanNotFoundError
|
If the specified scan_id does not exist in the database |
FilesetNotFoundError
|
If the AnglesAndInternodes fileset is not found |
FileNotFoundError
|
If the AnglesAndInternodes file is not found within the fileset |
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
Notes
- The 'type' query parameter accepts 'angles', 'internodes', or 'fruit_points'
- Invalid 'type' parameters will return the complete data dictionary
- All responses are JSON-encoded
- Input scan_id is sanitized before processing
See Also
plantdb.server.core.security.sanitize_name : Function used to validate and clean scan_id plantdb.server.rest_api.compute_fileset_matches : Function to match filesets with tasks
Examples:
>>> # Get all sequence data
>>> import requests
>>> response = requests.get("http://127.0.0.1:5000/sequence/real_plant_analyzed")
>>> data = response.json() # Expected output: {'angles': [...], 'internodes': [...], 'fruit_points': [...]}
>>> print(list(data))
['angles', 'internodes', 'fruit_points', 'manual_angles', 'manual_internodes']
>>> # Get only angles data
>>> response = requests.get("http://127.0.0.1:5000/sequence/real_plant_analyzed", params={'type': 'angles'})
>>> angles = response.json()
>>> print(angles[:5])
[47.13015345294241, 239.43543078022594, 311.8816488465762, 251.0289289739646, 249.56560354730826]
Source code in plantdb/server/api/assets.py
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 | |
is_directory_in_archive
Link
is_directory_in_archive(archive_path, target_dir)
Check if a specific directory exists within an archive file.
This function checks whether a given directory is present at the top level of a ZIP archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str or Path
|
The path to the ZIP archive file. |
required |
|
str
|
The name of the target directory to check for within the archive. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the target directory exists at the top level of the archive, False otherwise. |
Source code in plantdb/server/api/assets.py
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 | |
is_within_directory
Link
Check if a target path is within a directory.
This function determines if the absolute path of the target is located
within the absolute path of the directory. It uses os.path.commonpath
to perform the comparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str or Path
|
The path to the directory to check against. |
required |
|
str or Path
|
The path to the target to check if it resides within the directory. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/server/api/assets.py
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 | |