From 01abf65b1f26408d72b9c0f8e9eed7367f63cb8f Mon Sep 17 00:00:00 2001 From: Pratik Vyas Date: Tue, 24 Sep 2013 13:39:19 +0530 Subject: [PATCH] Differentiate between base_path and storage_base_path --- conf/conf.py | 1 + core/doctype/file_data/file_data.py | 5 +++-- webnotes/utils/__init__.py | 14 +++++++++++--- webnotes/utils/file_manager.py | 11 ++++++----- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/conf/conf.py b/conf/conf.py index 0de8a0297a..b732ab3a5b 100644 --- a/conf/conf.py +++ b/conf/conf.py @@ -10,6 +10,7 @@ db_password = '%(db_password)s' # user attachments stored in files_path = 'public/files' +public_path = 'public' # max file attachment size (default 1MB) max_file_size = 1000000 diff --git a/core/doctype/file_data/file_data.py b/core/doctype/file_data/file_data.py index e2645ac0e7..a1354435fe 100644 --- a/core/doctype/file_data/file_data.py +++ b/core/doctype/file_data/file_data.py @@ -9,6 +9,7 @@ naming for same name files: file.gif, file-1.gif, file-2.gif etc """ import webnotes, webnotes.utils, os +from webnotes import conf class DocType(): def __init__(self, d, dl): @@ -28,7 +29,7 @@ class DocType(): def on_trash(self): if self.doc.file_name and webnotes.conn.sql("""select count(*) from `tabFile Data` where file_name=%s""", self.doc.file_name)[0][0]==1: - path = webnotes.utils.get_path("public", "files", self.doc.file_name) + path = webnotes.utils.get_storage_path(conf.files_path, self.doc.file_name) if os.path.exists(path): os.remove(path) @@ -38,4 +39,4 @@ class DocType(): "write", self.doc.attached_to_name): webnotes.msgprint(webnotes._("No permission to write / remove."), raise_exception=True) - \ No newline at end of file + diff --git a/webnotes/utils/__init__.py b/webnotes/utils/__init__.py index bb21548b1d..5daf3d82d4 100644 --- a/webnotes/utils/__init__.py +++ b/webnotes/utils/__init__.py @@ -810,18 +810,26 @@ def filter_strip_join(some_list, sep): """given a list, filter None values, strip spaces and join""" return (cstr(sep)).join((cstr(a).strip() for a in filter(None, some_list))) -def get_path(*path): +def get_path(base=None, *path): + if not base: + base = get_base_path() import os - return os.path.join(get_base_path(), *path) + return os.path.join(base, *path) def get_base_path(): import conf import os return os.path.dirname(os.path.abspath(conf.__file__)) -def get_storage_base_path(sites_dir, hostname): +def get_storage_base_path(sites_dir=None, hostname=None): import os + if not sites_dir and not hostname: + sites_dir = conf.sites_dir + hostname = conf.site return os.path.join(sites_dir, hostname) + +def get_storage_path(*path): + return get_path(base=get_storage_base_path(), *path) def get_url(uri=None): url = get_request_site_address() diff --git a/webnotes/utils/file_manager.py b/webnotes/utils/file_manager.py index 4de0230cae..3079abea38 100644 --- a/webnotes/utils/file_manager.py +++ b/webnotes/utils/file_manager.py @@ -3,9 +3,10 @@ from __future__ import unicode_literals import webnotes -import os, conf -from webnotes.utils import cstr, get_path, cint +import os +from webnotes.utils import cstr, cint, get_storage_path from webnotes import _ +from webnotes import conf class MaxFileSizeReachedError(webnotes.ValidationError): pass @@ -64,7 +65,7 @@ def get_uploaded_content(): def save_file(fname, content, dt, dn): import filecmp from webnotes.model.code import load_doctype_module - files_path = get_path("public", "files") + files_path = get_storage_path(conf.public_path) module = load_doctype_module(dt, webnotes.conn.get_value("DocType", dt, "module")) if hasattr(module, "attachments_folder"): @@ -102,7 +103,7 @@ def save_file(fname, content, dt, dn): f = webnotes.bean({ "doctype": "File Data", - "file_name": os.path.relpath(os.path.join(files_path, fname), get_path("public")), + "file_name": os.path.relpath(os.path.join(files_path, fname), get_storage_path(conf.public_path)), "attached_to_doctype": dt, "attached_to_name": dn, "file_size": file_size @@ -183,7 +184,7 @@ def get_file(fname): # read the file import os - files_path = get_path("public", "files") + files_path = get_storage_path(conf.files_path) file_path = os.path.join(files_path, file_name) if not os.path.exists(file_path): # check in folders