Skip to content

Commit

Permalink
Merge pull request #1023 from Nojus297/filename_fix
Browse files Browse the repository at this point in the history
Filename sanitation fix (#1009)
  • Loading branch information
tommysolsen committed Feb 22, 2020
2 parents af74a6d + 16ac040 commit 160fec9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions mps_youtube/commands/download.py
Expand Up @@ -195,7 +195,8 @@ def down_plist(dltype, parturl):
plist(parturl)
dump(False)
title = g.pafy_pls[parturl][0].title
subdir = util.mswinfn(title.replace("/", "-"))
# Remove double quotes for convenience
subdir = util.sanitize_filename(title.replace('"', ''))
down_many(dltype, "1-", subdir=subdir)
msg = g.message
plist(parturl)
Expand Down Expand Up @@ -227,8 +228,9 @@ def _make_fname(song, ext=None, av=None, subdir=None):

# filename = song.title[:59] + "." + ext
filename = song.title + "." + ext
filename = os.path.join(ddir, util.mswinfn(filename.replace("/", "-")))
# Remove double quotes for convenience
filename = filename.replace('"', '')
filename = os.path.join(ddir, util.sanitize_filename(filename))
return filename


Expand Down
14 changes: 14 additions & 0 deletions mps_youtube/util.py
Expand Up @@ -9,6 +9,7 @@
import unicodedata
import urllib
import json
import platform
from datetime import datetime, timezone

import pafy
Expand All @@ -18,6 +19,7 @@

from importlib import import_module

macos = platform.system() == "Darwin"

mswin = os.name == "nt"
not_utf8_environment = mswin or "UTF-8" not in sys.stdout.encoding
Expand Down Expand Up @@ -124,6 +126,18 @@ def mswinfn(filename):

return filename

def sanitize_filename(filename, ignore_slashes=False):
""" Sanitize filename """
if not ignore_slashes:
filename = filename.replace('/', '-')
if macos:
filename = filename.replace(':', '_')
if mswin:
filename = utf8_replace(filename) if not_utf8_environment else filename
allowed = re.compile(r'[^\\?*$\'"%&:<>|]')
filename = "".join(x if allowed.match(x) else "_" for x in filename)

return filename

def set_window_title(title):
""" Set terminal window title. """
Expand Down

0 comments on commit 160fec9

Please sign in to comment.