Skip to content

Commit 5090afd

Browse files
committed
Change filename generation to use an incremental counter for maintaining upload order
1 parent 957b529 commit 5090afd

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

backend/server.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import os
22
import sys
33
from flask import Flask, request, jsonify
4-
import uuid
54
import cv2
6-
75
import importlib.util
8-
import sys
96
from pathlib import Path
107

118
module_path = Path(__file__).resolve().parent.parent / 'AI' / 'main.py'
@@ -26,12 +23,14 @@
2623
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
2724
app.config['ALLOWED_EXTENSIONS'] = ['jpg', 'png', 'jpeg']
2825

26+
cod_counter = 1
2927
def check_filename(filename):
3028
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
3129

3230

3331
@app.route('/upload', methods=['POST'])
3432
def upload_file():
33+
global cod_counter
3534
# verific daca in cerere exista un fisier.
3635
if 'file' not in request.files:
3736
return jsonify({'error': 'No file part'}), 400
@@ -43,14 +42,14 @@ def upload_file():
4342
return jsonify({'error': 'No selected file'}), 400
4443

4544
if file and check_filename(file.filename):
46-
cod_unic = uuid.uuid4().hex
4745
# am creat un hash unic pe care sa il apenduiesc in fata numelui fisierului, pentru
4846
# ca daca il lasam fara, fisierele nu mai aveau denumiri distincte, si folderul
4947
# ramanea mereu doar cu o poza, la fiecare incarcare a camerei din flutter.
50-
filename = f"{cod_unic}{file.filename}"
48+
filename = f"{cod_counter}_{file.filename}"
5149
# concatenez cu un contor pentru ca altfel, imi punea pozele
5250
# distincte ca acelasi obiect file in folder.
5351
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
52+
cod_counter += 1
5453
return jsonify({'message': 'File uploaded successfully!'}), 200
5554
else:
5655
return jsonify({'error': 'File type not allowed'}), 400

0 commit comments

Comments
 (0)