Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use pathvalidate to validate filename and filepath #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions canvas_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Callable
import os
from pathlib import Path
from pathvalidate import sanitize_filename,sanitize_filepath
from utils import get_application_setting, ConfigKey

app_setting = get_application_setting()
Expand Down Expand Up @@ -52,11 +53,12 @@ def download_canvas(print_output: Callable[[str], None]) -> None:
for file in folder.get_files():
if not sync_on or str(file) not in current_files: # Download checking condition
try:
print_output(f"位于{folder_top if folder_top else 'Canvas Home'},正在下载文件:{file}")
file_name=sanitize_filename(str(file),platform="auto")
print_output(f"位于{folder_top if folder_top else 'Canvas Home'},正在下载文件:{file_name}")

# Create folder to replicate canvas structure
Path(download_to_folder, folder_top).mkdir(parents=True, exist_ok=True)
file.download(Path(app_setting.value(ConfigKey.FOLDER_PATH_ABS), folder_top, str(file)).as_posix())
sanitize_filepath(Path(download_to_folder, folder_top),platform="auto").mkdir(parents=True, exist_ok=True)
file.download(sanitize_filepath(Path(download_to_folder, folder_top, file_name),platform="auto").as_posix())
new_files_idx += 1 # only increment after download because it might have error during download
except exceptions.ResourceDoesNotExist:
print_output(f"下载失败:资源不存在")
Expand Down