Skip to main content

A Flask library for secure file uploads and deletions

Project description

Imhotep Files For Flask

This Python library simplifies file upload and deletion operations in web applications using Flask or similar frameworks. It includes two core functions:

upload_file: Handles file uploads with validation for allowed extensions and secure file saving. delete_file: Safely deletes files from the server.

Features

  • Secure filename handling with werkzeug.utils.secure_filename.
  • Validation for allowed file extensions.
  • Error handling and debugging support for various scenarios.
  • Clean and customizable file naming convention.
  • Safe deletion of files with error handling.

Installation

Before using the library, ensure you have the following dependencies installed:

pip install imhotep-files-flask==0.1

You Can make sure of the installation using

imhotep-files-flask

and you will see on the terminal

Hello from imhotep_files_flask!

Usage

  1. Uploading a File The upload_file function handles uploading files to a specified folder with validations such as allowed extensions, correct file name format, and ensuring a file is provided.
upload_file(request, upload_folder, allowed_extensions, file_name)

Parameters:

  • request: The incoming HTTP request object that contains the file.
  • upload_folder: The target folder path where the file will be saved.
  • allowed_extensions: A list of allowed file extensions (e.g., ['.jpg', '.png', '.txt']).
  • file_name: The base name for the file (without extension).

Returns:

  • The full path to the uploaded file if successful.
  • Error message if there is an issue.
  1. Deleting a File The delete_file function handles removing files from the filesystem with safety checks and error handling.
delete_file(file_path):

Parameters: file_path: The full path to the file that needs to be deleted. Returns: A success message if the file is deleted. Error message if there is an issue (e.g., file does not exist).

Function Details

upload_file(request, upload_folder, allowed_extensions, file_name) This function is responsible for handling file uploads with the following steps:

  1. Validates that allowed extensions and upload folder are provided.
  2. Ensures the file name does not include an extension or start with a dot.
  3. Checks for the presence of a file in the request.
  4. Validates the file���s extension.
  5. Saves the file in the specified upload folder with a secure name. Example Flow:
  • User uploads a .jpg file named picture.jpg.
  • You pass 'profile_picture' as file_name, so the file will be saved as profile_picture.jpg in the upload folder.

delete_file(file_path) This function safely deletes a file from the given path. It performs the following:

  1. Checks if the file path is valid.
  2. Ensures the file exists.
  3. Tries to delete the file and handles any exceptions.

Example

  1. Uploading a File
    from flask import Flask, request
    from imhotep_files_flask import upload_file
    @app.route("/file_upload", methods=["POST"])
    def file_upload():
         user_id = 1
    
         photo_path, upload_error = upload_file(request, "path/to/your/upload/file/directory" , (".png", ".jpg", ".jpeg"), user_id)
         if upload_error:
             print(upload_error)
             return upload_error
         if photo_path:
         # File uploaded successfully, do something with the file path
         return "File uploaded successfully!"
    
  2. Deleting a File
    from flask import Flask, request
    from imhotep_files_flask import delete_file
    @app.route("/file_delete", methods=["POST"])
    def file_delete():
       file_path = "path/to/the/file/you/wants/to/delete"
    
       file_delete , error = delete_file(file_path)
       if error:
          print(error)
          return error
       if file_delete:
       # File deleted successfully, do something pn the database for example
       return "File deleted successfully!"
    

Error Handling

Both functions return clear error messages in case of failure:

  • upload_file returns descriptive error messages such as "No file uploaded", "Invalid file format", or "File name parameter should not include an extension".
  • delete_file returns errors like "File path must be a non-empty string" or "Error deleting file: ".

Contributing

Feel free to open issues or submit pull requests if you find any bugs or have suggestions for improvements.

License

This project is licensed under the MIT License.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

imhotep_files_flask-0.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

imhotep_files_flask-0.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file imhotep_files_flask-0.1.tar.gz.

File metadata

  • Download URL: imhotep_files_flask-0.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.0rc1

File hashes

Hashes for imhotep_files_flask-0.1.tar.gz
Algorithm Hash digest
SHA256 7e8a511e9960b5795f106dff04068ecaff57671f61fe1a44dedd17b7b916dfc3
MD5 847fbe48510703951e238079f2dc8be9
BLAKE2b-256 12c339104e075279af637d42a106974b5b94ebc30fa0afe60daf525101bb3507

See more details on using hashes here.

File details

Details for the file imhotep_files_flask-0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for imhotep_files_flask-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1dd3fbfb4f0a672a46fab78c25b9a0fcdd220058768d62e52b3ada33072981d1
MD5 2da9131af3fa639a70819903c5c24be1
BLAKE2b-256 b4932932e126b71f07981409e947e643ecd61c99d8212568e1e5a094ca22a181

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