<commit_before># Copyright: 2013 <NAME> <<EMAIL>>
# License: BSD 2-clause, see LICENSE for details.

import errno

from flask import Response, current_app, render_template, stream_with_context
from flask.views import MethodView
from werkzeug.exceptions import NotFound

from ..utils.name import ItemName
from . import blueprint


class DownloadView(MethodView):
    def get(self, name):
        try:
            baseitem = current_app.storage.open(name)
        except OSError as e:
            if e.errno == errno.ENOENT:
                raise NotFound()

        if not baseitem.meta['complete']:
            error = 'Upload incomplete. Try again later.'
            return render_template('display_error.html', name=name, item=baseitem, error=error), 409

        def stream():
            with baseitem as item:
                # Stream content from storage
                offset = 0
                size = item.data.size
                while offset < size:
                    buf = item.data.read(16*1024, offset)
                    offset += len(buf)
                    yield buf

        ret = Response(stream_with_context(stream()))
        ret.headers['Content-Disposition'] = 'attachment; filename="{}"'.format(baseitem.meta['filename'])
        ret.headers['Content-Length'] = baseitem.meta['size']<commit_msg>Manually close item in error path<commit_after># Copyright: 2013 <NAME> <<EMAIL>>
# License: BSD 2-clause, see LICENSE for details.

import errno

from flask import Response, current_app, render_template, stream_with_context
from flask.views import MethodView
from werkzeug.exceptions import NotFound

from ..utils.name import ItemName
from . import blueprint


class DownloadView(MethodView):
    def get(self, name):
        try:
            baseitem = current_app.storage.open(name)
        except OSError as e:
            if e.errno == errno.ENOENT:
                raise NotFound()

        try:
            if not baseitem.meta['complete'] or 1:
                error = 'Upload incomplete. Try again later.'
                return render_template('display_error.html', name=name, item=baseitem, error=error), 409
        finally:
            baseitem.close()

        def stream():
            with baseitem as item:
                # Stream content from storage
                offset = 0
                size = item.data.size
                while offset < size:
                    buf = item.data.read(16*1024, offset)
                    offset += len(buf)
                    yield buf

        ret = Response(stream_with_context(stream()))
        ret.headers['Content-Disposition'] = 'attachment; filename="{}"'.format(baseitem.meta['filename'])
        ret.headers['Content-Length'] = baseitem.meta['size']
{'fraction_non_alphanumeric': 0.09861717612809316, 'fraction_numerical': 0.011280931586608443, 'mean_word_length': 2.174364896073903, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1519451', 'n_tokens_mistral': 765, 'n_tokens_neox': 700, 'n_words': 207}