Refactor photo upload logic to improve file path handling and remove unused imports
This commit is contained in:
parent
a2b035f522
commit
7bdbb4ea2a
1 changed files with 5 additions and 3 deletions
|
@ -1,7 +1,7 @@
|
||||||
import os
|
import os
|
||||||
|
import posixpath
|
||||||
|
|
||||||
import datetime
|
from flask import Blueprint, current_app, request, jsonify
|
||||||
from flask import Blueprint, current_app, request, jsonify, send_from_directory
|
|
||||||
|
|
||||||
from .helpers import generate_hashed_filename, get_photo_attachable_class_by_name
|
from .helpers import generate_hashed_filename, get_photo_attachable_class_by_name
|
||||||
from .. import db
|
from .. import db
|
||||||
|
@ -12,7 +12,8 @@ photo_bp = Blueprint("photo_api", __name__)
|
||||||
def save_photo(file, model: str) -> str:
|
def save_photo(file, model: str) -> str:
|
||||||
assert current_app.static_folder
|
assert current_app.static_folder
|
||||||
|
|
||||||
rel_path = generate_hashed_filename(file, model)
|
hashed_name = generate_hashed_filename(file, model)
|
||||||
|
rel_path = posixpath.join("uploads", "photos", hashed_name)
|
||||||
abs_path = os.path.join(current_app.static_folder, "uploads", "photos", rel_path)
|
abs_path = os.path.join(current_app.static_folder, "uploads", "photos", rel_path)
|
||||||
|
|
||||||
dir_path = os.path.dirname(abs_path)
|
dir_path = os.path.dirname(abs_path)
|
||||||
|
@ -49,6 +50,7 @@ def upload_photo():
|
||||||
|
|
||||||
# Save file
|
# Save file
|
||||||
rel_path = save_photo(file, model)
|
rel_path = save_photo(file, model)
|
||||||
|
print(rel_path)
|
||||||
|
|
||||||
# Create Photo row
|
# Create Photo row
|
||||||
photo = Photo(filename=rel_path, caption=caption)
|
photo = Photo(filename=rel_path, caption=caption)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue