utils
PlantDB Utility FunctionsLink
A small collection of helper utilities used throughout the PlantDB project to simplify common filesystem-related tasks. These functions handle safe retrieval of database resources, verify path containment, and inspect the contents of ZIP archives, reducing boiler-plate and ensuring consistent error handling across the code base.
Key FeaturesLink
- resource_file: Retrieves a
Fileobject from the PlantDB database, translating common error conditions into clear JSON-style messages and HTTP status codes. - is_within_directory: Checks whether a target path lies inside a given directory, preventing directory-traversal vulnerabilities.
- is_directory_in_archive: Determines if a specific top-level directory exists inside a ZIP archive, useful for validating package structures before extraction.
Usage ExamplesLink
from plantdb.server.api.utils import resource_file, is_within_directory, is_directory_in_archive
Retrieve a file from the database (returns a File object or an error dict)Link
result = resource_file(db, "scan123", "segmentation")
Verify a path is under a base directoryLink
is_within_directory("/data/plantdb", "/data/plantdb/scans/scan123") True
Check for a directory named 'images' inside a zip fileLink
is_directory_in_archive("dataset.zip", "images") True
is_directory_in_archive
Link
is_directory_in_archive(archive_path, target_dir)
Check if a specific directory exists within an archive file.
This function checks whether a given directory is present at the top level of a ZIP archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str or Path
|
The path to the ZIP archive file. |
required |
|
str
|
The name of the target directory to check for within the archive. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the target directory exists at the top level of the archive, False otherwise. |
Source code in plantdb/server/api/utils.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
is_within_directory
Link
Check if a target path is within a directory.
This function determines if the absolute path of the target is located
within the absolute path of the directory. It uses os.path.commonpath
to perform the comparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str or Path
|
The path to the directory to check against. |
required |
|
str or Path
|
The path to the target to check if it resides within the directory. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/server/api/utils.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
resource_file
Link
Retrieve a specific File object from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
The database instance. |
required |
|
str
|
Identifier of the scan containing the requested file. |
required |
|
str
|
Name of the task (fileset) and file to fetch. |
required |
|
Additional arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
tuple
|
Either |
Source code in plantdb/server/api/utils.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |