Skip to content

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_directory and is_directory_in_archive for safe path checks inside the filesystem and ZIP files.

Usage ExamplesLink

Hereafter is a minimal working example that:

  1. Creates a Flask app
  2. Sets up a local test database with a JSON Web Token session manager
  3. Registers the Login and Logout resources to a REST API
  4. 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

Archive(db, logger=None)

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

db Link

FSDB

A database instance providing the resources to serve.

required

logger Link

Logger

A logger instance to record operations and errors.

None
Source code in plantdb/server/api/assets.py
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
def __init__(self, db, logger=None):
    """Initialize the resource.

    Parameters
    ----------
    db : plantdb.commons.fsdb.core.FSDB
        A database instance providing the resources to serve.
    logger : logging.Logger
        A logger instance to record operations and errors.
    """
    self.db: FSDB = db
    self.logger: logging.Logger = (
        logger if logger else get_logger(self.__class__.__name__)
    )

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

scan_id Link

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 400.

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:

>>> # Start the REST API server (in test mode)
>>> 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()
>>> import os
>>> import requests
>>> import shutil
>>> import tempfile
>>> from io import BytesIO
>>> from pathlib import Path
>>> from zipfile import ZipFile
>>> from plantdb.commons import api_endpoints
>>> file_uri = api_endpoints.archive('real_plant_analyzed')
>>> # Get the archive for the 'real_plant' dataset with
>>> zip_file = requests.get("http://127.0.0.1:5000" + file_uri, 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}")
Successfully wrote to /tmp/tmpmogtl78q.zip
>>> # 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)
['measures.json', 'pipeline.toml', 'files.json', 'scan.toml', 'TriangleMesh_9_most_connected_t_open3d_00e095c359/TriangleMesh.ply', 'PointCloud_1_0_1_0_10_0_7ee836e5a9/PointCloud.ply', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00030_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00016_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00018_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00057_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00048_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00027_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00051_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00006_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00022_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00003_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00017_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00004_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00059_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00035_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00005_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00046_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00001_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00026_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00054_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00031_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00025_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00007_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00028_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00010_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00008_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00042_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00000_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00036_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00041_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00015_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00024_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00052_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00053_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00009_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00002_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00039_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00034_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00019_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00020_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00044_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00047_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00056_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00029_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00049_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00045_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00021_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00037_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00032_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00011_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00058_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00012_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00038_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00050_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00055_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00040_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00043_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00014_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00023_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00033_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00013_rgb.png', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d.json', 'metadata/metadata.json', 'metadata/CurveSkeleton__TriangleMesh_0393cb5708.json', 'metadata/PointCloud_1_0_1_0_10_0_7ee836e5a9.json', 'metadata/Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc.json', 'metadata/TreeGraph__False_CurveSkeleton_c304a2cc71.json', 'metadata/AnglesAndInternodes_1_0_2_0_6_0_6dd64fc595.json', 'metadata/images.json', 'metadata/Voxels___x____300__450__colmap_camera_False_2a093f0ccc.json', 'metadata/TriangleMesh_9_most_connected_t_open3d_00e095c359.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7.json', 'metadata/PointCloud_1_0_1_0_10_0_7ee836e5a9/PointCloud.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00034_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00040_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00021_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00056_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00029_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00050_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00018_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00039_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00017_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00043_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00025_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00016_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00047_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00033_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00051_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00053_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00005_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00048_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00031_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00042_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00008_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00002_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00013_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00000_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00014_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00035_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00023_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00027_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00044_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00041_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00010_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00001_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00045_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00007_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00052_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00020_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00049_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00015_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00028_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00004_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00054_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00037_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00038_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00003_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00059_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00032_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00022_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00057_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00012_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00026_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00030_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00055_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00024_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00058_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00011_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00046_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00009_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00036_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00006_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00019_rgb.json', 'metadata/images/00034_rgb.json', 'metadata/images/00040_rgb.json', 'metadata/images/00021_rgb.json', 'metadata/images/00056_rgb.json', 'metadata/images/00029_rgb.json', 'metadata/images/00050_rgb.json', 'metadata/images/00018_rgb.json', 'metadata/images/00039_rgb.json', 'metadata/images/00017_rgb.json', 'metadata/images/00043_rgb.json', 'metadata/images/00025_rgb.json', 'metadata/images/00016_rgb.json', 'metadata/images/00047_rgb.json', 'metadata/images/00033_rgb.json', 'metadata/images/00051_rgb.json', 'metadata/images/00053_rgb.json', 'metadata/images/00005_rgb.json', 'metadata/images/00048_rgb.json', 'metadata/images/00031_rgb.json', 'metadata/images/00042_rgb.json', 'metadata/images/00008_rgb.json', 'metadata/images/00002_rgb.json', 'metadata/images/00013_rgb.json', 'metadata/images/00000_rgb.json', 'metadata/images/00014_rgb.json', 'metadata/images/00035_rgb.json', 'metadata/images/00023_rgb.json', 'metadata/images/00027_rgb.json', 'metadata/images/00044_rgb.json', 'metadata/images/00041_rgb.json', 'metadata/images/00010_rgb.json', 'metadata/images/00001_rgb.json', 'metadata/images/00045_rgb.json', 'metadata/images/00007_rgb.json', 'metadata/images/00052_rgb.json', 'metadata/images/00020_rgb.json', 'metadata/images/00049_rgb.json', 'metadata/images/00015_rgb.json', 'metadata/images/00028_rgb.json', 'metadata/images/00004_rgb.json', 'metadata/images/00054_rgb.json', 'metadata/images/00037_rgb.json', 'metadata/images/00038_rgb.json', 'metadata/images/00003_rgb.json', 'metadata/images/00059_rgb.json', 'metadata/images/00032_rgb.json', 'metadata/images/00022_rgb.json', 'metadata/images/00057_rgb.json', 'metadata/images/00012_rgb.json', 'metadata/images/00026_rgb.json', 'metadata/images/00030_rgb.json', 'metadata/images/00055_rgb.json', 'metadata/images/00024_rgb.json', 'metadata/images/00058_rgb.json', 'metadata/images/00011_rgb.json', 'metadata/images/00046_rgb.json', 'metadata/images/00009_rgb.json', 'metadata/images/00036_rgb.json', 'metadata/images/00006_rgb.json', 'metadata/images/00019_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00034_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00040_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00021_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00056_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00029_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00050_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00018_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00039_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00017_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00043_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00025_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00016_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00047_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00033_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00051_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00053_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00005_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00048_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00031_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00042_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00008_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00002_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00013_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00000_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00014_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00035_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00023_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00027_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00044_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00041_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00010_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00001_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00045_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00007_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00052_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00020_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00049_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00015_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00028_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00004_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00054_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00037_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00038_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00003_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00059_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00032_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00022_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00057_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00012_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00026_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00030_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00055_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00024_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00058_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00011_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00046_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00009_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00036_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00006_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00019_rgb.json', 'metadata/Voxels___x____300__450__colmap_camera_False_2a093f0ccc/Voxels.json', 'TreeGraph__False_CurveSkeleton_c304a2cc71/TreeGraph.p', 'images/00013_rgb.jpg', 'images/00010_rgb.jpg', 'images/00020_rgb.jpg', 'images/00028_rgb.jpg', 'images/00035_rgb.jpg', 'images/00054_rgb.jpg', 'images/00030_rgb.jpg', 'images/00018_rgb.jpg', 'images/00029_rgb.jpg', 'images/00005_rgb.jpg', 'images/00004_rgb.jpg', 'images/00038_rgb.jpg', 'images/00047_rgb.jpg', 'images/00053_rgb.jpg', 'images/00014_rgb.jpg', 'images/00021_rgb.jpg', 'images/00037_rgb.jpg', 'images/00057_rgb.jpg', 'images/00007_rgb.jpg', 'images/00016_rgb.jpg', 'images/00050_rgb.jpg', 'images/00036_rgb.jpg', 'images/00017_rgb.jpg', 'images/00052_rgb.jpg', 'images/00009_rgb.jpg', 'images/00031_rgb.jpg', 'images/00002_rgb.jpg', 'images/00059_rgb.jpg', 'images/00027_rgb.jpg', 'images/00015_rgb.jpg', 'images/00032_rgb.jpg', 'images/00023_rgb.jpg', 'images/00000_rgb.jpg', 'images/00056_rgb.jpg', 'images/00033_rgb.jpg', 'images/00048_rgb.jpg', 'images/00019_rgb.jpg', 'images/00039_rgb.jpg', 'images/00044_rgb.jpg', 'images/00040_rgb.jpg', 'images/00022_rgb.jpg', 'images/00003_rgb.jpg', 'images/00055_rgb.jpg', 'images/00034_rgb.jpg', 'images/00049_rgb.jpg', 'images/00045_rgb.jpg', 'images/00043_rgb.jpg', 'images/00051_rgb.jpg', 'images/00011_rgb.jpg', 'images/00012_rgb.jpg', 'images/00024_rgb.jpg', 'images/00025_rgb.jpg', 'images/00046_rgb.jpg', 'images/00001_rgb.jpg', 'images/00042_rgb.jpg', 'images/00006_rgb.jpg', 'images/00041_rgb.jpg', 'images/00008_rgb.jpg', 'images/00026_rgb.jpg', 'images/00058_rgb.jpg', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/colmap.log', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/cnc_vs_colmap_poses_estimated.png', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/points3d.json', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/images.json', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/model_analyzer.log', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/sparse.ply', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/cameras.json', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/euclidean_distances.json', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00030_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00016_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00018_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00057_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00048_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00027_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00051_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00006_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00022_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00003_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00017_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00004_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00059_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00035_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00005_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00046_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00001_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00026_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00054_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00031_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00025_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00007_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00028_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00010_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00008_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00042_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00000_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00036_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00041_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00015_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00024_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00052_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00053_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00009_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00002_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00039_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00034_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00019_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00020_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00044_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00047_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00056_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00029_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00049_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00045_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00021_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00037_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00032_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00011_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00058_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00012_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00038_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00050_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00055_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00040_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00043_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00014_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00023_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00033_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00013_rgb.png', 'Voxels___x____300__450__colmap_camera_False_2a093f0ccc/Voxels.tiff', 'AnglesAndInternodes_1_0_2_0_6_0_6dd64fc595/AnglesAndInternodes.json', 'AnglesAndInternodes_1_0_2_0_6_0_6dd64fc595/fruit_direction.json', 'AnglesAndInternodes_1_0_2_0_6_0_6dd64fc595/stem_direction.json', 'CurveSkeleton__TriangleMesh_0393cb5708/CurveSkeleton.json']
>>> 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
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
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
@sanitize_ids("scan_id")
@rate_limit(max_requests=5, window_seconds=60)
@add_jwt_from_header
@use_guest_as_default  # FIXME: Remove this if we want strict token identification
def get(self, 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
    ----------
    scan_id : str
        Unique identifier for the scan dataset to be archived.

    Returns
    -------
    flask.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 ``400``.

    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
    --------
    >>> # Start the REST API server (in test mode)
    >>> 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()

    >>> import os
    >>> import requests
    >>> import shutil
    >>> import tempfile
    >>> from io import BytesIO
    >>> from pathlib import Path
    >>> from zipfile import ZipFile
    >>> from plantdb.commons import api_endpoints
    >>> file_uri = api_endpoints.archive('real_plant_analyzed')
    >>> # Get the archive for the 'real_plant' dataset with
    >>> zip_file = requests.get("http://127.0.0.1:5000" + file_uri, 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}")
    Successfully wrote to /tmp/tmpmogtl78q.zip
    >>> # 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)
    ['measures.json', 'pipeline.toml', 'files.json', 'scan.toml', 'TriangleMesh_9_most_connected_t_open3d_00e095c359/TriangleMesh.ply', 'PointCloud_1_0_1_0_10_0_7ee836e5a9/PointCloud.ply', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00030_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00016_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00018_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00057_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00048_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00027_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00051_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00006_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00022_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00003_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00017_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00004_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00059_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00035_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00005_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00046_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00001_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00026_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00054_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00031_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00025_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00007_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00028_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00010_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00008_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00042_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00000_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00036_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00041_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00015_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00024_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00052_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00053_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00009_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00002_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00039_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00034_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00019_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00020_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00044_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00047_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00056_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00029_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00049_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00045_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00021_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00037_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00032_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00011_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00058_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00012_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00038_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00050_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00055_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00040_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00043_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00014_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00023_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00033_rgb.png', 'Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00013_rgb.png', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d.json', 'metadata/metadata.json', 'metadata/CurveSkeleton__TriangleMesh_0393cb5708.json', 'metadata/PointCloud_1_0_1_0_10_0_7ee836e5a9.json', 'metadata/Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc.json', 'metadata/TreeGraph__False_CurveSkeleton_c304a2cc71.json', 'metadata/AnglesAndInternodes_1_0_2_0_6_0_6dd64fc595.json', 'metadata/images.json', 'metadata/Voxels___x____300__450__colmap_camera_False_2a093f0ccc.json', 'metadata/TriangleMesh_9_most_connected_t_open3d_00e095c359.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7.json', 'metadata/PointCloud_1_0_1_0_10_0_7ee836e5a9/PointCloud.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00034_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00040_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00021_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00056_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00029_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00050_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00018_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00039_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00017_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00043_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00025_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00016_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00047_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00033_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00051_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00053_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00005_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00048_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00031_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00042_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00008_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00002_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00013_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00000_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00014_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00035_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00023_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00027_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00044_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00041_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00010_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00001_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00045_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00007_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00052_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00020_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00049_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00015_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00028_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00004_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00054_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00037_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00038_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00003_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00059_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00032_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00022_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00057_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00012_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00026_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00030_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00055_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00024_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00058_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00011_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00046_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00009_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00036_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00006_rgb.json', 'metadata/Undistorted_SIMPLE_RADIAL_Colmap__a333f181b7/00019_rgb.json', 'metadata/images/00034_rgb.json', 'metadata/images/00040_rgb.json', 'metadata/images/00021_rgb.json', 'metadata/images/00056_rgb.json', 'metadata/images/00029_rgb.json', 'metadata/images/00050_rgb.json', 'metadata/images/00018_rgb.json', 'metadata/images/00039_rgb.json', 'metadata/images/00017_rgb.json', 'metadata/images/00043_rgb.json', 'metadata/images/00025_rgb.json', 'metadata/images/00016_rgb.json', 'metadata/images/00047_rgb.json', 'metadata/images/00033_rgb.json', 'metadata/images/00051_rgb.json', 'metadata/images/00053_rgb.json', 'metadata/images/00005_rgb.json', 'metadata/images/00048_rgb.json', 'metadata/images/00031_rgb.json', 'metadata/images/00042_rgb.json', 'metadata/images/00008_rgb.json', 'metadata/images/00002_rgb.json', 'metadata/images/00013_rgb.json', 'metadata/images/00000_rgb.json', 'metadata/images/00014_rgb.json', 'metadata/images/00035_rgb.json', 'metadata/images/00023_rgb.json', 'metadata/images/00027_rgb.json', 'metadata/images/00044_rgb.json', 'metadata/images/00041_rgb.json', 'metadata/images/00010_rgb.json', 'metadata/images/00001_rgb.json', 'metadata/images/00045_rgb.json', 'metadata/images/00007_rgb.json', 'metadata/images/00052_rgb.json', 'metadata/images/00020_rgb.json', 'metadata/images/00049_rgb.json', 'metadata/images/00015_rgb.json', 'metadata/images/00028_rgb.json', 'metadata/images/00004_rgb.json', 'metadata/images/00054_rgb.json', 'metadata/images/00037_rgb.json', 'metadata/images/00038_rgb.json', 'metadata/images/00003_rgb.json', 'metadata/images/00059_rgb.json', 'metadata/images/00032_rgb.json', 'metadata/images/00022_rgb.json', 'metadata/images/00057_rgb.json', 'metadata/images/00012_rgb.json', 'metadata/images/00026_rgb.json', 'metadata/images/00030_rgb.json', 'metadata/images/00055_rgb.json', 'metadata/images/00024_rgb.json', 'metadata/images/00058_rgb.json', 'metadata/images/00011_rgb.json', 'metadata/images/00046_rgb.json', 'metadata/images/00009_rgb.json', 'metadata/images/00036_rgb.json', 'metadata/images/00006_rgb.json', 'metadata/images/00019_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00034_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00040_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00021_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00056_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00029_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00050_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00018_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00039_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00017_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00043_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00025_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00016_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00047_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00033_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00051_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00053_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00005_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00048_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00031_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00042_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00008_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00002_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00013_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00000_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00014_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00035_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00023_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00027_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00044_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00041_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00010_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00001_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00045_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00007_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00052_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00020_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00049_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00015_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00028_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00004_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00054_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00037_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00038_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00003_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00059_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00032_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00022_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00057_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00012_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00026_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00030_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00055_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00024_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00058_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00011_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00046_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00009_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00036_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00006_rgb.json', 'metadata/Masks_1__0__1__0____channel____rgb_5619aa428d/00019_rgb.json', 'metadata/Voxels___x____300__450__colmap_camera_False_2a093f0ccc/Voxels.json', 'TreeGraph__False_CurveSkeleton_c304a2cc71/TreeGraph.p', 'images/00013_rgb.jpg', 'images/00010_rgb.jpg', 'images/00020_rgb.jpg', 'images/00028_rgb.jpg', 'images/00035_rgb.jpg', 'images/00054_rgb.jpg', 'images/00030_rgb.jpg', 'images/00018_rgb.jpg', 'images/00029_rgb.jpg', 'images/00005_rgb.jpg', 'images/00004_rgb.jpg', 'images/00038_rgb.jpg', 'images/00047_rgb.jpg', 'images/00053_rgb.jpg', 'images/00014_rgb.jpg', 'images/00021_rgb.jpg', 'images/00037_rgb.jpg', 'images/00057_rgb.jpg', 'images/00007_rgb.jpg', 'images/00016_rgb.jpg', 'images/00050_rgb.jpg', 'images/00036_rgb.jpg', 'images/00017_rgb.jpg', 'images/00052_rgb.jpg', 'images/00009_rgb.jpg', 'images/00031_rgb.jpg', 'images/00002_rgb.jpg', 'images/00059_rgb.jpg', 'images/00027_rgb.jpg', 'images/00015_rgb.jpg', 'images/00032_rgb.jpg', 'images/00023_rgb.jpg', 'images/00000_rgb.jpg', 'images/00056_rgb.jpg', 'images/00033_rgb.jpg', 'images/00048_rgb.jpg', 'images/00019_rgb.jpg', 'images/00039_rgb.jpg', 'images/00044_rgb.jpg', 'images/00040_rgb.jpg', 'images/00022_rgb.jpg', 'images/00003_rgb.jpg', 'images/00055_rgb.jpg', 'images/00034_rgb.jpg', 'images/00049_rgb.jpg', 'images/00045_rgb.jpg', 'images/00043_rgb.jpg', 'images/00051_rgb.jpg', 'images/00011_rgb.jpg', 'images/00012_rgb.jpg', 'images/00024_rgb.jpg', 'images/00025_rgb.jpg', 'images/00046_rgb.jpg', 'images/00001_rgb.jpg', 'images/00042_rgb.jpg', 'images/00006_rgb.jpg', 'images/00041_rgb.jpg', 'images/00008_rgb.jpg', 'images/00026_rgb.jpg', 'images/00058_rgb.jpg', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/colmap.log', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/cnc_vs_colmap_poses_estimated.png', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/points3d.json', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/images.json', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/model_analyzer.log', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/sparse.ply', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/cameras.json', 'Colmap_True_null_SIMPLE_RADIAL_ffcef49fdc/euclidean_distances.json', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00030_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00016_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00018_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00057_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00048_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00027_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00051_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00006_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00022_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00003_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00017_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00004_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00059_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00035_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00005_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00046_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00001_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00026_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00054_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00031_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00025_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00007_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00028_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00010_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00008_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00042_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00000_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00036_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00041_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00015_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00024_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00052_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00053_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00009_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00002_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00039_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00034_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00019_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00020_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00044_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00047_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00056_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00029_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00049_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00045_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00021_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00037_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00032_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00011_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00058_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00012_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00038_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00050_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00055_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00040_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00043_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00014_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00023_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00033_rgb.png', 'Masks_1__0__1__0____channel____rgb_5619aa428d/00013_rgb.png', 'Voxels___x____300__450__colmap_camera_False_2a093f0ccc/Voxels.tiff', 'AnglesAndInternodes_1_0_2_0_6_0_6dd64fc595/AnglesAndInternodes.json', 'AnglesAndInternodes_1_0_2_0_6_0_6dd64fc595/fruit_direction.json', 'AnglesAndInternodes_1_0_2_0_6_0_6dd64fc595/stem_direction.json', 'CurveSkeleton__TriangleMesh_0393cb5708/CurveSkeleton.json']
    >>> shutil.rmtree(tmp_dir)  # Remove the temporary directory (and its contents)
    >>> # Stop the test server
    >>> server.stop()
    """
    try:
        scan = self.db.get_scan(scan_id, **kwargs)

    except NoAuthUserError as e:
        return {"error": str(e)}, 401  # HTTP 401 Unauthorized (authentication)
    except ScanNotFoundError:
        return {
            "error": f"Could not find a scan named `{scan_id}`!"
        }, 404  # HTTP 404 Not Found
    except Exception as e:
        return {
            "error": f"Error accessing the scan {scan_id}: {str(e)}"
        }, 500  # HTTP 500 Internal Server Error

    try:
        zip_path = create_zip_for_scan(Path(scan.path()), self.logger)
    except ArchiveError as exc:
        return {"error": str(exc)}, 400

    # Schedule the temporary file for cleanup after request completion
    @after_this_request
    def cleanup_temp_file(response):
        try:
            if zip_path.exists():
                zip_path.unlink()
                self.logger.info(f"Temporary archive `{zip_path}` deleted.")
        except Exception as e:
            self.logger.error(f"Failed to delete temporary file `{zip_path}`: {e}")
        return response

    return send_file(
        zip_path, download_name=f"{scan_id}.zip", mimetype="application/zip"
    )

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

scan_id Link

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: {'error': error_message} The integer represents the HTTP status code (200 for success, 400 or 500 for errors)

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:

>>> # Start the REST API server (in test mode)
>>> 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()
>>> import requests
>>> from pathlib import Path
>>> from tempfile import gettempdir
>>> from plantdb.commons import api_endpoints
>>> 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/'+api_endpoints.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'
>>> file_uri = api_endpoints.archive(new_dataset)
>>> with open(zip_file, 'rb') as zip_f:
...    files = {'zip_file': (str(zip_file), zip_f, 'application/zip')}
...    response = requests.post("http://127.0.0.1:5000"+file_uri, files=files, headers={'Authorization': 'Bearer ' + jwt_token})
>>> print(response.ok)
True
>>> # Call for a refresh of the database for this specific dataset to 'register' it:
>>> _ = requests.get("http://127.0.0.1:5000" + api_endpoints.refresh(new_dataset))
>>> # Look if it is now part of the list of scans:
>>> r = requests.get("http://127.0.0.1:5000" + api_endpoints.scans())
>>> scans_list = r.json()
>>> print(new_dataset in scans_list)
True
>>> # Stop the test server
>>> server.stop()
Source code in plantdb/server/api/assets.py
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
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
@sanitize_ids("scan_id")
@rate_limit(max_requests=5, window_seconds=60)
@add_jwt_from_header
def post(self, 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
    ----------
    scan_id : str
        Unique identifier for the scan dataset where the ZIP contents will be extracted.

    Returns
    -------
    tuple
        A tuple containing (dict, int) where the dict contains either:
        - On success: {'success': message, 'files': list_of_extracted_files}
        - On failure: {'error': error_message}
        The integer represents the HTTP status code (``200`` for success, ``400`` or ``500`` for errors)

    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
    --------
    >>> # Start the REST API server (in test mode)
    >>> 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()

    >>> import requests
    >>> from pathlib import Path
    >>> from tempfile import gettempdir
    >>> from plantdb.commons import api_endpoints
    >>> 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/'+api_endpoints.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'
    >>> file_uri = api_endpoints.archive(new_dataset)
    >>> with open(zip_file, 'rb') as zip_f:
    ...    files = {'zip_file': (str(zip_file), zip_f, 'application/zip')}
    ...    response = requests.post("http://127.0.0.1:5000"+file_uri, files=files, headers={'Authorization': 'Bearer ' + jwt_token})
    >>> print(response.ok)
    True
    >>> # Call for a refresh of the database for this specific dataset to 'register' it:
    >>> _ = requests.get("http://127.0.0.1:5000" + api_endpoints.refresh(new_dataset))
    >>> # Look if it is now part of the list of scans:
    >>> r = requests.get("http://127.0.0.1:5000" + api_endpoints.scans())
    >>> scans_list = r.json()
    >>> print(new_dataset in scans_list)
    True
    >>> # Stop the test server
    >>> server.stop()
    """
    # 1. Basic pre-condition checks
    if self.db.scan_exists(scan_id):
        self.logger.error("Dataset `%s` already exists.", scan_id)
        return {"error": f"Dataset `{scan_id}` already exists!"}, 400

    # Get the zip file from the request
    uploaded = request.files.get("zip_file")
    try:
        # Check if a file was provided
        _ensure_file_present(uploaded)
        # Validate the file MIME type
        _validate_mime_type(uploaded)
        # Validate the file extension
        _validate_extension(uploaded)
    except ValidationError as exc:
        self.logger.error("Upload validation failed: %s", exc)
        return {"error": str(exc)}, 400

    # 2. Store uploaded file temporarily
    try:
        temp_zip = _save_to_temp(uploaded, self.logger)
    except ValidationError as exc:
        return {"error": str(exc)}, 500  # HTTP 500 Internal Server Error

    # try/except/finally to ensure the temporary file is always removed
    try:
        # 3. Archive integrity & domain validation
        _test_zip_integrity(temp_zip, self.logger)
        _ensure_valid_structure(temp_zip)

        # 4. Create destination scan and extract files
        try:
            scan_path = Path(self.db.create_scan(scan_id, **kwargs).path())

        except NoAuthUserError as e:
            return {"error": str(e)}, 401  # HTTP 401 Unauthorized (authentication)
        except Exception as e:
            return {
                "error": f"Error accessing the scan {scan_id}: {str(e)}"
            }, 500  # HTTP 500 Internal Server Error

        extracted_files = extract_zip_to_scan(temp_zip, scan_path, self.logger)

        # 5. Refresh DB state and respond
        self.db.reload(scan_id)
        return {
            "message": "ZIP file processed successfully",
            "files": extracted_files,
        }, 200

    except (ValidationError, ExtractionError) as exc:
        # Validation / extraction errors are client-side problems → 400
        return {"error": str(exc)}, 400
    except Exception as exc:
        # Unexpected server-side errors → 500
        self.logger.exception("Unexpected error while processing archive")
        return {
            "error": f"Internal server error: {exc}"
        }, 500  # HTTP 500 Internal Server Error
    finally:
        temp_zip.unlink(missing_ok=True)

CurveSkeleton Link

CurveSkeleton(db, logger=None)

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

db Link

FSDB

A database instance providing the resources to serve.

required

logger Link

Logger

A logger instance to record operations and errors.

None
Source code in plantdb/server/api/assets.py
788
789
790
791
792
793
794
795
796
797
798
799
800
801
def __init__(self, db, logger=None):
    """Initialize the resource.

    Parameters
    ----------
    db : plantdb.commons.fsdb.core.FSDB
        A database instance providing the resources to serve.
    logger : logging.Logger
        A logger instance to record operations and errors.
    """
    self.db: FSDB = db
    self.logger: logging.Logger = (
        logger if logger else get_logger(self.__class__.__name__)
    )

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

scan_id Link

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 (in test mode)
>>> 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()
>>> import requests
>>> from plantdb.commons import api_endpoints
>>> # Fetch skeleton data for a valid scan
>>> file_uri = api_endpoints.skeleton('real_plant_analyzed')
>>> response = requests.get("http://127.0.0.1:5000" + file_uri)
>>> skeleton_data = response.json()
>>> print(list(skeleton_data.keys()))
['points', 'lines']
>>> # Stop the test server
>>> server.stop()
Source code in plantdb/server/api/assets.py
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
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
@sanitize_ids("scan_id")
@rate_limit(max_requests=5, window_seconds=60)
@add_jwt_from_header
@use_guest_as_default  # FIXME: Remove this if we want strict token identification
def get(self, 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
    ----------
    scan_id : str
        Identifier for the plant scan to retrieve skeleton data for.
        Must contain only alphanumeric characters, underscores, dashes, or periods.

    Returns
    -------
    Union[dict, Tuple[dict, int]]
        On success: Dictionary containing the curve skeleton data
        On failure: Tuple of (error_dict, http_status_code)

    Raises
    ------
    plantdb.commons.fsdb.exceptions.ScanNotFoundError
        If the requested scan ID doesn't exist in the database
    plantdb.commons.fsdb.exceptions.FilesetNotFoundError
        If the CurveSkeleton fileset is not found for the scan
    plantdb.commons.fsdb.exceptions.FileNotFoundError
        If the CurveSkeleton file is missing from the fileset
    http.client.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 (in test mode)
    >>> 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()

    >>> import requests
    >>> from plantdb.commons import api_endpoints
    >>> # Fetch skeleton data for a valid scan
    >>> file_uri = api_endpoints.skeleton('real_plant_analyzed')
    >>> response = requests.get("http://127.0.0.1:5000" + file_uri)
    >>> skeleton_data = response.json()
    >>> print(list(skeleton_data.keys()))
    ['points', 'lines']
    >>> # Stop the test server
    >>> server.stop()
    """
    file = resource_file(self.db, scan_id, "CurveSkeleton", **kwargs)
    # If an error tuple was returned, forward it directly to Flask-RESTful.
    if isinstance(file, tuple) and isinstance(file[0], dict):
        return file

    # Load the JSON file:
    try:
        skeleton = read_json(file.path())
    except Exception as e:
        return json.dumps({"error": str(e)}), 500  # HTTP 500 Internal Server Error
    else:
        return skeleton

FilePath Link

FilePath(db, logger=None)

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

db Link

FSDB

A database instance providing the resources to serve.

required

logger Link

Logger

A logger instance to record operations and errors.

None
Source code in plantdb/server/api/assets.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def __init__(self, db, logger=None):
    """Initialize the resource.

    Parameters
    ----------
    db : plantdb.commons.fsdb.core.FSDB
        A database instance providing the resources to serve.
    logger : logging.Logger
        A logger instance to record operations and errors.
    """
    self.db: FSDB = db
    self.logger: logging.Logger = (
        logger if logger else get_logger(self.__class__.__name__)
    )

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

path Link

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)
>>> 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()
>>> import requests
>>> import toml
>>> from plantdb.commons import api_endpoints
>>> # Request a TOML configuration file
>>> file_uri = api_endpoints.file_path('real_plant_analyzed/pipeline.toml')
>>> response = requests.get("http://127.0.0.1:5000" + file_uri)
>>> cfg = toml.loads(response.content.decode())
>>> print(cfg['Undistorted'])
{'upstream_task': 'ImagesFilesetExists'}
>>> # Request a JSON file
>>> file_uri = api_endpoints.file_path('real_plant_analyzed/files.json')
>>> response = requests.get("http://127.0.0.1:5000" + file_uri)
>>> 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']
>>> # Stop the test server
>>> server.stop()
Source code in plantdb/server/api/assets.py
173
174
175
176
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
229
230
231
232
233
@rate_limit(max_requests=120, window_seconds=60)
def get(self, 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
    ----------
    path : str
        Relative path to the requested file within the database directory.
        This path will be resolved against the database root path.

    Returns
    -------
    flask.Response
        A Flask response object containing the requested file or an
        appropriate error response if the file is not found.

    Raises
    ------
    werkzeug.exceptions.NotFound
        If the requested file does not exist.
    werkzeug.exceptions.Forbidden
        If the file access is forbidden.
    http.client.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)
    >>> 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()

    >>> import requests
    >>> import toml
    >>> from plantdb.commons import api_endpoints
    >>> # Request a TOML configuration file
    >>> file_uri = api_endpoints.file_path('real_plant_analyzed/pipeline.toml')
    >>> response = requests.get("http://127.0.0.1:5000" + file_uri)
    >>> cfg = toml.loads(response.content.decode())
    >>> print(cfg['Undistorted'])
    {'upstream_task': 'ImagesFilesetExists'}
    >>> # Request a JSON file
    >>> file_uri = api_endpoints.file_path('real_plant_analyzed/files.json')
    >>> response = requests.get("http://127.0.0.1:5000" + file_uri)
    >>> 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']
    >>> # Stop the test server
    >>> server.stop()
    """
    return send_from_directory(self.db.path(), path)

Image Link

Image(db, logger=None)

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

db Link

FSDB

A database instance providing the resources to serve.

required

logger Link

Logger

A logger instance to record operations and errors.

None
Source code in plantdb/server/api/assets.py
256
257
258
259
260
261
262
263
264
265
266
267
268
269
def __init__(self, db, logger=None):
    """Initialize the resource.

    Parameters
    ----------
    db : plantdb.commons.fsdb.core.FSDB
        A database instance providing the resources to serve.
    logger : logging.Logger
        A logger instance to record operations and errors.
    """
    self.db: FSDB = db
    self.logger: logging.Logger = (
        logger if logger else get_logger(self.__class__.__name__)
    )

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

scan_id Link

str

Identifier for the scan containing the image.

required

fileset_id Link

str

Identifier for the fileset within the scan.

required

file_id Link

str

Identifier for the specific image file.

required

Other Parameters:

Name Type Description
size str or float

Query parameter controlling downsampling. Accepted values: * 'thumb': image max width and height to 150 (default); * 'large': image max width and height to 1500; * 'orig': original image, no chache; If an invalid string is supplied, the default 'thumb' is used.

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:

>>> # Start the REST API server (in test mode)
>>> 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()
>>> import numpy as np
>>> from plantdb.commons import api_endpoints
>>> import requests
>>> import pybase64
>>> from io import BytesIO
>>> from PIL import Image
>>> # Example #1 - Get the first image as a thumbnail (default):
>>> file_uri = api_endpoints.image('real_plant_analyzed', 'images', '00000_rgb')
>>> response = requests.get("http://127.0.0.1:5000" + file_uri, stream=True)
>>> img = Image.open(BytesIO(response.content))
>>> img.show()
>>> np.asarray(img).shape
(113, 150, 3)
>>> # Example #2 - Get the first image in original size:
>>> file_uri = api_endpoints.image('real_plant_analyzed', 'images', '00000_rgb', size="orig")
>>> response = requests.get("http://127.0.0.1:5000" + file_uri, stream=True)
>>> img = Image.open(BytesIO(response.content))
>>> img.show()
>>> np.asarray(img).shape
(1080, 1440, 3)
>>> # Example #3 - Get a base64 encoded image:
>>> file_uri = api_endpoints.image('real_plant_analyzed', 'images', '00000_rgb', size="orig", as_base64=True)
>>> response = requests.get("http://127.0.0.1:5000" + file_uri, stream=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))
>>> img.show()
>>> # Stop the test server
>>> server.stop()
Source code in plantdb/server/api/assets.py
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
@sanitize_ids("scan_id")
@sanitize_ids("fileset_id")
@sanitize_ids("file_id")
@rate_limit(max_requests=3000, window_seconds=60)
@add_jwt_from_header
@use_guest_as_default  # FIXME: Remove this if we want strict token identification
def get(self, 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
    ----------
    scan_id : str
        Identifier for the scan containing the image.
    fileset_id : str
        Identifier for the fileset within the scan.
    file_id : str
        Identifier for the specific image file.

    Other Parameters
    ----------------
    size : str or float
        Query parameter controlling downsampling.
        Accepted values:
            * `'thumb'`: image max width and height to `150` (default);
            * `'large'`: image max width and height to `1500`;
            * `'orig'`: original image, no chache;
        If an invalid string is supplied, the default 'thumb' is used.
    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
    -------
    flask.Response
        HTTP response containing the image data with 'content-type' mimetype.

    Raises
    ------
    werkzeug.exceptions.NotFound
        If the requested image file doesn't exist.
    http.client.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
    --------
    >>> # Start the REST API server (in test mode)
    >>> 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()

    >>> import numpy as np
    >>> from plantdb.commons import api_endpoints
    >>> import requests
    >>> import pybase64
    >>> from io import BytesIO
    >>> from PIL import Image
    >>> # Example #1 - Get the first image as a thumbnail (default):
    >>> file_uri = api_endpoints.image('real_plant_analyzed', 'images', '00000_rgb')
    >>> response = requests.get("http://127.0.0.1:5000" + file_uri, stream=True)
    >>> img = Image.open(BytesIO(response.content))
    >>> img.show()
    >>> np.asarray(img).shape
    (113, 150, 3)
    >>> # Example #2 - Get the first image in original size:
    >>> file_uri = api_endpoints.image('real_plant_analyzed', 'images', '00000_rgb', size="orig")
    >>> response = requests.get("http://127.0.0.1:5000" + file_uri, stream=True)
    >>> img = Image.open(BytesIO(response.content))
    >>> img.show()
    >>> np.asarray(img).shape
    (1080, 1440, 3)
    >>> # Example #3 - Get a base64 encoded image:
    >>> file_uri = api_endpoints.image('real_plant_analyzed', 'images', '00000_rgb', size="orig", as_base64=True)
    >>> response = requests.get("http://127.0.0.1:5000" + file_uri, stream=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))
    >>> img.show()
    >>> # Stop the test server
    >>> server.stop()
    """
    # Parse the `size` flag
    size = request.args.get("size", default="thumb", type=str)
    # Get the path to the image resource:
    try:
        path = webcache.image_path(
            self.db, scan_id, fileset_id, file_id, size, **kwargs
        )

    except NoAuthUserError as e:
        return {"error": str(e)}, 401  # HTTP 401 Unauthorized (authentication)
    except ScanNotFoundError as e:
        return {"error": str(e)}, 404  # HTTP 404 Not Found
    except FilesetNotFoundError as e:
        return {"error": str(e)}, 404  # HTTP 404 Not Found
    except FileNotFoundError as e:
        return {"error": str(e)}, 404  # HTTP 404 Not Found
    except Exception as e:
        self.logger.error(f"Error retrieving image file: {str(e)}")
        return {
            "error": f"Error retrieving image file: {str(e)}"
        }, 500  # HTTP 500 Internal Server Error

    mime_type, _ = mimetypes.guess_type(path)

    if self.wants_base64(request):
        # ---------- JSON (base64) ----------
        with open(path, "rb") as f:
            b64_str = pybase64.b64encode(f.read()).decode("ascii")
        # ``decode('ascii')`` gives us a plain string that can be JSON-encoded.
        payload = {"image": b64_str, "content-type": mime_type}
        # Wrap ``jsonify`` with ``make_response`` to add custom headers
        resp = make_response(jsonify(payload))
        resp.headers["Content-Type"] = "application/json"
        resp.headers["X-Content-Encoding"] = "base64"
    else:
        # ---------- Binary (streaming) ----------
        resp = make_response(send_file(path, mimetype=mime_type))
        resp.headers["X-Content-Encoding"] = "binary"

    return resp

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
271
272
273
274
275
276
277
@staticmethod
def wants_base64(request) -> bool:
    """
    Return ``True`` when the query string contains ``as_base64`` with a truthy value.
    """
    flag = request.args.get("as_base64", default="false", type=str).lower()
    return flag in ("true", "1", "yes")

Mesh Link

Mesh(db, logger=None)

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

db Link

FSDB

A database instance providing the resources to serve.

required

logger Link

Logger

A logger instance to record operations and errors.

None
Source code in plantdb/server/api/assets.py
631
632
633
634
635
636
637
638
639
640
641
642
643
644
def __init__(self, db, logger=None):
    """Initialize the resource.

    Parameters
    ----------
    db : plantdb.commons.fsdb.core.FSDB
        A database instance providing the resources to serve.
    logger : logging.Logger
        A logger instance to record operations and errors.
    """
    self.db: FSDB = db
    self.logger: logging.Logger = (
        logger if logger else get_logger(self.__class__.__name__)
    )

get Link

get(scan_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

scan_id Link

str

Identifier for the scan containing the mesh.

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 size parameter 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:

>>> # Start the REST API server (in test mode)
>>> 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()
>>> import requests
>>> from plantdb.commons import api_endpoints
>>> from plyfile import PlyData
>>> from io import BytesIO
>>> # Request a mesh file
>>> file_uri = api_endpoints.mesh('real_plant_analyzed', size='orig')
>>> response = requests.get("http://127.0.0.1:5000" + file_uri)
>>> # Parse the PLY data
>>> mesh_data = PlyData.read(BytesIO(response.content))
>>> # Access vertex coordinates
>>> vertices = mesh_data['vertex']
>>> x_coords = list(vertices['x'])
>>> len(x_coords)
28360
>>> # Stop the test server
>>> server.stop()
Source code in plantdb/server/api/assets.py
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
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
@sanitize_ids("scan_id")
@rate_limit(max_requests=5, window_seconds=60)
@add_jwt_from_header
@use_guest_as_default  # FIXME: Remove this if we want strict token identification
def get(self, scan_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
    ----------
    scan_id : str
        Identifier for the scan containing the mesh.

    Other Parameters
    ----------------
    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
    -------
    flask.Response
        HTTP response containing the mesh data as an octet-stream.

    Raises
    ------
    werkzeug.exceptions.NotFound
        If the requested mesh file doesn't exist
    http.client.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 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
    --------
    >>> # Start the REST API server (in test mode)
    >>> 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()

    >>> import requests
    >>> from plantdb.commons import api_endpoints
    >>> from plyfile import PlyData
    >>> from io import BytesIO
    >>> # Request a mesh file
    >>> file_uri = api_endpoints.mesh('real_plant_analyzed', size='orig')
    >>> response = requests.get("http://127.0.0.1:5000" + file_uri)
    >>> # Parse the PLY data
    >>> mesh_data = PlyData.read(BytesIO(response.content))
    >>> # Access vertex coordinates
    >>> vertices = mesh_data['vertex']
    >>> x_coords = list(vertices['x'])
    >>> len(x_coords)
    28360
    >>> # Stop the test server
    >>> server.stop()
    """
    # Parse the `size` flag
    size = request.args.get("size", default="orig", type=str)
    # Make sure that the 'size' argument we got is a valid option, else default to 'orig':
    if not size in ["orig"]:
        size = "orig"

    # Parse the coords flag (accepting true/1/yes in any case)
    coords_flag = request.args.get("coords", default="false", type=str).lower() in (
        "true",
        "1",
        "yes",
    )

    file = resource_file(self.db, scan_id, "TriangleMesh", **kwargs)
    # If an error tuple was returned, forward it directly to Flask-RESTful.
    if isinstance(file, tuple) and isinstance(file[0], dict):
        return file

    try:
        # Get the path to the mesh resource:
        path = webcache.mesh_path(
            self.db, file.scan.id, file.fileset.id, file.id, size, **kwargs
        )

    except NoAuthUserError as e:
        return {"error": str(e)}, 401  # HTTP 401 Unauthorized (authentication)
    except ScanNotFoundError as e:
        return {"error": str(e)}, 404  # HTTP 404 Not Found
    except FilesetNotFoundError as e:
        return {"error": str(e)}, 404  # HTTP 404 Not Found
    except FileNotFoundError as e:
        return {"error": str(e)}, 404  # HTTP 404 Not Found
    except Exception as e:
        self.logger.error(f"Error retrieving mesh file: {str(e)}")
        return {
            "error": f"Error retrieving mesh file: {str(e)}"
        }, 500  # HTTP 500 Internal Server Error

    # If coords_flag is set, read the file and return JSON
    if coords_flag:
        import numpy as np
        from open3d import io

        pcd = io.read_triangle_mesh(path, print_progress=False)
        # Convert the Open3D Vector3dVector to a plain Python list so JSON can serialize it.
        return jsonify(
            {
                "vertices": np.array(pcd.vertices).tolist(),
                "triangles": np.array(pcd.triangles).tolist(),
            }
        )
    # Otherwise, return the file directly
    return send_file(path, mimetype="application/octet-stream")

PointCloud Link

PointCloud(db, logger=None)

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

db Link

FSDB

A database instance providing the resources to serve.

required

logger Link

Logger

A logger instance to record operations and errors.

None
Source code in plantdb/server/api/assets.py
437
438
439
440
441
442
443
444
445
446
447
448
449
450
def __init__(self, db, logger=None):
    """Initialize the resource.

    Parameters
    ----------
    db : plantdb.commons.fsdb.core.FSDB
        A database instance providing the resources to serve.
    logger : logging.Logger
        A logger instance to record operations and errors.
    """
    self.db: FSDB = db
    self.logger: logging.Logger = (
        logger if logger else get_logger(self.__class__.__name__)
    )

get Link

get(scan_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

scan_id Link

str

Identifier for the scan containing the point cloud.

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.

type str

Query parameter indicating whether to return the reconstructed point cloud (default) or the ground truth ('type=gt').

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:

>>> # Start the REST API server (in test mode)
>>> 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()
>>> import requests
>>> from plantdb.commons import api_endpoints
>>> from plyfile import PlyData
>>> from io import BytesIO
>>> # Get original point cloud:
>>> file_uri = api_endpoints.pointcloud('real_plant_analyzed', size='orig')
>>> response = requests.get("http://127.0.0.1:5000" + file_uri)
>>> pcd_data = PlyData.read(BytesIO(response.content))
>>> # Get the number of points in the point-cloud from X-coordinates:
>>> len(list(pcd_data['vertex']['x']))
57890
>>> # Get preview (downsampled) version
>>> file_uri = api_endpoints.pointcloud('real_plant_analyzed', size='preview')
>>> response = requests.get("http://127.0.0.1:5000" + file_uri)
>>> pcd_data = PlyData.read(BytesIO(response.content))
>>> # Get the number of points in the point-cloud from X-coordinates:
>>> len(list(pcd_data['vertex']['x']))
2907
>>> # Get custom downsampled version (voxel size 0.01)
>>> file_uri = api_endpoints.pointcloud('real_plant_analyzed', size=0.01)
>>> response = requests.get("http://127.0.0.1:5000" + file_uri)
>>> pcd_data = PlyData.read(BytesIO(response.content))
>>> # Get the number of points in the point-cloud from X-coordinates:
>>> len(list(pcd_data['vertex']['x']))
57856
>>> # Send the coordinates (read the file on the server-side)
>>> file_uri = api_endpoints.pointcloud('real_plant_analyzed', size='preview', coords=True)
>>> response = requests.get("http://127.0.0.1:5000" + file_uri)
>>> coordinates = np.array(response.json()['coordinates'])
>>> # Get the shape of the `coordinates` array:
>>> coordinates.shape
(2907, 3)
>>> # Stop the test server
>>> server.stop()
Source code in plantdb/server/api/assets.py
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
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
@sanitize_ids("scan_id")
@rate_limit(max_requests=5, window_seconds=60)
@add_jwt_from_header
@use_guest_as_default  # FIXME: Remove this if we want strict token identification
def get(self, scan_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
    ----------
    scan_id : str
        Identifier for the scan containing the point cloud.

    Other Parameters
    ----------------
    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.
    type : str
        Query parameter indicating whether to return the reconstructed point cloud (default) or
        the ground truth ('type=gt').

    Returns
    -------
    flask.Response
        HTTP response containing the PLY data with 'application/octet-stream' mimetype.

    Raises
    ------
    werkzeug.exceptions.NotFound
        If the requested point-cloud file doesn't exist.
    http.client.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
    --------
    >>> # Start the REST API server (in test mode)
    >>> 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()

    >>> import requests
    >>> from plantdb.commons import api_endpoints
    >>> from plyfile import PlyData
    >>> from io import BytesIO
    >>> # Get original point cloud:
    >>> file_uri = api_endpoints.pointcloud('real_plant_analyzed', size='orig')
    >>> response = requests.get("http://127.0.0.1:5000" + file_uri)
    >>> pcd_data = PlyData.read(BytesIO(response.content))
    >>> # Get the number of points in the point-cloud from X-coordinates:
    >>> len(list(pcd_data['vertex']['x']))
    57890
    >>> # Get preview (downsampled) version
    >>> file_uri = api_endpoints.pointcloud('real_plant_analyzed', size='preview')
    >>> response = requests.get("http://127.0.0.1:5000" + file_uri)
    >>> pcd_data = PlyData.read(BytesIO(response.content))
    >>> # Get the number of points in the point-cloud from X-coordinates:
    >>> len(list(pcd_data['vertex']['x']))
    2907
    >>> # Get custom downsampled version (voxel size 0.01)
    >>> file_uri = api_endpoints.pointcloud('real_plant_analyzed', size=0.01)
    >>> response = requests.get("http://127.0.0.1:5000" + file_uri)
    >>> pcd_data = PlyData.read(BytesIO(response.content))
    >>> # Get the number of points in the point-cloud from X-coordinates:
    >>> len(list(pcd_data['vertex']['x']))
    57856
    >>> # Send the coordinates (read the file on the server-side)
    >>> file_uri = api_endpoints.pointcloud('real_plant_analyzed', size='preview', coords=True)
    >>> response = requests.get("http://127.0.0.1:5000" + file_uri)
    >>> coordinates = np.array(response.json()['coordinates'])
    >>> # Get the shape of the `coordinates` array:
    >>> coordinates.shape
    (2907, 3)
    >>> # Stop the test server
    >>> server.stop()
    """
    # Parse the `size` flag
    size = request.args.get("size", default="preview", type=str)
    # Try to convert the 'size' argument as a float:
    try:
        vxs = float(size)
    except ValueError:
        pass
    else:
        size = vxs
    # If a string, make sure that the 'size' argument we got is a valid option, else default to 'preview':
    if isinstance(size, str) and size not in ["orig", "preview"]:
        size = "preview"

    # Parse the `coords` flag
    true_coords_opt = {"true", "1", "yes"}  # valid options to match the `True` flag
    coords_flag = request.args.get("coords", default="false", type=str).lower() in true_coords_opt

    # Parse the `type` flag
    pcd_type = request.args.get("type", default="default", type=str)
    if pcd_type == "gt":
        task_name = "PointCloudGroundTruth"
    else:
        task_name = "PointCloud"  # default to 'default'

    # Get the corresponding `File` from the database
    file = resource_file(self.db, scan_id, task_name, **kwargs)
    # If an error tuple was returned, forward it directly to Flask-RESTful.
    if isinstance(file, tuple) and isinstance(file[0], dict):
        return file

    try:
        # Get the path to the pointcloud resource:
        path = webcache.pointcloud_path(
            self.db, file.scan.id, file.fileset.id, file.id, size, **kwargs
        )

    except NoAuthUserError as e:
        return {"error": str(e)}, 401  # HTTP 401 Unauthorized (authentication)
    except ScanNotFoundError as e:
        return {"error": str(e)}, 404  # HTTP 404 Not Found
    except FilesetNotFoundError as e:
        return {"error": str(e)}, 404  # HTTP 404 Not Found
    except FileNotFoundError as e:
        return {"error": str(e)}, 404  # HTTP 404 Not Found
    except Exception as e:
        self.logger.error(f"Error retrieving point cloud file: {str(e)}")
        return {
            "error": f"Error retrieving point cloud file: {str(e)}"
        }, 500  # HTTP 500 Internal Server Error

    # If coords_flag is set, read the file and return JSON
    if coords_flag:
        import numpy as np
        from open3d import io

        pcd = io.read_point_cloud(path, print_progress=False)
        # Convert the Open3D Vector3dVector to a plain Python list so JSON can serialize it.
        return jsonify({"coordinates": np.array(pcd.points).tolist()})
    # Otherwise, return the file directly
    return send_file(path, mimetype="application/octet-stream")

Sequence Link

Sequence(db, logger=None)

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

db Link

FSDB

A database instance providing the resources to serve.

required

logger Link

Logger

A logger instance to record operations and errors.

None
Source code in plantdb/server/api/assets.py
891
892
893
894
895
896
897
898
899
900
901
902
903
904
def __init__(self, db, logger=None):
    """Initialize the resource.

    Parameters
    ----------
    db : plantdb.commons.fsdb.core.FSDB
        A database instance providing the resources to serve.
    logger : logging.Logger
        A logger instance to record operations and errors.
    """
    self.db: FSDB = db
    self.logger: logging.Logger = (
        logger if logger else get_logger(self.__class__.__name__)
    )

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

scan_id Link

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:

>>> # Start the REST API server (in test mode)
>>> 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 all sequence data
>>> import requests
>>> from plantdb.commons import api_endpoints
>>> file_uri = api_endpoints.sequence('real_plant_analyzed')
>>> response = requests.get("http://127.0.0.1:5000" + file_uri)
>>> data = response.json()  # Expected output: {'angles': [...], 'internodes': [...], 'fruit_points': [...]}
>>> print(list(data))
['angles', 'internodes', 'fruit_points', 'manual_angles', 'manual_internodes']
>>> # Get only angles data
>>> file_uri = api_endpoints.sequence('real_plant_analyzed', seq_type='angles')
>>> response = requests.get("http://127.0.0.1:5000" + file_uri)
>>> angles = response.json()
>>> print(angles[:5])
[47.13015345294241, 239.43543078022594, 311.8816488465762, 251.0289289739646, 249.56560354730826]
>>> # Stop the test server
>>> server.stop()
Source code in plantdb/server/api/assets.py
 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
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 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
@sanitize_ids("scan_id")
@rate_limit(max_requests=60, window_seconds=60)
@add_jwt_from_header
@use_guest_as_default  # FIXME: Remove this if we want strict token identification
def get(self, 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
    ----------
    scan_id : str
        Unique identifier for the plant scan. Must contain only alphanumeric
        characters, underscores, dashes, or periods.

    Returns
    -------
    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
    ------
    plantdb.commons.fsdb.exceptions.ScanNotFoundError
        If the specified scan_id does not exist in the database
    plantdb.commons.fsdb.exceptions.FilesetNotFoundError
        If the AnglesAndInternodes fileset is not found
    plantdb.commons.fsdb.exceptions.FileNotFoundError
        If the AnglesAndInternodes file is not found within the fileset
    http.client.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
    --------
    >>> # Start the REST API server (in test mode)
    >>> 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 all sequence data
    >>> import requests
    >>> from plantdb.commons import api_endpoints
    >>> file_uri = api_endpoints.sequence('real_plant_analyzed')
    >>> response = requests.get("http://127.0.0.1:5000" + file_uri)
    >>> data = response.json()  # Expected output: {'angles': [...], 'internodes': [...], 'fruit_points': [...]}
    >>> print(list(data))
    ['angles', 'internodes', 'fruit_points', 'manual_angles', 'manual_internodes']
    >>> # Get only angles data
    >>> file_uri = api_endpoints.sequence('real_plant_analyzed', seq_type='angles')
    >>> response = requests.get("http://127.0.0.1:5000" + file_uri)
    >>> angles = response.json()
    >>> print(angles[:5])
    [47.13015345294241, 239.43543078022594, 311.8816488465762, 251.0289289739646, 249.56560354730826]
    >>> # Stop the test server
    >>> server.stop()
    """
    type = request.args.get("type", default="all", type=str)

    file = resource_file(self.db, scan_id, "AnglesAndInternodes", **kwargs)
    # If an error tuple was returned, forward it directly to Flask-RESTful.
    if isinstance(file, tuple) and isinstance(file[0], dict):
        return file

    # Load the JSON file:
    try:
        measures = read_json(file.path())
    except Exception as e:
        return json.dumps({"error": str(e)}), 500  # HTTP 500 Internal Server Error

    # Load the manual 'measures.json' JSON file:
    scan = file.scan
    manual_measures_file = scan.path() / "measures.json"
    try:
        manual_measures = read_json(manual_measures_file)
    except Exception as e:
        if manual_measures_file.exists():
            self.logger.warning(
                f"Failed to load manual measures file: {manual_measures_file}"
            )
            self.logger.warning(e)
        pass
    else:
        measures["manual_angles"] = manual_measures["angles"]
        measures["manual_internodes"] = manual_measures["internodes"]

    # Make sure that the 'type' argument we got is a valid option, else default to 'all':
    if type in [
        "angles",
        "internodes",
        "fruit_points",
        "manual_angles",
        "manual_internodes",
    ]:
        return measures[type]
    else:
        return measures