From 3bebaab659ac22b9e2d1c42cdb9b74536416df0c Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Tue, 25 Sep 2012 18:20:33 +0530 Subject: [PATCH] dont save duplicate files --- conf/apache.conf | 14 ++---- webnotes/__init__.py | 13 ++---- webnotes/auth.py | 2 +- webnotes/model/code.py | 4 +- webnotes/utils/__init__.py | 7 --- webnotes/utils/file_manager.py | 83 ++++++++++++++++------------------ 6 files changed, 51 insertions(+), 72 deletions(-) diff --git a/conf/apache.conf b/conf/apache.conf index 26d9cdfe3e..be284cd55e 100644 --- a/conf/apache.conf +++ b/conf/apache.conf @@ -20,16 +20,12 @@ Listen 8080 # rewrite rule RewriteEngine on - # condition 1: - # ignore login-page.html, app.html, blank.html, unsupported.html - RewriteCond %{REQUEST_URI} ^((?!app\.html|blank\.html|unsupported\.html).)*$ + # don't filter static files + RewriteRule ^(.*)/(lib/|app/|js/|css/|files/|backup/)(.*)$ $1/$2$3 [L] + RewriteRule ^(.*)/(app.html|unsupported.html|rss.xml|sitemap.xml|web.py|server.py)(.*)$ $1/$2$3 [L] - # condition 2: if there are no slashes - # and file is .html or does not containt a . - RewriteCond %{REQUEST_URI} ^(?!.+/)((.+\.html)|([^.]+))$ - - # rewrite if both of the above conditions are true - RewriteRule ^(.+)$ web.py?page=$1 [NC,L] + # everything else is a web page + RewriteRule ^(.*)/([^/]*)$ $1/web.py?page=$2 [L] AllowOverride all Order Allow,Deny diff --git a/webnotes/__init__.py b/webnotes/__init__.py index dd55f061a6..5d90311701 100644 --- a/webnotes/__init__.py +++ b/webnotes/__init__.py @@ -119,14 +119,6 @@ def msgprint(msg, small=0, raise_exception=0, as_table=False): raise raise_exception, msg else: raise ValidationError, msg - -def get_index_path(): - import os - return os.sep.join(os.path.dirname(os.path.abspath(__file__)).split(os.sep)[:-2]) - -def get_files_path(): - import conf - return conf.files_path def create_folder(path): """ @@ -254,3 +246,8 @@ def get_roles(user=None, with_standard=True): roles = filter(lambda x: x not in ['All', 'Guest', 'Administrator'], roles) return roles + +def generate_hash(): + """Generates random hash for session id""" + import hashlib, time + return hashlib.sha224(str(time.time())).hexdigest() \ No newline at end of file diff --git a/webnotes/auth.py b/webnotes/auth.py index 8fffc95acc..4812626d51 100644 --- a/webnotes/auth.py +++ b/webnotes/auth.py @@ -319,7 +319,7 @@ class Session: if webnotes.login_manager.user=='Guest': sid = 'Guest' else: - sid = webnotes.utils.generate_hash() + sid = webnotes.generate_hash() self.data['user'] = webnotes.login_manager.user self.data['sid'] = sid diff --git a/webnotes/model/code.py b/webnotes/model/code.py index 4526c8d44f..7ef324fd96 100644 --- a/webnotes/model/code.py +++ b/webnotes/model/code.py @@ -38,7 +38,7 @@ custom_class = ''' # Please edit this list and import only required elements import webnotes -from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, generate_hash, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add +from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add from webnotes.model import db_exists from webnotes.model.doc import Document, addchild, getchildren, make_autoname from webnotes.model.utils import getlist @@ -69,7 +69,7 @@ def execute(code, doc=None, doclist=[]): """ # functions used in server script of DocTypes # -------------------------------------------------- - from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, generate_hash, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add + from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add from webnotes.model import db_exists from webnotes.model.doc import Document, addchild, getchildren from webnotes.model.utils import getlist diff --git a/webnotes/utils/__init__.py b/webnotes/utils/__init__.py index a3cd9d3201..696f8cdd93 100644 --- a/webnotes/utils/__init__.py +++ b/webnotes/utils/__init__.py @@ -106,13 +106,6 @@ def get_request_site_address(): except TypeError, e: return 'http://localhost' -def generate_hash(): - """ - Generates random hash for session id - """ - import hashlib, time - return hashlib.sha224(str(time.time())).hexdigest() - def random_string(length): """generate a random string""" import string diff --git a/webnotes/utils/file_manager.py b/webnotes/utils/file_manager.py index bda78b3402..cf45fafdbe 100644 --- a/webnotes/utils/file_manager.py +++ b/webnotes/utils/file_manager.py @@ -22,6 +22,7 @@ from __future__ import unicode_literals import webnotes +import os, conf def upload(): # get record details @@ -112,9 +113,9 @@ def remove_file(dt, dn, fid): def make_thumbnail(blob, size): from PIL import Image - import cStringIO + from cStringIO import StringIO - fobj = cStringIO.StringIO(blob) + fobj = StringIO(blob) image = Image.open(fobj) image.thumbnail((tn,tn*2), Image.ANTIALIAS) outfile = cStringIO.StringIO() @@ -124,9 +125,7 @@ def make_thumbnail(blob, size): return fcontent -def get_uploaded_content(): - import webnotes - +def get_uploaded_content(): # should not be unicode when reading a file, hence using webnotes.form if 'filedata' in webnotes.form: i = webnotes.form['filedata'] @@ -136,68 +135,62 @@ def get_uploaded_content(): webnotes.msgprint('No File'); return None, None -def save_uploaded(): - import webnotes.utils - +def save_uploaded(): webnotes.response['type'] = 'iframe' - fname, content = get_uploaded_content() if content: fid = save_file(fname, content) return fid, fname - else: return None, fname -# ------------------------------------------------------- - def save_file(fname, content, module=None): from webnotes.model.doc import Document + from filecmp import cmp + + check_max_file_size(content) + new_fname = write_file(content) # some browsers return the full path if '\\' in fname: fname = fname.split('\\')[-1] if '/' in fname: fname = fname.split('/')[-1] + + # we use - for versions, so remove them from the name! + fname = fname.replace('-', '') + + fpath = os.path.join(conf.files_path, fname) + if os.path.exists(fpath) and cmp(fpath, new_fname): + # remove file, already exists! + os.remove(new_fname) + return fname + else: + # generate the ID (?) + f = Document('File Data') + f.file_name = fname + f.save(1) + # rename new file + os.rename(new_fname, os.path.join(conf.files_path, f.name)) + return f.name - # generate the ID (?) - f = Document('File Data') - f.file_name = fname - if module: - f.module = module - f.save(1) - - write_file(f.name, content) - - return f.name - -# ------------------------------------------------------- - -def write_file(fid, content): - import os, conf - - # test size - max_file_size = 1000000 - if hasattr(conf, 'max_file_size'): - max_file_size = conf.max_file_size +def check_max_file_size(content): + max_file_size = getattr(conf, 'max_file_size', 1000000) if len(content) > max_file_size: raise Exception, 'Maximum File Limit (%s MB) Crossed' % (int(max_file_size / 1000000)) - # no slashes - fid = fid.replace('/','-') - - # save to a folder (not accessible to public) - folder = webnotes.get_files_path() - +def write_file(content): + """write file to disk with a random name""" # create account folder (if not exists) - webnotes.create_folder(folder) + webnotes.create_folder(conf.files_path) + fname = os.path.join(conf.files_path, webnotes.generate_hash()) # write the file - file = open(os.path.join(folder, fid),'w+') - file.write(content) - file.close() - + with open(fname, 'w+') as f: + f.write(content) + + return fname def get_file_system_name(fname): # get system name from File Data table @@ -208,7 +201,7 @@ def delete_file(fid, verbose=0): """delete file from file system""" import os webnotes.conn.sql("delete from `tabFile Data` where name=%s", fid) - path = os.path.join(webnotes.get_files_path(), fid.replace('/','-')) + path = os.path.join(conf.files_path, fid.replace('/','-')) if os.path.exists(path): os.remove(path) @@ -223,7 +216,7 @@ def get_file(fname): # read the file import os - with open(os.path.join(webnotes.get_files_path(), file_id), 'r') as f: + with open(os.path.join(conf.files_path, file_id), 'r') as f: content = f.read() return [file_name, content] \ No newline at end of file