Skip to content

Commit 63042d0

Browse files
Fix sync file extension normalization
1 parent 46a38b3 commit 63042d0

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

cloudinary_cli/modules/sync.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from cloudinary import api
88

99
from cloudinary_cli.utils.api_utils import query_cld_folder, upload_file, download_file
10-
from cloudinary_cli.utils.file_utils import walk_dir, delete_empty_dirs, get_destination_folder
10+
from cloudinary_cli.utils.file_utils import walk_dir, delete_empty_dirs, get_destination_folder, \
11+
normalize_file_extension
1112
from cloudinary_cli.utils.json_utils import print_json, read_json_from_file, write_json_to_file
1213
from cloudinary_cli.utils.utils import logger, run_tasks_concurrently, get_user_action, invert_dict
1314

@@ -149,7 +150,7 @@ def push(self):
149150
def save_sync_meta_file(self, upload_results):
150151
diverse_filenames = {}
151152
for local_path, remote_path in upload_results.items():
152-
local = path.relpath(local_path, self.local_dir)
153+
local = normalize_file_extension(path.relpath(local_path, self.local_dir))
153154
remote = path.relpath(remote_path, self.remote_dir)
154155
if local != remote:
155156
diverse_filenames[local] = remote

cloudinary_cli/utils/file_utils.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,37 @@
66
from cloudinary_cli.defaults import logger
77
from cloudinary_cli.utils.utils import etag
88

9+
FORMAT_ALIASES = {
10+
'jpeg': 'jpg',
11+
'jpe': 'jpg',
12+
'tif': 'tiff',
13+
'ps': 'eps',
14+
'ept': 'eps',
15+
'eps3': 'eps',
16+
'j2k': 'jpc',
17+
'jxr': 'wdp',
18+
'hdp': 'wdp',
19+
'm4v': 'mp4',
20+
'h264': 'mp4',
21+
'asf': 'wmv',
22+
'm2v': 'mpeg',
23+
'm2t': 'ts',
24+
'm2ts': 'ts',
25+
'aif': 'aiff',
26+
'aifc': 'aiff',
27+
'mka': 'webm',
28+
'webmda': 'webm',
29+
'webmdv': 'webm',
30+
'mp4dv': 'mp4',
31+
'mp4da': 'mp4',
32+
'opus': 'ogg',
33+
'bmp2': 'bmp',
34+
'bmp3': 'bmp',
35+
'mpg/3': 'mp3',
36+
'heif': 'heic',
37+
'mid': 'midi'
38+
}
39+
940

1041
def walk_dir(root_dir, include_hidden=False):
1142
all_files = {}
@@ -18,7 +49,8 @@ def walk_dir(root_dir, include_hidden=False):
1849
for file in files:
1950
full_path = path.join(root, file)
2051
relative_file_path = "/".join(p for p in [relative_path, file] if p)
21-
all_files[relative_file_path] = {
52+
normalized_relative_file_path = normalize_file_extension(relative_file_path)
53+
all_files[normalized_relative_file_path] = {
2254
"path": full_path,
2355
"etag": etag(full_path)
2456
}
@@ -82,3 +114,16 @@ def get_destination_folder(cloudinary_folder: str, file_path: str, parent: str =
82114

83115
return "/".join([cloudinary_folder, *folder_path]).strip("/")
84116

117+
118+
def normalize_file_extension(filename: str) -> str:
119+
"""
120+
Normalizes file extension. Makes it lower case and removes aliases.
121+
122+
:param filename: The input file name.
123+
:return: File name with normalized extension.
124+
"""
125+
filename, extension = os.path.splitext(filename)
126+
extension = extension[1:].lower()
127+
extension_alias = FORMAT_ALIASES.get(extension, extension)
128+
129+
return ".".join([p for p in [filename, extension_alias] if p])

test/test_file_utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from cloudinary_cli.utils.file_utils import get_destination_folder, walk_dir
3+
from cloudinary_cli.utils.file_utils import get_destination_folder, walk_dir, normalize_file_extension
44

55

66
class FileUtilsTest(unittest.TestCase):
@@ -21,3 +21,13 @@ def test_walk_dir(self):
2121

2222
self.assertEqual(1, len(walk_dir(test_dir, include_hidden=False)))
2323
self.assertEqual(4, len(walk_dir(test_dir, include_hidden=True)))
24+
25+
def test_normalize_file_extension(self):
26+
for value, expected in {
27+
"sample.jpg": "sample.jpg",
28+
"sample": "sample",
29+
"sample.JPG": "sample.jpg",
30+
"sample.JPE": "sample.jpg",
31+
"SAMPLE.JPEG": "SAMPLE.jpg",
32+
}.items():
33+
self.assertEqual(expected, normalize_file_extension(value))

0 commit comments

Comments
 (0)