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

Bugfix windows zip files #830

Merged
merged 3 commits into from
Mar 22, 2024
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
4 changes: 4 additions & 0 deletions docat/docat/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
:license: MIT, see LICENSE for more details.
"""

import logging
import os
import secrets
import shutil
Expand Down Expand Up @@ -38,6 +39,8 @@
DOCAT_DB_PATH = DOCAT_STORAGE_PATH / DB_PATH
DOCAT_UPLOAD_FOLDER = DOCAT_STORAGE_PATH / UPLOAD_FOLDER

logger = logging.getLogger(__name__)


@asynccontextmanager
async def lifespan(_: FastAPI):
Expand Down Expand Up @@ -248,6 +251,7 @@ def upload(
try:
extract_archive(target_file, base_path)
except Exception:
logger.exception("Failed to unzip {target_file=}")
response.status_code = status.HTTP_400_BAD_REQUEST
return ApiResponse(message="Cannot extract zip file.")

Expand Down
21 changes: 20 additions & 1 deletion docat/docat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import shutil
from pathlib import Path
from zipfile import ZipFile
from zipfile import ZipFile, ZipInfo

from docat.models import Project, ProjectDetail, Projects, ProjectVersion

Expand All @@ -15,6 +15,25 @@
DB_PATH = "db.json"


def is_dir(self):
"""Return True if this archive member is a directory."""
if self.filename.endswith("/"):
return True
# The ZIP format specification requires to use forward slashes
# as the directory separator, but in practice some ZIP files
# created on Windows can use backward slashes. For compatibility
# with the extraction code which already handles this:
if os.path.altsep:
return self.filename.endswith((os.path.sep, os.path.altsep))
return False


# Patch is_dir to allow windows zip files to be
# extracted correctly
# see: https://github.com/python/cpython/issues/117084
ZipInfo.is_dir = is_dir # type: ignore[method-assign]


def create_symlink(source, destination):
"""
Create a symlink from source to destination, if the
Expand Down
132 changes: 66 additions & 66 deletions docat/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions docat/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ license = "MIT"
[tool.poetry.dependencies]
python = "^3.10"
tinydb = "^4.8.0"
fastapi = {version = "^0.106.0", extras = ["all"]}
python-multipart = "^0.0.6"
uvicorn = "^0.25.0"
fastapi = {version = "^0.110.0", extras = ["all"]}
python-multipart = "^0.0.9"
uvicorn = "^0.29.0"
python-magic = "^0.4.27"

[tool.poetry.dev-dependencies]
flake8 = "^6.0.0"
flake8 = "^7.0.0"
flake8-isort = "^6.1.1"
flake8-black = "^0.3.6"
pytest = "^7.4.3"
pytest = "^8.1.1"
pytest-cov = "^4.1.0"
requests = "^2.31.0"
mypy = "^1.8.0"
mypy = "^1.9.0"

[tool.pytest.ini_options]
minversion = "6.0"
Expand Down