Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 11 additions & 37 deletions log2s3/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ def uri2file(file_path: str) -> Path:
target = (working_dir / file_path).resolve()
if working_dir.resolve().absolute() not in target.resolve().absolute().parents:
if not (target.exists() and target.samefile(working_dir)):
_log.warning(
"out of path: wdir=%s, target=%s", working_dir, target.resolve()
)
_log.warning("out of path: wdir=%s, target=%s", working_dir, target.resolve())
raise HTTPException(status_code=403, detail=f"cannot access to {file_path}")
return target

Expand Down Expand Up @@ -84,11 +82,7 @@ def read_file(response: Response, file_path: str, accept_encoding: str = Header(
if target.exists():
raise HTTPException(status_code=403, detail=f"cannot access to {file_path}")
# compressed case
target_compressed = [
x
for x in target.parent.iterdir()
if x.is_file() and x.name.startswith(target.name + ".")
]
target_compressed = [x for x in target.parent.iterdir() if x.is_file() and x.name.startswith(target.name + ".")]
for p in target_compressed:
_, stream = auto_compress_stream(p, "decompress")
_log.info("auto decompress %s: %s", acc, p)
Expand Down Expand Up @@ -136,11 +130,7 @@ def list_dir(file_path: str, file_prefix: str = "") -> dict[str, dict[str, str]]
elif target.is_dir():
for root, _, filenames in target.walk():
root = Path(root)
files = [
root / x
for x in filenames
if Path(x).suffix in (exts | {".log", ".txt"})
]
files = [root / x for x in filenames if Path(x).suffix in (exts | {".log", ".txt"})]
files = [x for x in files if x.name.startswith(file_prefix)]
for x in files:
reg_file(res, x)
Expand All @@ -165,9 +155,7 @@ def gen(ldir: dict[str, dict[str, str]]):
for title, files in ldir.items():
buf = io.StringIO()
uri = uriescape(f"html1/{title}")
buf.write(
'<div style="border: 1px solid black; float: left; margin: 10px; padding: 1em;">'
)
buf.write('<div style="border: 1px solid black; float: left; margin: 10px; padding: 1em;">')
buf.write(f'<h2><a href="{uri}">{title}</a></h2><ul>')
premonth = None
for dtstr in sorted(files.keys()):
Expand All @@ -183,9 +171,7 @@ def gen(ldir: dict[str, dict[str, str]]):
linkhtml = f'<a href="{uri}">{dt.strftime("%d")}</a>'
color = api_config.get("weekday_colors", {}).get(dt.weekday())
if color is not None:
buf.write(
f' <span style="background-color: {color};">{linkhtml}</span>'
)
buf.write(f' <span style="background-color: {color};">{linkhtml}</span>')
else:
buf.write(f" {linkhtml}")
buf.write("</li></ul>")
Expand All @@ -202,9 +188,7 @@ def gen(ldir: dict[str, dict[str, str]]):
def html2_gen1(uri: str, month: str, files: dict[str, str]) -> str:
dt = datetime.datetime.strptime(month, "%Y-%m").date()
buf = io.StringIO()
buf.write(
f'<tr><th colspan="7"><a href="{uri}?month={month}">{month}</a></th></tr>'
)
buf.write(f'<tr><th colspan="7"><a href="{uri}?month={month}">{month}</a></th></tr>')
wday = (dt.weekday() + 1) % 7
buf.write('<tr align="right">')
if wday != 0:
Expand Down Expand Up @@ -255,9 +239,7 @@ def html2_gen(ldir: dict[str, dict[str, str]], file_path: str):
wdstr = wd.strftime("%a")
color = api_config.get("weekday_colors", {}).get(wd.weekday())
if color:
buf.write(
f'<th style="background-color: {color};"><code>{wdstr}</code></th>'
)
buf.write(f'<th style="background-color: {color};"><code>{wdstr}</code></th>')
else:
buf.write(f"<th><code>{wdstr}</code></th>")
buf.write("</tr>")
Expand Down Expand Up @@ -292,17 +274,13 @@ def find_target(p: Path, accepts: list[str]) -> Path:
if p.with_suffix(p.suffix + ".br").exists():
return p.with_suffix(p.suffix + ".br")
# compressed case
target_compressed = [
x for x in p.parent.iterdir() if x.is_file() and x.name.startswith(p.name + ".")
]
target_compressed = [x for x in p.parent.iterdir() if x.is_file() and x.name.startswith(p.name + ".")]
if len(target_compressed):
return target_compressed[0]
raise HTTPException(status_code=404, detail=f"not found: {p}")


def get_streams(
files: dict[str, dict[str, str]], accepts: list[str]
) -> tuple[list[Stream], dict]:
def get_streams(files: dict[str, dict[str, str]], accepts: list[str]) -> tuple[list[Stream], dict]:
outputs: dict[str, list[str]] = {}
for _, v in files.items():
for k, fn in v.items():
Expand Down Expand Up @@ -335,9 +313,7 @@ def cat_file(file_path: str, month=month_query):
raise HTTPException(status_code=404, detail=f"not found: {file_path}")
streams, hdrs = get_streams(ldir, [])
# daily sort
return StreamingResponse(
content=CatStream(streams).gen(), media_type=media_type, headers=hdrs
)
return StreamingResponse(content=CatStream(streams).gen(), media_type=media_type, headers=hdrs)


@router.get("/merge/{file_path:path}")
Expand All @@ -348,6 +324,4 @@ def merge_file(file_path: str, month=month_query):
raise HTTPException(status_code=404, detail=f"not found: {file_path}")
streams, hdrs = get_streams(ldir, []) # cannot do passthrough compression
# daily sort
return StreamingResponse(
content=MergeStream(streams).gen(), media_type=media_type, headers=hdrs
)
return StreamingResponse(content=MergeStream(streams).gen(), media_type=media_type, headers=hdrs)
4 changes: 1 addition & 3 deletions log2s3/common_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ def read(self, sz: int = -1) -> bytes:
while True:
self.buf.append(next(self.gen1))
except StopIteration:
_log.debug(
"read %s / %s", len(self.buf), sum([len(x) for x in self.buf])
)
_log.debug("read %s / %s", len(self.buf), sum([len(x) for x in self.buf]))
buf = self.buf
self.buf = []
self.eof = True
Expand Down
16 changes: 4 additions & 12 deletions log2s3/compr_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class FileReadStream(Stream):
Read from file, stream interface
"""

def __init__(
self, file_like: io.RawIOBase | io.BufferedReader, bufsize=10 * 1024 * 1024
):
def __init__(self, file_like: io.RawIOBase | io.BufferedReader, bufsize=10 * 1024 * 1024):
self.fd = file_like
self.bufsize = bufsize

Expand Down Expand Up @@ -79,9 +77,7 @@ class S3GetStream(Stream):
Read data from S3 object with chunked read.
"""

def __init__(
self, s3_client: S3ClientType, bucket: str, key: str, bufsize=1024 * 1024
):
def __init__(self, s3_client: S3ClientType, bucket: str, key: str, bufsize=1024 * 1024):
self.obj = s3_client.get_object(Bucket=bucket, Key=key)
self.bufsize = bufsize

Expand Down Expand Up @@ -389,15 +385,11 @@ def __init__(self, prev_stream):
except ImportError:
pass

stream_ext: dict[str, tuple[str, type[Stream], type[Stream]]] = {
v[0]: (k, *v[1:]) for k, v in stream_map.items()
}
stream_ext: dict[str, tuple[str, type[Stream], type[Stream]]] = {v[0]: (k, *v[1:]) for k, v in stream_map.items()}
stream_compress_modes = list(stream_map.keys()) + ["decompress", "raw"]


def auto_compress_stream(
ifname: pathlib.Path, mode: str, ifp: Optional[Stream] = None
) -> tuple[os.PathLike, Stream]:
def auto_compress_stream(ifname: pathlib.Path, mode: str, ifp: Optional[Stream] = None) -> tuple[os.PathLike, Stream]:
if ifp is None:
ifp = FileReadStream(ifname.open("br"))
if mode == "raw":
Expand Down
Loading
Loading