diff --git a/my_app/helper/file_naming.py b/my_app/helper/file_naming.py index 4658428..de5a12b 100644 --- a/my_app/helper/file_naming.py +++ b/my_app/helper/file_naming.py @@ -7,7 +7,13 @@ def file_retitling(original_file_url: str, folder_suffix: str, name: str) -> dict: """ Renames the uploaded video filename using the generated name for that video_info record, - and returns file metadata as a dictionary. + moves it into a sub-folder, and returns file metadata as a dictionary. + + Also updates the Frappe File doctype record so that Frappe's internal file + management stays in sync with the new physical location. Without this step, + the old File record retains the original (now broken) file_url, causing + Frappe to treat the attachment as missing and potentially re-upload the file + on the next save — resulting in duplicate entries under /files/original/. Args: original_file_url (str): The URL to the original uploaded file (e.g., /files/sample123.mp4). @@ -39,6 +45,16 @@ def file_retitling(original_file_url: str, folder_suffix: str, name: str) -> dic new_file_url_doc = f"/files/{folder_suffix}/{new_filename}" + # Sync the Frappe File doctype record to the new location. + # shutil.move() updates only the physical file; without updating the File + # record, Frappe still holds a reference to the old (now missing) path. + # On the next document save Frappe may re-upload the file, producing a + # duplicate entry in /files/original/. + file_doc_name = frappe.db.get_value("File", {"file_url": original_file_url}, "name") + if file_doc_name: + frappe.db.set_value("File", file_doc_name, "file_url", new_file_url_doc) + frappe.db.commit() + file_info = { "original_filename": original_filename, "original_filepath": original_path,