This project implements a WebSocket service using Django that allows clients to send a file to the server. The server then identifies the file's extension and returns this information to the client.
- WebSocket Setup: Implements the WebSocket connection using Django Channels.
- File Processing: Accepts a file over the WebSocket, determines its extension, and communicates this back to the client.
- Security Measures: Addresses common security concerns to ensure safe file handling.
- Documentation: Thoroughly documents the setup, usage, dependencies, and interaction with the WebSocket service.
- Python 3.12
-
Clone the repository:
git clone https://github.com/KarimTayie/file-extension-websocket.git cd file-extension-websocket
-
Create a virtual environment:
python3 -m venv env source env/bin/activate # Linux/Mac # OR .\env\Scripts\activate # Windows
-
Install dependencies:
pip install -r requirements.txt
This project relies on
python-magic
. Ensure thatlibmagic
is installed on your system:-
Debian/Ubuntu:
sudo apt-get install libmagic1
-
Windows: You'll need DLLs for libmagic. You can fetch them with:
pip install python-magic-bin
-
macOS:
- When using Homebrew:
brew install libmagic
- When using MacPorts:
port install file
- When using Homebrew:
-
-
Set up environment variables:
Create a
.env
file in the project root directory and define the following variables:SECRET_KEY=<your_secret_key> DEBUG=True MAX_ALLOWED_SIZE=<max_allowed_size_in_megabytes>
-
Start the Django development server:
python manage.py runserver
-
Navigate to
http://localhost:8000/fe/upload/
in your web browser to access the file upload form. -
Choose a file and click the "Send File" button. The file extension will be identified and displayed on the page.
- The WebSocket endpoint for file extension identification is located at
ws://localhost:8000/ws/file_extension/
.
- The maximum allowed size for uploaded files is defined by the
MAX_ALLOWED_SIZE
environment variable in the.env
file. - Files exceeding the maximum allowed size will result in a validation error.
- File extensions are identified using the
python-magic
library along with MIME type detection.