Skip to main content

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 pipeline
  • schemas.py โ†’ Handles request validation and enum definitions
  • utils.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 recursively
  • by_types โ†’ Filter by extensions
  • by_names โ†’ Match exact filenames or relative paths
  • by_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:

  1. Open an issue
  2. Submit a pull request
  3. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

directory_extractor-0.1.1.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

directory_extractor-0.1.1-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

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

Hashes for directory_extractor-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3145bb627089c2fd539acd5fe357bb607f4551935f502c9f98b47f09c5dae17a
MD5 98ce022048cb6e7b0603efad5b4d8bb1
BLAKE2b-256 a06b6167e7250541c6118e80d91a49c61941d16c9ae2f430abe2ed9a3cd5ef68

See more details on using hashes here.

File details

Details for the file directory_extractor-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for directory_extractor-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d66524d74f6945f8b0168b9354cbefa806ebc5daa4be869dc104b360267c4142
MD5 5cf8257f7d7a8da58d7fbeebe7d5b383
BLAKE2b-256 eac93181f480e51ef1d5691350fba8a0c769acaf3a79255bb8b9b489bb93ab3d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page