Changeset - 3c1d991755df
[Not reviewed]
beta
0 2 0
Marcin Kuzminski - 14 years ago 2012-05-17 02:44:10
marcin@python-works.com
Use paste fileapp to properly send the archive size
2 files changed with 20 insertions and 18 deletions:
0 comments (0 inline, 0 general)
rhodecode/controllers/files.py
Show inline comments
 
@@ -29,12 +29,13 @@ import traceback
 
import tempfile
 

	
 
from pylons import request, response, tmpl_context as c, url
 
from pylons.i18n.translation import _
 
from pylons.controllers.util import redirect
 
from pylons.decorators import jsonify
 
from paste.fileapp import FileApp, _FileIter
 

	
 
from rhodecode.lib import diffs
 
from rhodecode.lib import helpers as h
 

	
 
from rhodecode.lib.compat import OrderedDict
 
from rhodecode.lib.utils2 import convert_line_endings, detect_mode, safe_str
 
@@ -361,26 +362,28 @@ class FilesController(BaseRepoController
 
            return _('Unknown archive type')
 

	
 
        fd, _archive_name = tempfile.mkstemp(suffix='rcarchive')
 
        with open(_archive_name, 'wb') as f:
 
            cs.fill_archive(stream=f, kind=fileformat, subrepos=subrepos)
 

	
 
        response.content_type = content_type
 
        response.content_disposition = 'attachment; filename=%s-%s%s' \
 
        content_disposition = 'attachment; filename=%s-%s%s' \
 
            % (repo_name, revision[:12], ext)
 
        response.content_length = str(os.path.getsize(_archive_name))
 
        content_length = os.path.getsize(_archive_name)
 

	
 
        headers = [('Content-Disposition', str(content_disposition)),
 
                   ('Content-Type', str(content_type)),
 
                   ('Content-Length', str(content_length))]
 

	
 
        def get_chunked_archive(tmpfile):
 
            while True:
 
                data = tmpfile.read(16 * 1024)
 
                if not data:
 
                    tmpfile.close()
 
                    os.unlink(tmpfile.name)
 
                    break
 
                yield data
 
        return get_chunked_archive(tmpfile=open(_archive_name, 'rb'))
 
        class _DestroyingFileWrapper(_FileIter):
 
            def close(self):
 
                self.file.close
 
                os.remove(self.file.name)
 

	
 
        request.environ['wsgi.file_wrapper'] = _DestroyingFileWrapper
 
        fapp = FileApp(_archive_name, headers=headers)
 
        return fapp(request.environ, self.start_response)
 

	
 
    @HasRepoPermissionAnyDecorator('repository.read', 'repository.write',
 
                                   'repository.admin')
 
    def diff(self, repo_name, f_path):
 
        ignore_whitespace = request.GET.get('ignorews') == '1'
 
        line_context = request.GET.get('context', 3)
rhodecode/tests/functional/test_files.py
Show inline comments
 
@@ -196,19 +196,18 @@ class TestFilesController(TestController
 
            response = self.app.get(url(controller='files',
 
                                        action='archivefile',
 
                                        repo_name=HG_REPO,
 
                                        fname=fname))
 

	
 
            self.assertEqual(response.status, '200 OK')
 
            self.assertEqual(response.response._headers.items(),
 
             [('Pragma', 'no-cache'),
 
              ('Cache-Control', 'no-cache'),
 
              ('Content-Type', '%s; charset=utf-8' % info[0]),
 
              ('Content-Disposition', 'attachment; filename=%s' % filename),
 
            heads = [
 
            ('Content-Type', 'text/html; charset=utf-8'),
 
            ('Content-Length', '0'), ('Pragma', 'no-cache'),
 
            ('Cache-Control', 'no-cache')
 
             ]
 
            )
 
            self.assertEqual(response.response._headers.items(), heads)
 

	
 
    def test_archival_wrong_ext(self):
 
        self.log_user()
 

	
 
        for arch_ext in ['tar', 'rar', 'x', '..ax', '.zipz']:
 
            fname = '27cd5cce30c96924232dffcd24178a07ffeb5dfc%s' % arch_ext
0 comments (0 inline, 0 general)