Secure and extensible directory extraction pipeline with filtering and guardrails
Project description
๐ Directory Extraction Mechanism Using Python
๐ This is my first publicly published Python package on PyPI . I built this project to explore secure filesystem handling, clean architecture, and reusable backend utilities. I truly appreciate any feedback, suggestions, improvements, or contributions from the community!
๐ฆ Install from PyPI
You can now install it directly:
pip install directory-extractor
If you want to run the FastAPI server (if CLI enabled):
pip install directory-extractor[api]
directory-extractor-api
๐ About The Project
A robust, secure, and flexible directory scanning pipeline built with FastAPI that extracts files from a directory using multiple filtering strategies.
It supports:
- โ Multiple selection modes (all / by types / by names / by patterns)
- โ Exclusion filters
- โ Safety guardrails (size, date range, result limit)
- โ Secure path validation (prevents directory traversal)
- โ Clean structured JSON response with detailed stats
This project was designed with security, clarity, and extensibility in mind.
๐ Features
๐ 1. Multiple Selection Modes
You can extract files using different strategies:
| Mode | Description |
|---|---|
all |
Return all files recursively |
by_types |
Filter by file extensions (pdf, md, docx, etc.) |
by_names |
Select exact filenames or relative paths |
by_patterns |
Use glob patterns like **/*.md |
๐ก 2. Built-in Guardrails
After selection and exclusion, files are validated against:
- ๐ Maximum file size (
max_file_size_mb) - ๐
Modification time window (
modified_after,modified_before) - ๐ข Maximum number of results (
limit) - โ Files deleted during processing (tracked safely)
๐ 3. Security First
- Uses
Path.resolve()to prevent directory traversal attacks - Rejects absolute glob patterns
- Ensures all files remain inside the target directory
- Avoids symlink loops
- UTC-normalized time comparisons
๐ Project Structure
src/
โโโ directory_extractor/
โโโ api.py # FastAPI router & pipeline orchestration
โโโ schemas.py # Pydantic models & enums
โโโ utils.py # Core file collection & filtering logic
File Responsibilities
api.pyโ Exposes the FastAPI endpoint and orchestrates the 4-step pipelineschemas.pyโ Handles request validation and enum definitionsutils.pyโ Contains reusable filesystem logic and guardrails
The separation ensures the file-handling logic remains reusable and testable.
โ๏ธ How the Pipeline Works
The extraction follows a 4-step processing pipeline :
1๏ธโฃ SELECT files (based on mode)
2๏ธโฃ APPLY EXCLUSIONS
3๏ธโฃ APPLY GUARDRAILS
4๏ธโฃ FORMAT RESPONSE
Step 1 โ Selection
Depending on mode:
allโ Collect everything recursivelyby_typesโ Filter by extensionsby_namesโ Match exact filenames or relative pathsby_patternsโ Match glob patterns
Step 2 โ Exclusions
Optional exclude_names supports:
- Relative paths โ
docs/README.md - Bare filenames โ
temp.txt - Case-insensitive matching (optional)
Step 3 โ Guardrails
Applies:
- File size limit
- Date filtering (UTC normalized)
- Result cap
- Tracks skipped files with reasons
Step 4 โ Response Formatting
Returns:
- Total candidates
- Exclusion stats
- Guardrail skip stats
- Final selected files (relative + absolute paths)
๐ง API Usage
Endpoint
POST /directory/
Example Request
{
"directory": "/home/user/docs",
"mode": "by_types",
"file_types": ["pdf", "md"],
"max_file_size_mb": 10,
"limit": 50
}
Example Response
{
"total_candidates": 127,
"post_exclude_candidates": 115,
"total_selected": 42,
"selected_files": [
"README.md",
"docs/guide.pdf"
]
}
๐ Request Model Overview
Required
| Field | Description |
|---|---|
directory |
Root directory to scan |
Selection Options
| Field | Used When |
|---|---|
mode |
Always |
file_types |
by_types |
file_names |
by_names |
include_globs |
by_patterns |
Guardrails
| Field | Purpose |
|---|---|
max_file_size_mb |
Skip large files |
modified_after |
Include newer files |
modified_before |
Include older files |
limit |
Maximum result count |
๐งช Example Use Cases
โ Index all files
{
"directory": "/data"
}
โ Only PDFs modified after Jan 1, 2024
{
"directory": "/data",
"mode": "by_types",
"file_types": ["pdf"],
"modified_after": "2024-01-01T00:00:00"
}
๐ Why This Project Matters
This project demonstrates:
- Clean architecture separation
- Secure filesystem handling
- Production-style validation with Pydantic
- Observability through structured stats
- Thread-safe handling of blocking file I/O
- Reusable package design
๐ฎ Future Enhancements
I would love to expand this further. Some ideas:
- Async file scanning
- Streaming large directory results
- Directory indexing cache
- File hashing support
- Logging & observability hooks
- CLI enhancements
Suggestions are welcome!
๐ค Contributing
This is my first public package, and I am actively learning.
If you find:
- Bugs ๐
- Improvements ๐ก
- Performance enhancements โก
- Security suggestions ๐
- Documentation improvements ๐
Please feel free to:
- Open an issue
- Submit a pull request
- Share feedback
Your support means a lot ๐
๐งพ License
Open for research, experimentation, and development use.
๐ค Author
Created by Siddharth Singh
๐ LinkedIn: https://www.linkedin.com/in/siddharth-singh-021b34193/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file directory_extractor-0.1.1.tar.gz.
File metadata
- Download URL: directory_extractor-0.1.1.tar.gz
- Upload date:
- Size: 17.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3145bb627089c2fd539acd5fe357bb607f4551935f502c9f98b47f09c5dae17a
|
|
| MD5 |
98ce022048cb6e7b0603efad5b4d8bb1
|
|
| BLAKE2b-256 |
a06b6167e7250541c6118e80d91a49c61941d16c9ae2f430abe2ed9a3cd5ef68
|
File details
Details for the file directory_extractor-0.1.1-py3-none-any.whl.
File metadata
- Download URL: directory_extractor-0.1.1-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d66524d74f6945f8b0168b9354cbefa806ebc5daa4be869dc104b360267c4142
|
|
| MD5 |
5cf8257f7d7a8da58d7fbeebe7d5b383
|
|
| BLAKE2b-256 |
eac93181f480e51ef1d5691350fba8a0c769acaf3a79255bb8b9b489bb93ab3d
|