@@ -10,6 +10,7 @@ db_password = '%(db_password)s' | |||||
# user attachments stored in | # user attachments stored in | ||||
files_path = 'public/files' | files_path = 'public/files' | ||||
public_path = 'public' | |||||
# max file attachment size (default 1MB) | # max file attachment size (default 1MB) | ||||
max_file_size = 1000000 | max_file_size = 1000000 | ||||
@@ -9,6 +9,7 @@ naming for same name files: file.gif, file-1.gif, file-2.gif etc | |||||
""" | """ | ||||
import webnotes, webnotes.utils, os | import webnotes, webnotes.utils, os | ||||
from webnotes import conf | |||||
class DocType(): | class DocType(): | ||||
def __init__(self, d, dl): | def __init__(self, d, dl): | ||||
@@ -28,7 +29,7 @@ class DocType(): | |||||
def on_trash(self): | def on_trash(self): | ||||
if self.doc.file_name and webnotes.conn.sql("""select count(*) from `tabFile Data` | 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: | 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): | if os.path.exists(path): | ||||
os.remove(path) | os.remove(path) | ||||
@@ -38,4 +39,4 @@ class DocType(): | |||||
"write", self.doc.attached_to_name): | "write", self.doc.attached_to_name): | ||||
webnotes.msgprint(webnotes._("No permission to write / remove."), | webnotes.msgprint(webnotes._("No permission to write / remove."), | ||||
raise_exception=True) | raise_exception=True) | ||||
@@ -810,18 +810,26 @@ def filter_strip_join(some_list, sep): | |||||
"""given a list, filter None values, strip spaces and join""" | """given a list, filter None values, strip spaces and join""" | ||||
return (cstr(sep)).join((cstr(a).strip() for a in filter(None, some_list))) | 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 | import os | ||||
return os.path.join(get_base_path(), *path) | |||||
return os.path.join(base, *path) | |||||
def get_base_path(): | def get_base_path(): | ||||
import conf | import conf | ||||
import os | import os | ||||
return os.path.dirname(os.path.abspath(conf.__file__)) | 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 | import os | ||||
if not sites_dir and not hostname: | |||||
sites_dir = conf.sites_dir | |||||
hostname = conf.site | |||||
return os.path.join(sites_dir, hostname) | 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): | def get_url(uri=None): | ||||
url = get_request_site_address() | url = get_request_site_address() | ||||
@@ -3,9 +3,10 @@ | |||||
from __future__ import unicode_literals | from __future__ import unicode_literals | ||||
import webnotes | 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 _ | ||||
from webnotes import conf | |||||
class MaxFileSizeReachedError(webnotes.ValidationError): pass | class MaxFileSizeReachedError(webnotes.ValidationError): pass | ||||
@@ -64,7 +65,7 @@ def get_uploaded_content(): | |||||
def save_file(fname, content, dt, dn): | def save_file(fname, content, dt, dn): | ||||
import filecmp | import filecmp | ||||
from webnotes.model.code import load_doctype_module | 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")) | module = load_doctype_module(dt, webnotes.conn.get_value("DocType", dt, "module")) | ||||
if hasattr(module, "attachments_folder"): | if hasattr(module, "attachments_folder"): | ||||
@@ -102,7 +103,7 @@ def save_file(fname, content, dt, dn): | |||||
f = webnotes.bean({ | f = webnotes.bean({ | ||||
"doctype": "File Data", | "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_doctype": dt, | ||||
"attached_to_name": dn, | "attached_to_name": dn, | ||||
"file_size": file_size | "file_size": file_size | ||||
@@ -183,7 +184,7 @@ def get_file(fname): | |||||
# read the file | # read the file | ||||
import os | 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) | file_path = os.path.join(files_path, file_name) | ||||
if not os.path.exists(file_path): | if not os.path.exists(file_path): | ||||
# check in folders | # check in folders | ||||