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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Missing library stats dont cause a crash on first launch anymore [#264](https://github.com/pSpitzner/beets-flask/issues/264)
- Fixed a potential memory leak when checking if files are archives. We now only check the file extension instead of trying to open the file, which should avoid the issue with `tarfile.is_tarfile` [#258](https://github.com/pSpitzner/beets-flask/issues/258)

### Other (dev)

Expand Down
28 changes: 23 additions & 5 deletions backend/beets_flask/disk.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
from __future__ import annotations

import importlib.util
import os
import re
import subprocess
from abc import ABC, abstractmethod
from collections.abc import Iterator, Sequence
from dataclasses import dataclass
from fnmatch import fnmatch
from functools import cache
from pathlib import Path
from typing import (
Literal,
)

from beets.importer import (
ArchiveImportTask,
)
from beets.importer.tasks import (
MULTIDISC_MARKERS,
MULTIDISC_PAT_FMT,
Expand Down Expand Up @@ -222,9 +221,28 @@ def from_path(
)


@cache
def allowed_archive_extensions() -> list[str]:
# Tar files
ext = [".tar", ".tar.gz", ".tgz", ".tar.bz2", ".tbz2", ".tar.xz", ".txz"]
# Zip files
ext += [".zip"]
if importlib.util.find_spec("rarfile") is not None:
ext += [".rar"]
if importlib.util.find_spec("py7zr") is not None:
ext += [".7z"]
return ext


def is_archive_file(path: Path | str) -> bool:
"""Check if a file is an archive file based on its extension."""
return ArchiveImportTask.is_archive(str(path))
"""Check if a file is an archive file based on its extension.

It seems like there is a memory issue with `tarfile.is_tarfile`
(see https://github.com/pSpitzner/beets-flask/issues/258). We try
to avoid this by only checking the file extension here, and not trying to open the file.
"""
allowed_extensions = allowed_archive_extensions()
return Path(path).suffix.lower() in allowed_extensions


@dataclass
Expand Down
4 changes: 2 additions & 2 deletions docs/develop/contribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ Run [Ruff](https://docs.astral.sh/ruff/) manually or use the pre-commit hooks to
```bash
cd backend
# Code formatting and linting
ruff check
ruff lint
ruff check . --fix
ruff format .
# Typing checks
mypy .
# Run the tests
Expand Down