2024-06-05 13:39:34 +00:00
|
|
|
# -------------------------------------
|
|
|
|
# @file : dirscan.py
|
|
|
|
# @author : Autumn
|
|
|
|
# @contact : rainy-autumn@outlook.com
|
|
|
|
# @time : 2024/5/9 20:22
|
|
|
|
# -------------------------------------------
|
|
|
|
|
|
|
|
from bson import ObjectId
|
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
from motor.motor_asyncio import AsyncIOMotorCursor
|
|
|
|
from api.users import verify_token
|
|
|
|
from core.db import get_mongo_db
|
2024-07-04 15:28:11 +00:00
|
|
|
from core.util import search_to_mongodb, get_search_query
|
2024-06-05 13:39:34 +00:00
|
|
|
from loguru import logger
|
2024-07-04 15:28:11 +00:00
|
|
|
|
2024-06-05 13:39:34 +00:00
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/dirscan/result/data")
|
|
|
|
async def dirscan_data(request_data: dict, db=Depends(get_mongo_db), _: dict = Depends(verify_token)):
|
|
|
|
try:
|
|
|
|
page_index = request_data.get("pageIndex", 1)
|
|
|
|
page_size = request_data.get("pageSize", 10)
|
2024-07-04 15:28:11 +00:00
|
|
|
query = await get_search_query("dir", request_data)
|
|
|
|
if query == "":
|
2024-06-05 13:39:34 +00:00
|
|
|
return {"message": "Search condition parsing error", "code": 500}
|
|
|
|
total_count = await db['DirScanResult'].count_documents(query)
|
2024-07-04 15:28:11 +00:00
|
|
|
sort = request_data.get("sort", {})
|
|
|
|
sort_by = [('_id', -1)]
|
|
|
|
if sort != {}:
|
|
|
|
if 'length' in sort:
|
|
|
|
sort_value = sort['length']
|
|
|
|
if sort_value is not None:
|
|
|
|
if sort_value == "ascending":
|
|
|
|
sort_value = 1
|
|
|
|
else:
|
|
|
|
sort_value = -1
|
|
|
|
sort_by = [('length', sort_value)]
|
|
|
|
cursor: AsyncIOMotorCursor = ((db['DirScanResult'].find(query, {"_id": 0, "id": {"$toString": "$_id"}, "url": 1,
|
|
|
|
"status": 1, "msg": 1, "length": 1})
|
|
|
|
.sort(sort_by)
|
2024-06-05 13:39:34 +00:00
|
|
|
.skip((page_index - 1) * page_size)
|
|
|
|
.limit(page_size)))
|
|
|
|
result = await cursor.to_list(length=None)
|
|
|
|
return {
|
|
|
|
"code": 200,
|
|
|
|
"data": {
|
|
|
|
'list': result,
|
|
|
|
'total': total_count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(str(e))
|
|
|
|
# Handle exceptions as needed
|
2024-07-04 15:28:11 +00:00
|
|
|
return {"message": "error", "code": 500}
|