def _get_ufile(file_indicator = None): ufile = UFile.query.filter(UFile.url == file_indicator).first() if not ufile: ufile = None if ufile is not None: # test whether file exists if not funcs.f_exists(ufile.filename): db_session.delete(ufile) db_session.commit() abort(404) else: if not funcs.f_exists(file_indicator): abort(404) ufile = UFile( name = funcs.get_name_from_filename(file_indicator), url = file_indicator, filename = file_indicator, filesize = funcs.get_file_size(file_indicator), mimetype = funcs.get_file_mimetype(file_indicator), created = datetime.utcnow() ) db_session.add(ufile) db_session.commit() #if not r: # logging.error('Failed to commit new record: %r' % ufile) # abort(404) return ufile
def static_file(file_id=None, filename = None): #if not funcs.f_exists(filename): abort(404) ufile = UFile.query.filter(UFile.id == file_id).first() if not ufile: abort(404) if not funcs.f_exists(ufile.filename): abort(404) is_download = request.args.get('download') if is_download == 'yes': if not ufile.download: abort(403) as_attachment = True try: attachment_filename = filename.encode('UTF-8') except: attachment_filename = None else: as_attachment = False attachment_filename = None # reference if not as_attachment and request.referrer.startswith(config.URL_ROOT): self_ref = True else: self_ref = False key = request.args.get('key') v_key = request.cookies.get(sf_cookie_name(ufile.filename)) if self_ref or (key and v_key and v_key == sf_cookie_val(ufile.filename, key)): pass else: abort(403) return send_from_directory(os.path.normpath(os.path.join(config.ROOT_PATH, config.UPLOAD_FILE_PATH)), ufile.filename, as_attachment=as_attachment, attachment_filename=attachment_filename)
def direct_file(file_id, filename): ufile = UFile.query.filter(UFile.id == file_id).first() if not ufile: abort(404) if not ufile.linkable: abort(403) if isinstance(ufile.expire_at, datetime) and datetime.utcnow()>ufile.expire_at: abort(403) if not funcs.f_exists(ufile.filename): abort(404) return send_from_directory(os.path.normpath(os.path.join(config.ROOT_PATH, config.UPLOAD_FILE_PATH)), filename)