import os
from cerberus import Validator

import virtool.file
import virtool.utils
from virtool.handlers.utils import json_response, not_found, invalid_query

FILE_TYPES = {
    "/upload/viruses": "viruses",
    "/upload/reads": "reads",
    "/upload/hmm/profiles": "profiles",
    "/upload/hmm/annotations": "annotations",
    "/upload/subtraction": "subtraction"
}


async def upload(req):
    try:
        file_type = FILE_TYPES[req.path]
    except KeyError:
        return not_found()

    v = Validator({
        "name": {"type": "string", "required": True}
    })

    if not v(dict(req.query)):
        return invalid_query(v.errors)

    reader = await req.multipart()
    file = await reader.next()

    db = req.app["db"]

    filename = req.query["name"]

    file_id = "{}-{}".format(await virtool.utils.get_new_id(db.files), filename)

    while file_id in await db.files.distinct("_id"):
        file_id = "{}-{}".format(await virtool.utils.get_new_id(db.files), filename)

    file_path = os.path.join(req.app["settings"].get("data_path"), "files", file_id)

    document = {
        "_id": file_id,
        "name": filename,
        "type": file_type,
        "user": {
            "id": req["session"].user_id
        },
        "uploaded_at": virtool.utils.timestamp(),
        "created": False,
        "ready": False
    }

    await db.files.insert_one(document)

    document = {key: document[key] for key in virtool.file.LIST_PROJECTION if document.get(key, False)}

    document = virtool.file.processor(document)

    await req.app["dispatcher"].dispatch(
        "files",
        "update",
        document
    )

    size = 0

    with open(file_path, "wb") as handle:
        while True:
            chunk = await file.read_chunk()
            if not chunk:
                break
            size += len(chunk)
            handle.write(chunk)

    return json_response(document)

@@ -3,16 +3,29 @@
 port os%0A
+import arrow%0A
 from cer
@@ -1081,16 +1081,185 @@
 le_id)%0A%0A
+    uploaded_at = virtool.utils.timestamp()%0A%0A    expires_at = None%0A%0A    if file_type == %22viruses%22:%0A        expires_at = arrow.get(uploaded_at).shift(hours=+5).datetime%0A%0A
     docu
@@ -1263,24 +1263,24 @@
 ocument = %7B%0A
-
         %22_id
@@ -1441,33 +1441,53 @@
 t%22: 
-virtool.utils.timestamp()
+uploaded_at,%0A        %22expires_at%22: expires_at
 ,%0A  

Add expires_at file document field