Skip to content

Commit 5a29842

Browse files
committed
fix: 增加 206 统计,修复获取文件从主控获取
1 parent 4a4fb24 commit 5a29842

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.10
1+
3.0.11

core/cluster.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ async def _check_hash(self, file: File, storage: storages.iStorage):
157157

158158
async def get_file(self, hash: str):
159159
file = None
160-
if not await self.available():
160+
if await self.available():
161161
storage = self.get_width_storage()
162162
if isinstance(storage, storages.LocalStorage) and await storage.exists(hash):
163163
return LocalStorageFile(
@@ -1102,7 +1102,7 @@ async def _(request: aweb.Request):
11021102
resp = aweb.Response(status=500)
11031103

11041104
start = request.http_range.start or 0
1105-
end = request.http_range.stop or file.size
1105+
end = request.http_range.stop or file.size - 1
11061106
size = end - start + 1
11071107

11081108
cluster.hit(file.storage, size)
@@ -1115,39 +1115,53 @@ async def _(request: aweb.Request):
11151115
if name:
11161116
headers["Content-Disposition"] = f"attachment; filename={name}"
11171117
headers["X-BMCLAPI-Hash"] = hash
1118+
11181119
if isinstance(file, LocalStorageFile):
11191120
resp = aweb.FileResponse(
11201121
file.path,
1121-
headers=headers,
1122+
headers=headers
11221123
)
11231124
elif isinstance(file, MemoryStorageFile):
11241125
resp = aweb.Response(
1125-
body=file.data
1126+
body=file.data[start:end + 1],
1127+
headers={
1128+
"Content-Range": f"bytes={start}-{end}/{file.size}",
1129+
"Content-Length": str(size),
1130+
"Content-Type": "application/octet-stream",
1131+
**headers
1132+
}
11261133
)
11271134
elif isinstance(file, URLStorageFile):
11281135
resp = aweb.HTTPFound(
1129-
file.url
1136+
file.url,
1137+
headers=headers
11301138
)
1131-
type = db.StatusType.ERROR
1139+
type = None
11321140
if resp.status == 200:
11331141
type = db.StatusType.SUCCESS
1142+
elif resp.status == 206:
1143+
type = db.StatusType.PARTIAL
11341144
elif resp.status == 403:
11351145
type = db.StatusType.FORBIDDEN
11361146
elif resp.status == 404:
11371147
type = db.StatusType.NOT_FOUND
11381148
elif resp.status == 302:
11391149
type = db.StatusType.REDIRECT
1150+
if (type == db.StatusType.SUCCESS or type is None) and request.http_range.stop is not None:
1151+
type = db.StatusType.PARTIAL
11401152
storage_name = file.storage.unique_id if file.storage is not None else None
11411153
db.add_file(cluster.id, storage_name, size)
11421154
db.add_response(
11431155
address,
1144-
type,
1156+
type or db.StatusType.ERROR,
11451157
user_agent
11461158
)
11471159
return resp
11481160
except:
1161+
logger.traceback()
11491162
db.add_response(
11501163
address,
1151-
db.StatusType.ERROR
1164+
db.StatusType.ERROR,
1165+
user_agent
11521166
)
11531167
return aweb.Response(status=500)

core/database.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from sqlalchemy import create_engine, Column, Integer, String, LargeBinary
88
from sqlalchemy.ext.declarative import declarative_base
99
from sqlalchemy.orm import sessionmaker
10+
from sqlalchemy.orm.decl_api import DeclarativeMeta
1011

1112
from core import logger, scheduler, utils
1213

@@ -42,13 +43,14 @@ class ResponseStatistics:
4243
forbidden: int = 0
4344
error: int = 0
4445
redirect: int = 0
46+
partial: int = 0
4547
ip_tables: defaultdict[str, int] = field(default_factory=lambda: defaultdict(int))
4648
user_agents: defaultdict[str, int] = field(default_factory=lambda: defaultdict(int))
4749

4850

4951

5052
engine = create_engine('sqlite:///database.db')
51-
Base = declarative_base()
53+
Base: DeclarativeMeta = declarative_base()
5254

5355
class ClusterStatisticsTable(Base):
5456
__tablename__ = 'ClusterStatistics'
@@ -71,6 +73,7 @@ class ResponseTable(Base):
7173
id = Column(Integer, primary_key=True)
7274
hour = Column(Integer, nullable=False)
7375
success = Column(String, nullable=False)
76+
partial = Column(String, nullable=False)
7477
forbidden = Column(String, nullable=False)
7578
not_found = Column(String, nullable=False)
7679
error = Column(String, nullable=False)
@@ -80,6 +83,7 @@ class ResponseTable(Base):
8083

8184
class StatusType(Enum):
8285
SUCCESS = "success"
86+
PARTIAL = "partial"
8387
FORBIDDEN = "forbidden"
8488
NOT_FOUND = "not_found"
8589
ERROR = "error"
@@ -181,12 +185,12 @@ def _commit_cluster(hour: int, cluster: str, hits: int, bytes: int):
181185
)
182186
return True
183187

184-
def _commit_response(hour: int, ip_tables: defaultdict[str, int], user_agents: defaultdict[str, int], success: int = 0, forbidden: int = 0, redirect: int = 0, not_found: int = 0, error: int = 0):
188+
def _commit_response(hour: int, ip_tables: defaultdict[str, int], user_agents: defaultdict[str, int], success: int = 0, forbidden: int = 0, redirect: int = 0, not_found: int = 0, error: int = 0, partial: int = 0):
185189
if ip_tables == {}:
186190
return False
187191
session = SESSION.get_session()
188192
q = session.query(ResponseTable).filter_by(hour=hour)
189-
r = q.first() or ResponseTable(hour=hour, ip_tables=b'', user_agents=b'', success=str(0), forbidden=str(0), redirect=str(0), not_found=str(0), error=str(0))
193+
r = q.first() or ResponseTable(hour=hour, ip_tables=b'', user_agents=b'', success=str(0), forbidden=str(0), redirect=str(0), not_found=str(0), error=str(0), partial=str(0))
190194
if q.count() == 0:
191195
session.add(r)
192196
origin_ip_tables: defaultdict[str, int] = defaultdict(lambda: 0)
@@ -235,7 +239,8 @@ def _commit_response(hour: int, ip_tables: defaultdict[str, int], user_agents: d
235239
'forbidden': str(int(r.forbidden) + forbidden), # type: ignore
236240
'redirect': str(int(r.redirect) + redirect), # type: ignore
237241
'not_found': str(int(r.not_found) + not_found), # type: ignore
238-
'error': str(int(r.error) + error) # type: ignore
242+
'error': str(int(r.error) + error), # type: ignore
243+
'partial': str(int(r.partial) + partial) # type: ignore
239244
}
240245
)
241246
return True
@@ -265,7 +270,7 @@ def commit():
265270
_commit_cluster(cluster[0], cluster[1], value.hits, value.bytes)
266271

267272
for hour, value in response_cache.items():
268-
_commit_response(hour, value.ip_tables, value.user_agents, value.success, value.forbidden, value.redirect, value.not_found, value.error)
273+
_commit_response(hour, value.ip_tables, value.user_agents, value.success, value.forbidden, value.redirect, value.not_found, value.error, value.partial)
269274

270275
session.commit()
271276
old_keys = []

0 commit comments

Comments
 (0)