<commit_before>"""
Provides request handlers for file downloads.

"""
import gzip
import json

from aiohttp import web

import virtool.bio
import virtool.db.downloads
import virtool.db.history
import virtool.db.otus
import virtool.db.references
import virtool.errors
import virtool.http.routes
import virtool.otus
from virtool.api.utils import CustomEncoder, not_found

routes = virtool.http.routes.Routes()
<commit_msg>Add sample file download handler<commit_after>"""
Provides request handlers for file downloads.

"""
import os
import gzip
import json

from aiohttp import web

import virtool.bio
import virtool.db.downloads
import virtool.db.history
import virtool.db.otus
import virtool.db.references
import virtool.errors
import virtool.http.routes
import virtool.otus
import virtool.utils
from virtool.api.utils import CustomEncoder, bad_request, not_found

routes = virtool.http.routes.Routes()


@routes.get("/download/samples/{sample_id}/{prefix}_{suffix}.fq")
async def download_uncompressed_sample_reads(req):
    db = req.app["db"]
    data_path = req.app["settings"]["data_path"]

    sample_id = req.match_info["sample_id"]

    document = await db.samples.find_one(sample_id, ["paired", "files"])

    if document is None or not document.get("files", False):
        return not_found()

    suffix = req.match_info["suffix"]

    path = os.path.join(data_path, "samples", sample_id, f"reads_{suffix}.fastq")

    if not os.path.isfile(path):
        return not_found()

    file_stats = virtool.utils.file_stats(path)

    headers = {
        "Content-Length": file_stats["size"],
        "Content-Type": "text"
    }

    return web.FileResponse(path, chunk_size=1024*1024, headers=headers)


@routes.get("/download/samples/{sample_id}/{prefix}_{suffix}.fq.gz")
async def download_sample_reads(req):
    db = req.app["db"]
    data_path = req.app["settings"]["data_path"]

    sample_id = req.match_info["sample_id"]

    document = await db.samples.find_one(sample_id, ["paired", "files"])

    if document is None or not document.get("files", False):
        return not_found()

    suffix = req.match_info["suffix"]

    path = os.path.join(data_path, "samples", sample_id, f"reads_{suffix}.fq.gz")

    if not os.path.isfile(path):
        return not_found()

    file_stats = virtool.utils.file_stats(path)

    headers = {
        "Content-Length": file_stats["size"],
        "Content-Type": "application/gzip"
    }

    return web.FileResponse(path, chunk_size=1024*1024, headers=headers)

{'fraction_non_alphanumeric': 0.12524850894632206, 'fraction_numerical': 0.006361829025844931, 'mean_word_length': 4.198347107438017, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5686249', 'n_tokens_mistral': 843, 'n_tokens_neox': 806, 'n_words': 181}