Pārlūkot izejas kodu

filemanager fixes

version-14
Pratik Vyas pirms 11 gadiem
vecāks
revīzija
a257d47a76
2 mainītis faili ar 15 papildinājumiem un 16 dzēšanām
  1. +2
    -2
      frappe/utils/__init__.py
  2. +13
    -14
      frappe/utils/file_manager.py

+ 2
- 2
frappe/utils/__init__.py Parādīt failu

@@ -782,8 +782,8 @@ def get_site_base_path(sites_dir=None, hostname=None):
def get_site_path(*path): def get_site_path(*path):
return get_path(base=get_site_base_path(), *path) return get_path(base=get_site_base_path(), *path)


def get_files_path():
return get_site_path("public", "files")
def get_files_path(*path):
return get_site_path("public", "files", *path)


def get_backups_path(): def get_backups_path():
return get_site_path("private", "backups") return get_site_path("private", "backups")


+ 13
- 14
frappe/utils/file_manager.py Parādīt failu

@@ -6,7 +6,7 @@ import frappe
import os, base64, re import os, base64, re
import hashlib import hashlib
import mimetypes import mimetypes
from frappe.utils import cstr, cint, get_site_path, get_hook_method
from frappe.utils import cstr, cint, get_site_path, get_hook_method, get_files_path
from frappe import _ from frappe import _
from frappe import conf from frappe import conf
from copy import copy from copy import copy
@@ -117,24 +117,23 @@ def save_file(fname, content, dt, dn, decode=False):
"file_hash": content_hash "file_hash": content_hash
}) })


f = webnotes.bean(file_data)
f = frappe.bean(file_data)
f.ignore_permissions = True f.ignore_permissions = True
try: try:
f.insert(); f.insert();
except webnotes.DuplicateEntryError:
return webnotes.doc("File Data", f.doc.duplicate_entry)
except frappe.DuplicateEntryError:
return frappe.doc("File Data", f.doc.duplicate_entry)


return f.doc return f.doc
def save_file_on_filesystem(fname, content, content_type=None): def save_file_on_filesystem(fname, content, content_type=None):
import filecmp import filecmp
from frappe.modules import load_doctype_module
public_path = os.path.join(frappe.local.site_path, "public", "files")
temp_fname = write_file(content, public_path, fname)
public_path = os.path.join(frappe.local.site_path, "public")
fpath = write_file(content, get_files_path(), fname)
path = os.path.relpath(fpath, public_path) path = os.path.relpath(fpath, public_path)
return { return {
'file_name': path, 'file_name': path,
'file_path': path
'file_url': '/' + path
} }
def check_max_file_size(content): def check_max_file_size(content):
@@ -150,11 +149,11 @@ def check_max_file_size(content):
def write_file(content, file_path, fname): def write_file(content, file_path, fname):
"""write file to disk with a random name (to compare)""" """write file to disk with a random name (to compare)"""
# create directory (if not exists) # create directory (if not exists)
frappe.create_folder(files_path)
frappe.create_folder(get_files_path())
# write the file # write the file
with open(fname, 'w+') as f:
with open(os.path.join(file_path, fname), 'w+') as f:
f.write(content) f.write(content)
return fname
return get_files_path(fname)


def remove_all(dt, dn): def remove_all(dt, dn):
"""remove all files in a transaction""" """remove all files in a transaction"""
@@ -176,9 +175,9 @@ def delete_file_data_content(doc):
def delete_file_from_filesystem(doc): def delete_file_from_filesystem(doc):
path = doc.file_name path = doc.file_name
if path.startswith("files/"): if path.startswith("files/"):
path = frappe.utils.get_site_path("public", self.doc.file_name)
path = frappe.utils.get_site_path("public", doc.file_name)
else: else:
path = frappe.utils.get_site_path("public", "files", self.doc.file_name)
path = frappe.utils.get_site_path("public", "files", doc.file_name)
if os.path.exists(path): if os.path.exists(path):
os.remove(path) os.remove(path)
@@ -203,7 +202,7 @@ def get_content_hash(content):
return hashlib.md5(content).hexdigest() return hashlib.md5(content).hexdigest()


def get_file_name(fname, optional_suffix): def get_file_name(fname, optional_suffix):
n_records = webnotes.conn.sql("select name from `tabFile Data` where file_name='{}'".format(fname))
n_records = frappe.db.sql("select name from `tabFile Data` where file_name='{}'".format(fname))
if len(n_records) > 0: if len(n_records) > 0:
partial, extn = fname.rsplit('.', 1) partial, extn = fname.rsplit('.', 1)
return '{partial}{suffix}.{extn}'.format(partial=partial, extn=extn, suffix=optional_suffix) return '{partial}{suffix}.{extn}'.format(partial=partial, extn=extn, suffix=optional_suffix)


Notiek ielāde…
Atcelt
Saglabāt