Ip logging
This commit is contained in:
+17
-1
@@ -21,7 +21,7 @@ from .utils.sentry import initialize_sentry
|
||||
initialize_sentry()
|
||||
|
||||
from .config import IMAGE_EXTENSIONS, RESULT_FOLDER, WORKER_FILES
|
||||
from .models import Image, Submission, cleanup_old_entries, db
|
||||
from .models import Image, Submission, UploadLog, cleanup_old_entries, db
|
||||
|
||||
|
||||
def create_app() -> Flask:
|
||||
@@ -113,6 +113,22 @@ def create_app() -> Flask:
|
||||
submission_hash = hashlib.md5(submission_data).hexdigest()
|
||||
submission_path = RESULT_FOLDER / img_hash / submission_hash
|
||||
|
||||
# Log upload to database
|
||||
client_ip = request.headers.get("X-Forwarded-For", request.remote_addr)
|
||||
if client_ip and "," in client_ip:
|
||||
client_ip = client_ip.split(",")[0].strip()
|
||||
user_agent = request.headers.get("User-Agent", "Unknown")
|
||||
|
||||
upload_log = UploadLog(
|
||||
ip_address=client_ip,
|
||||
user_agent=user_agent,
|
||||
image_hash=img_hash,
|
||||
submission_hash=submission_hash,
|
||||
filename=image.filename,
|
||||
)
|
||||
db.session.add(upload_log) # pylint: disable=no-member
|
||||
db.session.commit() # pylint: disable=no-member
|
||||
|
||||
# Check if hash is present on the DB
|
||||
submission: Submission = Submission.query.filter_by(hash=submission_hash).first()
|
||||
|
||||
|
||||
@@ -104,6 +104,18 @@ class IHDR(db.Model): # type: ignore
|
||||
return zlib.crc32(b"IHDR" + ihdr_data) & 0xFFFFFFFF
|
||||
|
||||
|
||||
class UploadLog(db.Model): # type: ignore
|
||||
"""Model representing upload activity logs."""
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
ip_address = Column(String(45), nullable=False) # IPv6 max length is 45
|
||||
user_agent = Column(String(512), nullable=True)
|
||||
upload_time = Column(DateTime, nullable=False, default=lambda: datetime.now(timezone.utc))
|
||||
image_hash = Column(String(64), nullable=False)
|
||||
submission_hash = Column(String(128), nullable=True)
|
||||
filename = Column(String(128), nullable=True)
|
||||
|
||||
|
||||
def fill_ihdr_db() -> None:
|
||||
"""
|
||||
Populate IHDR table with common PNG configurations.
|
||||
|
||||
Reference in New Issue
Block a user