Skip to content

Commit

Permalink
feat: 支持 webdav 提供直链访问
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxiu2b2t committed Dec 13, 2024
1 parent 4a56922 commit 09aa3eb
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 134 deletions.
19 changes: 11 additions & 8 deletions core/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
import pyzstd as zstd
import aiohttp
from tqdm import tqdm

from . import web
from . import utils, logger, config, scheduler, units, storages, i18n
from .storages import File as SFile
import socketio
import urllib.parse as urlparse
from . import database as db
from .config import USER_AGENT, API_VERSION
from .utils import WrapperTQDM

from cryptography import x509
from cryptography.hazmat.backends import default_backend
Expand Down Expand Up @@ -123,19 +125,19 @@ async def get_missing_files(self, files: set[File]) -> set[File | Any]:
function = self._check_exists

await self.available()
with tqdm(
with WrapperTQDM(tqdm(
total=len(self.available_storages) * 256,
desc="List Files",
unit="dir",
unit_scale=True
) as pbar:
)) as pbar:
await asyncio.gather(*(self.get_storage_filelist(storage, pbar) for storage in self.available_storages))
with tqdm(
with WrapperTQDM(tqdm(
total=len(files) * len(self.available_storages),
desc="Checking files",
unit="file",
unit_scale=True,
) as pbar:
)) as pbar:
missing_files = set()
waiting_files: asyncio.Queue[File] = asyncio.Queue()

Expand All @@ -145,14 +147,14 @@ async def get_missing_files(self, files: set[File]) -> set[File | Any]:
await asyncio.gather(*(self._get_missing_file_storage(function, missing_files, waiting_files, storage, pbar) for storage in self.available_storages))
return missing_files

async def get_storage_filelist(self, storage: storages.iStorage, pbar: tqdm):
async def get_storage_filelist(self, storage: storages.iStorage, pbar: WrapperTQDM):
result = await storage.list_files(pbar)
for files in result.values():
for file in files:
self.cache_filelist[storage][file.hash] = file
return result

async def _get_missing_file_storage(self, function: Callable[..., Coroutine[Any, Any, bool]], missing_files: set[File], files: asyncio.Queue[File], storage: storages.iStorage, pbar: tqdm):
async def _get_missing_file_storage(self, function: Callable[..., Coroutine[Any, Any, bool]], missing_files: set[File], files: asyncio.Queue[File], storage: storages.iStorage, pbar: WrapperTQDM):
while not files.empty():
file = await files.get()
if await function(file, storage):
Expand Down Expand Up @@ -290,13 +292,13 @@ def __init__(self, total: int = 0, size: int = 0):
self.pbar = None

def __enter__(self):
self.pbar = tqdm(
self.pbar = WrapperTQDM(tqdm(
total=self.total_size,
unit="b",
unit_scale=True,
unit_divisor=1024,
desc=i18n.locale.t("cluster.processbar.download_files")
)
))
self.downloaded_files = 0
self.failed_files = 0
return self
Expand Down Expand Up @@ -1151,6 +1153,7 @@ async def _(request: aweb.Request):
elif isinstance(storage, storages.WebDavStorage):
file = await storage.get_file(MEASURES_HASH[size])
if file.url:
logger.debug("Requested measure url:", file.url)
return aweb.HTTPFound(file.url)
elif file.size >= 0:
return aweb.Response(
Expand Down
2 changes: 1 addition & 1 deletion core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def rank_clusters_url(self):

const = Const()

VERSION = "3.3.7"
VERSION = "3.3.8"
API_VERSION = "1.13.1"
USER_AGENT = f"openbmclapi/{API_VERSION} python-openbmclapi/{VERSION}"
PYTHON_VERSION = ".".join(map(str, (sys.version_info.major, sys.version_info.minor, sys.version_info.micro)))
32 changes: 0 additions & 32 deletions core/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,37 +232,6 @@ async def _(request: web.Request):
except:
return web.json_response([])

@route.get("/api/system_info")
async def _(request: web.Request):
return web.json_response(counter.get_json())

@route.get("/api/count")
async def _(request: web.Request):
# statistics of the cluster hits and bytes
session = db.SESSION.get_session()
current_hour = db.get_hour()
hour_of_day = (current_hour // 24) * 24
next_hour = hour_of_day + 24
q = session.query(db.ClusterStatisticsTable).filter(
db.ClusterStatisticsTable.hour >= hour_of_day,
db.ClusterStatisticsTable.hour < next_hour
).all()
return web.json_response({
"hits": sum([int(item.hits) for item in q]), # type: ignore
"bytes": sum([int(item.bytes) for item in q]) # type: ignore
})


@route.get("/api/openbmclapi/rank")
async def _(request: web.Request):
async with aiohttp.ClientSession(
"bd.bangbang93.com"
) as session:
async with session.get("/openbmclapi/metric/rank") as resp:
return web.json_response(
await resp.json(),
)

route.static("/assets", "./assets")

class JSONEncoder(json.JSONEncoder):
Expand All @@ -282,7 +251,6 @@ async def websocket_process_api(resp: web.WebSocketResponse, data: Any):
return
await resp.send_json(res, dumps=json_dumps)


async def process_api(
event: Optional[str],
req_data: Any,
Expand Down
Loading

0 comments on commit 09aa3eb

Please sign in to comment.