<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):
    content_disposition = 'attachment'  # to trigger download

    def get(self, name):
        try:
            item = current_app.storage.open(name)
        except OSError as e:
            if e.errno == errno.ENOENT:
                raise NotFound()
            # XXX item is undefined here, but we proceed... XXX

        if not item.meta.get('unlocked'):
            error = 'File Locked.'
        elif not item.meta.get('complete'):
            error = 'Upload incomplete. Try again later.'
        else:
            error = None
        if error:
            try:
                return render_template('display_error.html', name=name, item=item, error=error), 409
            finally:
                item.close()

        def stream():
            with item 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'] = '{}; filename="{}"'.format(
            self.content_disposition, item.meta['filename'])
        ret.headers['Content-Length'] = item.meta['size']
        ret.headers['Content-Type'] = item.meta['type']  # 'application/octet-stream'
        return ret


class InlineView(DownloadView):
    content_disposition = 'inline'  # to trigger viewing in browser, for some types


blueprint.add_url_rule('/<itemname:name>/+download', view_func=DownloadView.as_view('download'))
blueprint.add_url_rule('/<itemname:name>/+inline', view_func=InlineView.as_view('inline'))
<commit_msg>implement 'timestamp-download'<commit_after># Copyright: 2013 <NAME> <<EMAIL>>
# License: BSD 2-clause, see LICENSE for details.

import errno
import time

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):
    content_disposition = 'attachment'  # to trigger download

    def get(self, name):
        try:
            item = current_app.storage.openwrite(name)
        except OSError as e:
            if e.errno == errno.ENOENT:
                raise NotFound()
            # XXX item is undefined here, but we proceed... XXX

        if not item.meta.get('unlocked'):
            error = 'File Locked.'
        elif not item.meta.get('complete'):
            error = 'Upload incomplete. Try again later.'
        else:
            error = None
        if error:
            try:
                return render_template('display_error.html', name=name, item=item, error=error), 409
            finally:
                item.close()

        def stream():
            with item 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
                item.meta['timestamp-download'] = int(time.time())

        ret = Response(stream_with_context(stream()))
        ret.headers['Content-Disposition'] = '{}; filename="{}"'.format(
            self.content_disposition, item.meta['filename'])
        ret.headers['Content-Length'] = item.meta['size']
        ret.headers['Content-Type'] = item.meta['type']  # 'application/octet-stream'
        return ret


class InlineView(DownloadView):
    content_disposition = 'inline'  # to trigger viewing in browser, for some types


blueprint.add_url_rule('/<itemname:name>/+download', view_func=DownloadView.as_view('download'))
blueprint.add_url_rule('/<itemname:name>/+inline', view_func=InlineView.as_view('inline'))

{'fraction_non_alphanumeric': 0.1095538425601495, 'fraction_numerical': 0.00700770847932726, 'mean_word_length': 2.2762050497322113, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2194819', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1133, 'n_words': 305}