From be715d277b98231f6af07d5ee26e63b0a16ae532 Mon Sep 17 00:00:00 2001 From: Pratik Vyas Date: Fri, 7 Mar 2014 00:56:50 +0530 Subject: [PATCH] move download_backup to frappe.utils.response --- frappe/app.py | 5 +++-- frappe/utils/__init__.py | 29 ----------------------------- frappe/utils/response.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/frappe/app.py b/frappe/app.py index b369c14b53..d08bc4cd02 100644 --- a/frappe/app.py +++ b/frappe/app.py @@ -15,6 +15,7 @@ import frappe import frappe.handler import frappe.auth import frappe.api +import frappe.utils.response import frappe.website.render from frappe.utils import get_site_name @@ -58,8 +59,8 @@ def application(request): frappe.handler.handle() elif frappe.request.path.startswith("/api/"): frappe.api.handle() - elif request.path.startswith('/backups'): - frappe.utils.download_backup(request.path) + elif frappe.request.path.startswith('/backups'): + frappe.utils.response.download_backup(request.path) elif frappe.local.request.method in ('GET', 'HEAD'): frappe.website.render.render(frappe.request.path[1:]) else: diff --git a/frappe/utils/__init__.py b/frappe/utils/__init__.py index 802f6aecac..da5006d67f 100644 --- a/frappe/utils/__init__.py +++ b/frappe/utils/__init__.py @@ -7,11 +7,7 @@ from __future__ import unicode_literals from werkzeug.test import Client import os import re -import mimetypes import urllib -from werkzeug.wsgi import wrap_file -from werkzeug.wrappers import Response -from werkzeug.exceptions import NotFound, Unauthorized import frappe no_value_fields = ['Section Break', 'Column Break', 'HTML', 'Table', 'FlexTable', @@ -933,28 +929,3 @@ def touch_file(path): def get_test_client(): from frappe.app import application return Client(application) - -def download_backup(path): - try: - frappe.only_for(("System Manager", "Administrator")) - except frappe.PermissionError: - raise Unauthorized - send_private_file(path) - -def send_private_file(path): - path = path[1:] if path.startswith('/') else path - path = os.path.join(frappe.local.conf.get('private_path', 'private'), path) - - if frappe.local.request.headers.get('X-Use-X-Accel-Redirect'): - path = '/' + path - frappe.local._response.headers['X-Accel-Redirect'] = path - else: - filename = os.path.basename(path) - filepath = get_site_path(path) - try: - f = open(filepath, 'rb') - except IOError: - raise NotFound - frappe.local._response = Response(wrap_file(frappe.local.request.environ, f)) - frappe.local._response.headers.add('Content-Disposition', 'attachment', filename=filename) - frappe.local._response.headers['Content-Type'] = mimetypes.guess_type(filename)[0] or 'application/octet-stream' diff --git a/frappe/utils/response.py b/frappe/utils/response.py index 4d2dd81f8c..7ac620bf6f 100644 --- a/frappe/utils/response.py +++ b/frappe/utils/response.py @@ -5,11 +5,16 @@ from __future__ import unicode_literals import json import datetime import gzip, cStringIO +import mimetypes +import os import frappe import frappe.utils import frappe.sessions import frappe.model.utils from werkzeug.local import LocalProxy +from werkzeug.wsgi import wrap_file +from werkzeug.wrappers import Response +from werkzeug.exceptions import NotFound, Unauthorized def report_error(status_code): if status_code!=404 or frappe.conf.logging: @@ -113,3 +118,28 @@ def compressBuf(buf): zfile.write(buf) zfile.close() return zbuf.getvalue() + +def download_backup(path): + try: + frappe.only_for(("System Manager", "Administrator")) + except frappe.PermissionError: + raise Unauthorized + send_private_file(path) + +def send_private_file(path): + path = path[1:] if path.startswith('/') else path + path = os.path.join(frappe.local.conf.get('private_path', 'private'), path) + + if frappe.local.request.headers.get('X-Use-X-Accel-Redirect'): + path = '/' + path + frappe.local._response.headers['X-Accel-Redirect'] = path + else: + filename = os.path.basename(path) + filepath = frappe.utils.get_site_path(path) + try: + f = open(filepath, 'rb') + except IOError: + raise NotFound + frappe.local._response = Response(wrap_file(frappe.local.request.environ, f)) + frappe.local._response.headers.add('Content-Disposition', 'attachment', filename=filename) + frappe.local._response.headers['Content-Type'] = mimetypes.guess_type(filename)[0] or 'application/octet-stream'