seitime-frappe/frappe/patches/v4_0/file_manager_hooks.py
Chinmay Pai 8943f6cfd5
file-api: migration improvements and fixes
* migrate more functions to file class
* add get_content(), returns file content from file_name
* move get_file_path() to get_full_path() to decrease naming ambiguity

Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>
2018-09-06 17:30:33 +05:30

37 lines
1.1 KiB
Python

# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
from __future__ import unicode_literals, print_function
import frappe
import os
from frappe.utils import get_files_path
from frappe.core.doctype.file.file import get_content_hash
def execute():
frappe.reload_doc('core', 'doctype', 'file_data')
for name, file_name, file_url in frappe.db.sql(
"""select name, file_name, file_url from `tabFile`
where file_name is not null"""):
b = frappe.get_doc('File', name)
old_file_name = b.file_name
b.file_name = os.path.basename(old_file_name)
if old_file_name.startswith('files/') or old_file_name.startswith('/files/'):
b.file_url = os.path.normpath('/' + old_file_name)
else:
b.file_url = os.path.normpath('/files/' + old_file_name)
try:
_file = frappe.get_doc("File", {"file_name": name})
content = _file.get_content()
b.content_hash = get_content_hash(content)
except IOError:
print('Warning: Error processing ', name)
_file_name = old_file_name
b.content_hash = None
try:
b.save()
except frappe.DuplicateEntryError:
frappe.delete_doc(b.doctype, b.name)