def get_filename(self, filename, filesystem=False, convert=False): """ Get the filename according to self.to_path, and if filesystem is False then return unicode filename, otherwise return filesystem encoded filename @param filename: relative filename, it'll be combine with self.to_path @param filesystem: if True, then encoding the filename to filesystem @param convert: if True, then convert filename with FilenameConverter class """ from uliweb.utils.common import safe_unicode #make sure the filename is unicode s = settings.GLOBAL if convert: _p, _f = os.path.split(filename) _filename = os.path.join(_p, self.filename_convert(_f)) else: _filename = filename nfile = safe_unicode(_filename, s.HTMLPAGE_ENCODING) f = os.path.normpath(os.path.join(self.to_path, nfile)).replace('\\', '/') if filesystem: return files.encode_filename(f, to_encoding=s.FILESYSTEM_ENCODING) return f
def get_filename(self, filename, filesystem=False, convert=False, subpath=''): """ Get the filename according to self.to_path, and if filesystem is False then return unicode filename, otherwise return filesystem encoded filename @param filename: relative filename, it'll be combine with self.to_path @param filesystem: if True, then encoding the filename to filesystem @param convert: if True, then convert filename with FilenameConverter class @param subpath: sub folder in to_path """ from uliweb.utils.common import safe_unicode #make sure the filename is unicode s = settings.GLOBAL if convert: _p, _f = os.path.split(filename) _filename = os.path.join(_p, self.filename_convert(_f)) else: _filename = filename nfile = safe_unicode(_filename, s.HTMLPAGE_ENCODING) if subpath: paths = [application_path(self.to_path), subpath, nfile] else: paths = [application_path(self.to_path), nfile] f = os.path.normpath(os.path.join(*paths)).replace('\\', '/') if filesystem: return files.encode_filename(f, to_encoding=s.FILESYSTEM_ENCODING) return f
def download(self, filename, action='download', x_filename='', real_filename=''): """ action will be "download", "inline" and if the request.GET has 'action', then the action will be replaced by it. """ from uliweb.utils.common import safe_str from uliweb.utils.filedown import filedown s = settings.GLOBAL action = request.GET.get('action', action) if not real_filename: real_filename = self.get_filename(filename, True, convert=False) else: real_filename = files.encode_filename(real_filename, to_encoding=s.FILESYSTEM_ENCODING) if not x_filename: x_filename = safe_str(filename, s.FILESYSTEM_ENCODING) if self.x_file_prefix: x_filename = os.path.normpath(os.path.join(self.x_file_prefix, x_filename)).replace('\\', '/') return filedown(request.environ, filename, action=action, x_sendfile=bool(self.x_sendfile), x_header_name=self.x_header_name, x_filename=x_filename, real_filename=real_filename)
def download(self, filename, action='download', x_filename='', x_sendfile=None, real_filename=''): """ action will be "download", "inline" and if the request.GET has 'action', then the action will be replaced by it. """ from uliweb import request from uliweb.utils.common import safe_str from uliweb.utils.filedown import filedown s = settings.GLOBAL action = request.GET.get('action', action) if not real_filename: real_filename = self.get_filename(filename, True, convert=False) else: real_filename = files.encode_filename(real_filename, to_encoding=s.FILESYSTEM_ENCODING) if not x_filename: x_filename = safe_str(filename, s.FILESYSTEM_ENCODING) if self.x_file_prefix: x_filename = os.path.normpath(os.path.join(self.x_file_prefix, x_filename)).replace('\\', '/') xsend_flag = bool(self.x_sendfile) if x_sendfile is None else x_sendfile return filedown(request.environ, filename, action=action, x_sendfile=xsend_flag, x_header_name=self.x_header_name, x_filename=x_filename, real_filename=real_filename)
def download(self, filename, action=None, x_filename='', real_filename=''): """ action will be "download", "inline" and if the request.GET has 'action', then the action will be replaced by it. """ from uliweb.utils.common import safe_str from uliweb.utils.filedown import filedown from uliweb import request from StringIO import StringIO from uliweb.utils import files action = request.GET.get('action', action) fname = safe_str(filename) if not x_filename: x_filename = fname if self.x_file_prefix: x_filename = os.path.normpath(os.path.join(self.x_file_prefix, x_filename)).replace('\\', '/') if not real_filename: #if not real_filename, then get the file info from database obj = self.get_file_record(filename) fname = obj.filename.encode('utf8') fileobj = StringIO(obj.content), obj.create_time, obj.size #fileobj should be (filename, mtime, size) else: fileobj = None s = settings.GLOBAL real_filename = files.encode_filename(real_filename, to_encoding=s.FILESYSTEM_ENCODING) return filedown(request.environ, fname, action=action, x_sendfile=bool(self.x_sendfile), x_header_name=self.x_header_name, x_filename=x_filename, real_filename=real_filename, fileobj=fileobj)
def get_filename(filename, filesystem=False): from uliweb import application from uliweb.utils import files from uliweb.utils.common import safe_unicode #make sure the filename is unicode s = application.settings.GLOBAL filename = safe_unicode(filename, s.HTMLPAGE_ENCODING) path = os.path.normpath(application.settings.UPLOAD.TO_PATH).replace('\\', '/') f = os.path.normpath(os.path.join(path, filename)).replace('\\', '/') if not f.startswith(path): raise Forbidden("You can not visit unsafe files.") if filesystem: return files.encode_filename(f, s.HTMLPAGE_ENCODING, s.FILESYSTEM_ENCODING) return f
def save_file(filename, fobj, replace=False): from uliweb import application from uliweb.utils import files assert hasattr(fobj, 'read'), "fobj parameter should be a file-like object" #get full path filename fname = get_filename(filename) s = application.settings.GLOBAL #change full path filename to filesystem filename fname1 = files.encode_filename(fname, s.HTMLPAGE_ENCODING, s.FILESYSTEM_ENCODING) #save file and get the changed filename, because the filename maybe change when #there is duplicate filename(replace=False, if replace=True, then the filename #will not changed fname2 = files.save_file(fname1, fobj, replace, application.settings.UPLOAD.BUFFER_SIZE) #create new filename according fname2 and filename, the result should be unicode return normfilename(os.path.join(os.path.dirname(filename), files.unicode_filename(fname2, s.FILESYSTEM_ENCODING)))
def download(self, filename, action=None, x_filename='', real_filename=''): """ action will be "download", "inline" and if the request.GET has 'action', then the action will be replaced by it. """ from uliweb.utils.common import safe_str from uliweb.utils.filedown import filedown from uliweb import request from StringIO import StringIO from uliweb.utils import files action = request.GET.get('action', action) fname = safe_str(filename) if not x_filename: x_filename = fname if self.x_file_prefix: x_filename = os.path.normpath( os.path.join(self.x_file_prefix, x_filename)).replace('\\', '/') if not real_filename: #if not real_filename, then get the file info from database obj = self.get_file_record(filename) fname = obj.filename.encode('utf8') fileobj = StringIO(obj.content), obj.create_time, obj.size #fileobj should be (filename, mtime, size) else: fileobj = None s = settings.GLOBAL real_filename = files.encode_filename( real_filename, to_encoding=s.FILESYSTEM_ENCODING) return filedown(request.environ, fname, action=action, x_sendfile=bool(self.x_sendfile), x_header_name=self.x_header_name, x_filename=x_filename, real_filename=real_filename, fileobj=fileobj)