assets
ArchiveError
Link
Bases: RuntimeError
Base class for archive‑related errors.
ExtractionError
Link
Bases: ArchiveError
Raised when an error occurs while extracting an archive.
FileUploadError
Link
Bases: RuntimeError
Raised when something goes wrong while persisting an uploaded file.
-
Reference API
server
server
services
assets
validate_upload_headers
ValidationError
Link
Bases: ArchiveError
Raised when an uploaded archive fails validation.
create_zip_for_scan
Link
Create a temporary ZIP file containing all files under scan_path (except any webcache directories).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Path
|
The root directory of the scan to archive. |
required |
|
Logger
|
Logger used for diagnostic messages. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The path to the created temporary ZIP file. |
Raises:
| Type | Description |
|---|---|
ArchiveError
|
If the ZIP file cannot be created. |
Source code in plantdb/server/services/assets.py
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 | |
extract_zip_to_scan
Link
extract_zip_to_scan(zip_path, destination, logger)
Extract zip_path into destination while performing safety checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Path
|
Path to the validated temporary ZIP file. |
required |
|
Path
|
Directory where the archive contents should be placed. |
required |
|
Logger
|
Logger for diagnostic output. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of extracted absolute file paths. |
Raises:
| Type | Description |
|---|---|
ExtractionError
|
If any step of the extraction fails. |
Notes
Safety checks include: path traversal, duplicate files, and hash verification
Source code in plantdb/server/services/assets.py
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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |
get_scan_path
Link
Resolve the absolute path of a scan.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
Database object exposing |
required |
|
str
|
Identifier of the scan. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Absolute directory of the scan. |
Source code in plantdb/server/services/assets.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
is_valid_archive
Link
is_valid_archive(archive_path)
Validate if a given archive meets specific directory and file requirements.
This function checks if the provided archive contains certain required directories and files, and verifies that the directory structure does not exceed a specified depth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str or Path
|
The path to the archive file (zip) to be validated. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Returns |
Notes
- The function currently assumes that the required directories and files are all at or above a specified depth in the archive.
- Make sure that the provided
archive_pathpoints to a valid zip file.
See Also
zipfile.ZipFile : Python's built-in module for reading and writing ZIP files.
Examples:
>>> import os
>>> from zipfile import ZipFile
>>> from plantdb.server.services.assets import is_valid_archive
>>> # Creating a valid archive for demonstration purposes
>>> with ZipFile('test_archive.zip', 'w') as zip_ref:
... zip_ref.writestr('images/', '')
... zip_ref.writestr('images/test1.jpg', '')
... zip_ref.writestr('metadata/', '')
... zip_ref.writestr('metadata/data.json', '')
... zip_ref.writestr('files.json', '')
>>> # Validate the archive
>>> is_valid_archive('test_archive.zip')
True
>>> is_valid_archive('/tmp/real_plant.zip')
True
>>> is_valid_archive('/tmp/real_plant_analyzed.zip')
True
Source code in plantdb/server/services/assets.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
validate_upload_headers
Link
validate_upload_headers(headers)
Verify that the request contains all mandatory headers and return a dictionary with normalized values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
|
required |
Returns:
| Type | Description |
|---|---|
dict
|
|
Raises:
| Type | Description |
|---|---|
FileUploadError
|
If a required header is missing or malformed. |
Source code in plantdb/server/services/assets.py
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 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
write_file
Link
write_file(file_path, data)
Write data to file_path in a single operation.
Returns:
| Type | Description |
|---|---|
int
|
Number of bytes written. |
Source code in plantdb/server/services/assets.py
157 158 159 160 161 162 163 164 165 166 167 | |
write_streamed_file
Link
write_streamed_file(file_path, content_length, chunk_size)
Persist a streamed upload. The Flask request object is accessed
globally - this function only deals with the low‑level I/O.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Path
|
Destination file. |
required |
|
int
|
Expected total size (from the |
required |
|
int
|
Size of each chunk read from |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of bytes actually written. |
Source code in plantdb/server/services/assets.py
170 171 172 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 | |