From a342ca2386c3edad9ce87a6f44c2c68c1af98c4b Mon Sep 17 00:00:00 2001 From: Afshan <33727827+AfshanKhan@users.noreply.github.com> Date: Thu, 11 Mar 2021 00:06:10 +0530 Subject: [PATCH] fix: Get file name from path (#12437) * fix: get file name from path * Update frappe/integrations/doctype/dropbox_settings/dropbox_settings.py Co-authored-by: Suraj Shetty <13928957+surajshetty3416@users.noreply.github.com> * fix: upload only files which have file url * fix: patch to fix files name and url * fix: fixing patch * refact: patch refactor * fix: missing : * fix: renamed variables * fix: refactoring patch * fix: update file exists function * fix: added patch path in patch.txt * fix: added try except * fix: Use set_value instead of set_values * style: Fix formatting Co-authored-by: Suraj Shetty <13928957+surajshetty3416@users.noreply.github.com> --- .../dropbox_settings/dropbox_settings.py | 10 +++--- frappe/patches.txt | 1 + .../rename_uploaded_files_with_proper_name.py | 31 +++++++++++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 frappe/patches/v12_0/rename_uploaded_files_with_proper_name.py diff --git a/frappe/integrations/doctype/dropbox_settings/dropbox_settings.py b/frappe/integrations/doctype/dropbox_settings/dropbox_settings.py index 71445b44d7..09da1ecc42 100644 --- a/frappe/integrations/doctype/dropbox_settings/dropbox_settings.py +++ b/frappe/integrations/doctype/dropbox_settings/dropbox_settings.py @@ -131,12 +131,10 @@ def upload_from_folder(path, is_private, dropbox_folder, dropbox_client, did_not for f in frappe.get_all("File", filters={"is_folder": 0, "is_private": is_private, "uploaded_to_dropbox": 0}, fields=['file_url', 'name', 'file_name']): - if is_private: - filename = f.file_url.replace('/private/files/', '') - else: - if not f.file_url: - f.file_url = '/files/' + f.file_name; - filename = f.file_url.replace('/files/', '') + if not f.file_url: + continue + filename = f.file_url.rsplit('/', 1)[-1] + filepath = os.path.join(path, filename) if filename in ignore_list: diff --git a/frappe/patches.txt b/frappe/patches.txt index a3f60ca210..6e94bf0adc 100644 --- a/frappe/patches.txt +++ b/frappe/patches.txt @@ -332,3 +332,4 @@ frappe.patches.v13_0.rename_desk_page_to_workspace # 02.02.2021 frappe.patches.v13_0.delete_package_publish_tool frappe.patches.v13_0.rename_list_view_setting_to_list_view_settings frappe.patches.v13_0.remove_twilio_settings +frappe.patches.v12_0.rename_uploaded_files_with_proper_name diff --git a/frappe/patches/v12_0/rename_uploaded_files_with_proper_name.py b/frappe/patches/v12_0/rename_uploaded_files_with_proper_name.py new file mode 100644 index 0000000000..854a381e1c --- /dev/null +++ b/frappe/patches/v12_0/rename_uploaded_files_with_proper_name.py @@ -0,0 +1,31 @@ +import frappe +import os + +def execute(): + file_names_with_url = frappe.get_all("File", filters={ + "is_folder": 0, + "file_name": ["like", "%/%"] + }, fields=['name', 'file_name', 'file_url']) + + for f in file_names_with_url: + filename = f.file_name.rsplit('/', 1)[-1] + + if not f.file_url: + f.file_url = f.file_name + + try: + if not file_exists(f.file_url): + continue + frappe.db.set_value('File', f.name, { + "file_name": filename, + "file_url": f.file_url + }, update_modified=False) + except Exception: + continue + +def file_exists(file_path): + file_path = frappe.utils.get_files_path( + file_path.rsplit('/', 1)[-1], + is_private=file_path.startswith('/private') + ) + return os.path.exists(file_path)