Skip to content

Commit

Permalink
fix: better filter
Browse files Browse the repository at this point in the history
Closes #103
  • Loading branch information
Ljzd-PRO committed May 5, 2024
1 parent 7e45d2b commit 8aeafcc
Showing 1 changed file with 41 additions and 52 deletions.
93 changes: 41 additions & 52 deletions ktoolbox/action/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,68 +49,57 @@ async def create_job_from_post(
attachments_path = post_path
content_path = None

# ``Post.attachment`` filter
allow_list_filter = filter(
lambda x: any(
map(
lambda y: fnmatch(x.name or Path(x.path).name, y),
config.job.allow_list
)
),
post.attachments or []
)
attachment_name_filter = filter(
lambda x: not any(
map(
lambda y: fnmatch(x.name or Path(x.path).name, y),
config.job.block_list
)
),
allow_list_filter
)

# Create jobs for ``Post.attachment``
# Filter and create jobs for ``Post.attachment``
jobs: List[Job] = []
for i, attachment in enumerate(attachment_name_filter): # type: int, Attachment
for i, attachment in enumerate(post.attachments): # type: int, Attachment
if not attachment.path:
continue
if is_valid_filename(attachment.name):
alt_filename = f"{i + 1}{Path(attachment.name).suffix}" if config.job.sequential_filename \
else attachment.name
else:
attachment_path_without_params = urlparse(attachment.path).path
alt_filename = f"{i + 1}{Path(attachment_path_without_params).suffix}" if config.job.sequential_filename \
else None
jobs.append(
Job(
path=attachments_path,
alt_filename=alt_filename,
server_path=attachment.path,
type=PostFileTypeEnum.Attachment
)
file_path_obj = Path(attachment.name) if is_valid_filename(attachment.name) else Path(
urlparse(attachment.path).path
)
if (not config.job.allow_list or any(
map(
lambda x: fnmatch(file_path_obj.name, x),
config.job.allow_list
)
)) and not any(
map(
lambda x: fnmatch(file_path_obj.name, x),
config.job.block_list
)
):
alt_filename = f"{i + 1}{file_path_obj.suffix}" if config.job.sequential_filename else file_path_obj.name
jobs.append(
Job(
path=attachments_path,
alt_filename=alt_filename,
server_path=attachment.path,
type=PostFileTypeEnum.Attachment
)
)

# Filter and create jobs for ``Post.file``
post_file_name = post.file.name or Path(post.file.path).name
if post.file and post.file.path and any(
if post.file and post.file.path:
post_file_name = post.file.name or Path(post.file.path).name
if (not config.job.allow_list or any(
map(
lambda x: fnmatch(post_file_name, x),
config.job.allow_list
)
)) and not any(
map(
lambda x: fnmatch(post_file_name, x),
config.job.allow_list
config.job.block_list
)
) and not any(
map(
lambda x: fnmatch(post_file_name, x),
config.job.block_list
)
):
jobs.append(
Job(
path=post_path,
alt_filename=f"{post.id}_{post_file_name}",
server_path=post.file.path,
type=PostFileTypeEnum.File
):
jobs.append(
Job(
path=post_path,
alt_filename=f"{post.id}_{post_file_name}",
server_path=post.file.path,
type=PostFileTypeEnum.File
)
)
)

# Write content file
if content_path and post.content:
Expand Down

0 comments on commit 8aeafcc

Please sign in to comment.