[utils] Prevent hyphen at beginning of filename (Fixes #5035)
authorPhilipp Hagemeister <phihag@phihag.de>
Tue, 24 Feb 2015 10:38:01 +0000 (11:38 +0100)
committerPhilipp Hagemeister <phihag@phihag.de>
Tue, 24 Feb 2015 10:38:01 +0000 (11:38 +0100)
test/test_utils.py
youtube_dl/utils.py

index c7373af1e2f64b8bc0bf0003d162b4e4b697eb87..2f8996d7bb5088e4c38470974b41e268fb260377 100644 (file)
@@ -85,6 +85,8 @@ class TestUtil(unittest.TestCase):
         self.assertEqual(
             sanitize_filename('New World record at 0:12:34'),
             'New World record at 0_12_34')
+        self.assertEqual(sanitize_filename('--gasdgf'), '_-gasdgf')
+        self.assertEqual(sanitize_filename('--gasdgf', is_id=True), '--gasdgf')
 
         forbidden = '"\0\\/'
         for fc in forbidden:
index 475fad3c903f9a2923def9f186c746d067807a68..e2631dccda7a13679da2280b1e8d38301b7ffe47 100644 (file)
@@ -304,6 +304,8 @@ def sanitize_filename(s, restricted=False, is_id=False):
         # Common case of "Foreign band name - English song title"
         if restricted and result.startswith('-_'):
             result = result[2:]
+        if result.startswith('-'):
+            result = '_' + result[len('-'):]
         if not result:
             result = '_'
     return result