<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
views.py

@author <NAME>
@author <NAME>

"""

from django.http import HttpResponse, HttpResponseRedirect
from django.conf import settings

from tardis.tardis_portal.models import *
from tardis.tardis_portal.views import render_response_index, \
    return_response_not_found, return_response_error, \
    has_datafile_access, experiment_access_required
from tardis.tardis_portal.logger import logger

from os.path import join

import urllib


def download_datafile(request, datafile_id):

    # todo handle missing file, general error
    datafile = Dataset_File.objects.get(pk=datafile_id)

    if has_datafile_access(datafile.id, request.user):
        url = datafile.url

        if url.startswith('http://') or url.startswith('https://') \
            or url.startswith('ftp://'):
            return HttpResponseRedirect(datafile.url)
        else:
            file_path = join(settings.FILE_STORE_PATH,
                             str(datafile.dataset.experiment.id),
                             datafile.url.partition('//')[2])
            try:
                logger.debug(file_path)
                from django.core.servers.basehttp import FileWrapper
                wrapper = FileWrapper(file(file_path))

                response = HttpResponse(wrapper,
                        mimetype='application/octet-stream')
                response['Content-Disposition'] = \
                    'attachment; filename=' + datafile.filename

                # import os
                # response['Content-Length'] = os.path.getsize(file_path)

                return response
<commit_msg>download functions now support filenames with spaces<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
views.py

@author <NAME>
@author <NAME>

"""

from django.http import HttpResponse, HttpResponseRedirect
from django.conf import settings

from tardis.tardis_portal.models import *
from tardis.tardis_portal.views import render_response_index, \
    return_response_not_found, return_response_error, \
    has_datafile_access, experiment_access_required
from tardis.tardis_portal.logger import logger

from os.path import join

import urllib


def download_datafile(request, datafile_id):

    # todo handle missing file, general error
    datafile = Dataset_File.objects.get(pk=datafile_id)

    if has_datafile_access(datafile.id, request.user):
        url = datafile.url

        if url.startswith('http://') or url.startswith('https://') \
            or url.startswith('ftp://'):
            return HttpResponseRedirect(datafile.url)
        else:
            file_path = join(settings.FILE_STORE_PATH,
                             str(datafile.dataset.experiment.id),
                             datafile.url.partition('//')[2])
            try:
                logger.debug(file_path)
                from django.core.servers.basehttp import FileWrapper
                wrapper = FileWrapper(file(file_path))

                response = HttpResponse(wrapper,
                        mimetype='application/octet-stream')
                response['Content-Disposition'] = \
                    'attachment; filename="' + datafile.filename + '"'

                # import os
                # response['Content-Length'] = os.path.getsize(file_path)

                return response

{'fraction_non_alphanumeric': 0.09928443649373882, 'fraction_numerical': 0.0011926058437686344, 'mean_word_length': 2.311944718657453, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5980212', 'n_tokens_mistral': 949, 'n_tokens_neox': 917, 'n_words': 182}