import os

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


async def find(req):
    db = req.app["db"]

    base_query = {
        "ready": True,
        "reserved": False
    }

    file_type = req.query.get("type", None)

    db_query = dict()

    if file_type:
        db_query["type"] = file_type

    data = await paginate(db.files, db_query, req.query, sort_by="uploaded_at", projection=virtool.file.PROJECTION)

    return json_response(data)


async def remove(req):
    file_id = req.match_info["file_id"]

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

    delete_result = await req.app["db"].files.delete_one({"_id": file_id})

    virtool.utils.rm(file_path)

    if delete_result.deleted_count == 0:
        return not_found("Document does not exist")

    await req.app["dispatcher"].dispatch("files", "remove", [file_id])

    return json_response({
        "file_id": file_id,
        "removed": True
    })

@@ -327,26 +327,28 @@
 pe:%0A        
-d
 b
+ase
 _query%5B%22type
@@ -389,16 +389,25 @@
 aginate(
+%0A        
 db.files
@@ -407,16 +407,24 @@
 b.files,
+%0A       
  db_quer
@@ -425,16 +425,24 @@
 b_query,
+%0A       
  req.que
@@ -444,16 +444,24 @@
 q.query,
+%0A       
  sort_by
@@ -475,16 +475,24 @@
 ded_at%22,
+%0A       
  project
@@ -518,16 +518,52 @@
 OJECTION
+,%0A        base_query=base_query%0A    
 )%0A%0A    r

Fix API handling of type query on GET /api/files