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

PixivHelper.sanitize_filename(): correctly cutting filename on linux #617

Merged
merged 1 commit into from
Jan 2, 2020
Merged
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
19 changes: 15 additions & 4 deletions PixivHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import logging.handlers
import os
import platform
import re
import shlex
import shutil
Expand Down Expand Up @@ -108,10 +109,20 @@ def sanitize_filename(s, rootDir=None):
while name.find(os.sep + os.sep) >= 0:
name = name.replace(os.sep + os.sep, os.sep)

# cut to 255 char
if len(name) > 255:
newLen = 250
name = name[:newLen]
if platform.system() == 'Linux':
# Linux: cut filename to <= 249 bytes
dirname, basename = os.path.split(name)
while len(basename.encode('utf-8')) > 249:
filename, extname = os.path.splitext(basename)
filename = filename[:len(filename) - 1]
basename = filename + extname

name = dirname + os.sep + basename
else:
# cut path to 255 char
if len(name) > 255:
newLen = 250
name = name[:newLen]

# Remove unicode control character
tempName = ""
Expand Down