Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
venv/
Binary file added __pycache__/app.cpython-313.pyc
Binary file not shown.
Binary file added __pycache__/config.cpython-313.pyc
Binary file not shown.
Binary file added __pycache__/extensions.cpython-313.pyc
Binary file not shown.
35 changes: 35 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os
import logging
from flask import Flask
from config import Config
from extensions import db

def create_app():
app = Flask(__name__)
app.config.from_object(Config)

# Initialize db with app
db.init_app(app)

# Configure logging
os.makedirs(app.config['LOG_FOLDER'], exist_ok=True)
logging.basicConfig(
filename=f"{app.config['LOG_FOLDER']}/app.log",
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s'
)

# Register blueprints
from controllers.file_controller import file_bp
app.register_blueprint(file_bp)

# Create tables
with app.app_context():
db.create_all()

return app

app = create_app()

if __name__ == '__main__':
app.run(debug=True)
14 changes: 14 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
from dotenv import load_dotenv

load_dotenv()

class Config:
SQLALCHEMY_DATABASE_URI = (
f"mysql+pymysql://{os.getenv('DB_USER')}:{os.getenv('DB_PASSWORD')}@"
f"{os.getenv('DB_HOST')}:{os.getenv('DB_PORT')}/{os.getenv('DB_NAME')}"
)
SQLALCHEMY_TRACK_MODIFICATIONS = False
UPLOAD_FOLDER = os.getenv('UPLOAD_FOLDER', 'uploads')
LOG_FOLDER = 'logs'
DEBUG = True
Binary file not shown.
53 changes: 53 additions & 0 deletions controllers/file_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import uuid
import logging
from flask import Blueprint, request, jsonify, send_file
from services.file_service import save_file, update_file, delete_file
from models.uploaded_file import UploadedFile
from config import Config

file_bp = Blueprint('file_bp', __name__)

@file_bp.route('/upload', methods=['POST'])
def upload_file():
try:
if 'file' not in request.files:
return jsonify({'message': 'No file uploaded'}), 400
file = request.files['file']
folder = f"{Config.UPLOAD_FOLDER}/{uuid.uuid4().hex}"
uploaded = save_file(file, folder)
return jsonify({'message': 'Upload successful', 'id': uploaded.id, 'path': uploaded.file_path}), 201
except Exception as e:
logging.error(f"Upload error: {e}")
return jsonify({'message': str(e)}), 400

@file_bp.route('/file/<int:file_id>', methods=['GET'])
def get_file(file_id):
try:
file = UploadedFile.query.get(file_id)
if not file:
return jsonify({'message': 'File not found'}), 404
return send_file(file.file_path, as_attachment=True)
except Exception as e:
logging.error(f"Get error: {e}")
return jsonify({'message': str(e)}), 400

@file_bp.route('/file/<int:file_id>', methods=['PUT'])
def update_existing_file(file_id):
try:
if 'file' not in request.files:
return jsonify({'message': 'No file provided'}), 400
file = request.files['file']
updated = update_file(file_id, file, Config.UPLOAD_FOLDER)
return jsonify({'message': 'File updated successfully', 'path': updated.file_path}), 200
except Exception as e:
logging.error(f"Update error: {e}")
return jsonify({'message': str(e)}), 400

@file_bp.route('/file/<int:file_id>', methods=['DELETE'])
def delete_existing_file(file_id):
try:
delete_file(file_id)
return jsonify({'message': 'File deleted successfully'}), 200
except Exception as e:
logging.error(f"Delete error: {e}")
return jsonify({'message': str(e)}), 400
3 changes: 3 additions & 0 deletions extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
52 changes: 52 additions & 0 deletions logs/app.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
2025-10-18 11:28:33,106 [INFO] WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
2025-10-18 11:28:33,107 [INFO] Press CTRL+C to quit
2025-10-18 11:28:33,108 [INFO] * Restarting with stat
2025-10-18 11:28:33,731 [WARNING] * Debugger is active!
2025-10-18 11:28:33,768 [INFO] * Debugger PIN: 125-425-247
2025-10-18 11:31:34,041 [INFO] 127.0.0.1 - - [18/Oct/2025 11:31:34] "POST /upload HTTP/1.1" 400 -
2025-10-18 11:37:48,094 [INFO] 127.0.0.1 - - [18/Oct/2025 11:37:48] "POST /upload HTTP/1.1" 400 -
2025-10-18 11:39:04,317 [INFO] 127.0.0.1 - - [18/Oct/2025 11:39:04] "POST /upload HTTP/1.1" 400 -
2025-10-18 11:40:22,064 [INFO] File saved: uploads/252487abf6e14a39bfa40b72412da69e/1aec644281434ee2a219a2f2f8e3a5b1_23790439261-2.pdf
2025-10-18 11:40:22,072 [INFO] 127.0.0.1 - - [18/Oct/2025 11:40:22] "POST /upload HTTP/1.1" 201 -
2025-10-18 11:47:02,491 [INFO] 127.0.0.1 - - [18/Oct/2025 11:47:02] "GET /file/1 HTTP/1.1" 200 -
2025-10-18 11:48:51,208 [ERROR] Update error: name 'app' is not defined
2025-10-18 11:48:51,210 [INFO] 127.0.0.1 - - [18/Oct/2025 11:48:51] "PUT /file/1 HTTP/1.1" 400 -
2025-10-18 11:49:15,582 [ERROR] Update error: name 'app' is not defined
2025-10-18 11:49:15,583 [INFO] 127.0.0.1 - - [18/Oct/2025 11:49:15] "PUT /file/1 HTTP/1.1" 400 -
2025-10-18 11:52:23,076 [INFO] * Detected change in '/Users/Zander/flask_file_upload_mvc/controllers/file_controller.py', reloading
2025-10-18 11:52:23,477 [INFO] * Restarting with stat
2025-10-18 11:52:24,544 [WARNING] * Debugger is active!
2025-10-18 11:52:24,573 [INFO] * Debugger PIN: 125-425-247
2025-10-18 11:52:25,660 [INFO] * Detected change in '/Users/Zander/flask_file_upload_mvc/controllers/file_controller.py', reloading
2025-10-18 11:52:25,849 [INFO] * Restarting with stat
2025-10-18 11:52:26,589 [WARNING] * Debugger is active!
2025-10-18 11:52:26,606 [INFO] * Debugger PIN: 125-425-247
2025-10-18 11:52:57,097 [INFO] WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
2025-10-18 11:52:57,098 [INFO] Press CTRL+C to quit
2025-10-18 11:52:57,099 [INFO] * Restarting with stat
2025-10-18 11:52:57,766 [WARNING] * Debugger is active!
2025-10-18 11:52:57,788 [INFO] * Debugger PIN: 125-425-247
2025-10-18 11:53:08,816 [INFO] File saved: uploads/7d00800752d64e77bce7e5cb0a37a406_ERD_Dormitally.pdf
2025-10-18 11:53:08,831 [INFO] File updated: ID=1
2025-10-18 11:53:08,834 [INFO] 127.0.0.1 - - [18/Oct/2025 11:53:08] "PUT /file/1 HTTP/1.1" 200 -
2025-10-18 11:53:41,896 [INFO] File deleted from storage: uploads/7d00800752d64e77bce7e5cb0a37a406_ERD_Dormitally.pdf
2025-10-18 11:53:41,901 [INFO] Record deleted: ID=1
2025-10-18 11:53:41,902 [INFO] 127.0.0.1 - - [18/Oct/2025 11:53:41] "DELETE /file/1 HTTP/1.1" 200 -
2025-10-19 21:53:39,376 [INFO] WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
2025-10-19 21:53:39,376 [INFO] Press CTRL+C to quit
2025-10-19 21:53:39,378 [INFO] * Restarting with stat
2025-10-19 21:53:39,891 [WARNING] * Debugger is active!
2025-10-19 21:53:39,924 [INFO] * Debugger PIN: 820-901-174
2025-10-19 22:00:48,564 [INFO] File saved: uploads/7f799bf24a634687a65c7b953243be31/90c1b158498d4b4783853be14fd6035b_ERD_Dormitally.pdf
2025-10-19 22:00:48,570 [INFO] 127.0.0.1 - - [19/Oct/2025 22:00:48] "POST /upload HTTP/1.1" 201 -
2025-10-19 22:09:16,689 [INFO] 127.0.0.1 - - [19/Oct/2025 22:09:16] "GET /3 HTTP/1.1" 404 -
2025-10-19 22:09:48,774 [INFO] 127.0.0.1 - - [19/Oct/2025 22:09:48] "GET /file/3 HTTP/1.1" 200 -
2025-10-19 22:10:14,576 [INFO] File saved: uploads/04a938cda90548a8bb5b55b271b5b2b1_23790439261.pdf
2025-10-19 22:10:14,594 [INFO] File updated: ID=3
2025-10-19 22:10:14,597 [INFO] 127.0.0.1 - - [19/Oct/2025 22:10:14] "PUT /file/3 HTTP/1.1" 200 -
2025-10-19 22:10:28,744 [INFO] File deleted from storage: uploads/04a938cda90548a8bb5b55b271b5b2b1_23790439261.pdf
2025-10-19 22:10:28,749 [INFO] Record deleted: ID=3
2025-10-19 22:10:28,750 [INFO] 127.0.0.1 - - [19/Oct/2025 22:10:28] "DELETE /file/3 HTTP/1.1" 200 -
Binary file added models/__pycache__/uploaded_file.cpython-313.pyc
Binary file not shown.
6 changes: 6 additions & 0 deletions models/uploaded_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from extensions import db

class UploadedFile(db.Model):
id = db.Column(db.Integer, primary_key=True)
filename = db.Column(db.String(255), nullable=False)
file_path = db.Column(db.String(255), nullable=False)
13 changes: 13 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
blinker==1.9.0
click==8.3.0
Flask==3.1.2
Flask-SQLAlchemy==3.1.1
greenlet==3.2.4
itsdangerous==2.2.0
Jinja2==3.1.6
MarkupSafe==3.0.3
PyMySQL==1.1.2
python-dotenv==1.1.1
SQLAlchemy==2.0.44
typing_extensions==4.15.0
Werkzeug==3.1.3
Binary file added services/__pycache__/file_service.cpython-313.pyc
Binary file not shown.
58 changes: 58 additions & 0 deletions services/file_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import os
import uuid
import logging
from werkzeug.utils import secure_filename
from models.uploaded_file import UploadedFile
from extensions import db

ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'pdf', 'txt'}

def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

def save_file(file, base_folder):
if not allowed_file(file.filename):
raise ValueError('File type not allowed')

os.makedirs(base_folder, exist_ok=True)
filename = secure_filename(file.filename)
unique_name = f"{uuid.uuid4().hex}_{filename}"
save_path = os.path.join(base_folder, unique_name)
file.save(save_path)

new_file = UploadedFile(filename=unique_name, file_path=save_path)
db.session.add(new_file)
db.session.commit()

logging.info(f"File saved: {save_path}")
return new_file

def update_file(file_id, new_file, base_folder):
existing = UploadedFile.query.get(file_id)
if not existing:
raise ValueError('File not found')

# remove old file if exists
if os.path.exists(existing.file_path):
os.remove(existing.file_path)

new_record = save_file(new_file, base_folder)
existing.filename = new_record.filename
existing.file_path = new_record.file_path
db.session.commit()

logging.info(f"File updated: ID={file_id}")
return existing

def delete_file(file_id):
file_record = UploadedFile.query.get(file_id)
if not file_record:
raise ValueError('File not found')

if os.path.exists(file_record.file_path):
os.remove(file_record.file_path)
logging.info(f"File deleted from storage: {file_record.file_path}")

db.session.delete(file_record)
db.session.commit()
logging.info(f"Record deleted: ID={file_id}")