Корректное сохранение файлов с длинными названиями
This commit is contained in:
parent
4f0df8626c
commit
8de9c2195a
2 changed files with 15 additions and 3 deletions
|
|
@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "yandex-music-downloader"
|
name = "yandex-music-downloader"
|
||||||
version = "3.4.7"
|
version = "3.4.8"
|
||||||
description = "Загрузчик музыки с сервиса Яндекс.Музыка"
|
description = "Загрузчик музыки с сервиса Яндекс.Музыка"
|
||||||
requires-python = ">=3.9"
|
requires-python = ">=3.9"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
|
|
||||||
16
ymd/core.py
16
ymd/core.py
|
|
@ -1,4 +1,5 @@
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
|
import hashlib
|
||||||
import re
|
import re
|
||||||
import typing
|
import typing
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
|
|
@ -52,6 +53,9 @@ MAX_COMPATIBILITY_LEVEL = 1
|
||||||
|
|
||||||
AUDIO_FILE_SUFFIXES = {".mp3", ".flac", ".m4a"}
|
AUDIO_FILE_SUFFIXES = {".mp3", ".flac", ".m4a"}
|
||||||
TEMPORARY_FILE_NAME_TEMPLATE = ".yandex-music-downloader.{}.tmp"
|
TEMPORARY_FILE_NAME_TEMPLATE = ".yandex-music-downloader.{}.tmp"
|
||||||
|
MAX_FILE_NAME_LENGTH_WITHOUT_SUFFIX = 255 - max(
|
||||||
|
len(suffix) for suffix in AUDIO_FILE_SUFFIXES
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class CoreTrackQuality(IntEnum):
|
class CoreTrackQuality(IntEnum):
|
||||||
|
|
@ -137,7 +141,14 @@ def prepare_base_path(
|
||||||
clear_re = UNSAFE_PATH_CLEAR_RE
|
clear_re = UNSAFE_PATH_CLEAR_RE
|
||||||
replacement = clear_re.sub("_", replacement)
|
replacement = clear_re.sub("_", replacement)
|
||||||
path_str = path_str.replace(placeholder, replacement)
|
path_str = path_str.replace(placeholder, replacement)
|
||||||
return Path(path_str)
|
path = Path(path_str)
|
||||||
|
trimmed_parts = [
|
||||||
|
part
|
||||||
|
if len(part) <= MAX_FILE_NAME_LENGTH_WITHOUT_SUFFIX
|
||||||
|
else part[:MAX_FILE_NAME_LENGTH_WITHOUT_SUFFIX]
|
||||||
|
for part in path.parts
|
||||||
|
]
|
||||||
|
return Path(*trimmed_parts)
|
||||||
|
|
||||||
|
|
||||||
def set_tags(
|
def set_tags(
|
||||||
|
|
@ -371,8 +382,9 @@ def write_via_temporary_file(
|
||||||
target_path: Path,
|
target_path: Path,
|
||||||
temporary_file_hook: Optional[Callable[[Path], None]] = None,
|
temporary_file_hook: Optional[Callable[[Path], None]] = None,
|
||||||
) -> Path:
|
) -> Path:
|
||||||
|
target_name = hashlib.sha256(target_path.name.encode()).hexdigest()
|
||||||
temporary_file = target_path.parent / (
|
temporary_file = target_path.parent / (
|
||||||
TEMPORARY_FILE_NAME_TEMPLATE.format(target_path.name)
|
TEMPORARY_FILE_NAME_TEMPLATE.format(target_name)
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
temporary_file.write_bytes(data)
|
temporary_file.write_bytes(data)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue