Skip to content

Commit a44acef

Browse files
Brian Lukconst-cloudinary
authored andcommitted
Refactor path/public_id formatting
1 parent 420b074 commit a44acef

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

cloudinary_cli/modules/sync.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import logging
22
from functools import reduce
33
from itertools import product
4-
from os import sep, remove
4+
from os import remove
55
from os.path import split, join as path_join, abspath
66

77
from click import command, argument, option, style
88
from cloudinary import api
99

1010
from cloudinary_cli.utils.api_utils import query_cld_folder, upload_file, download_file
11-
from cloudinary_cli.utils.file_utils import walk_dir, delete_empty_dirs
11+
from cloudinary_cli.utils.file_utils import walk_dir, delete_empty_dirs, get_destination_folder
1212
from cloudinary_cli.utils.json_utils import print_json
1313
from cloudinary_cli.utils.utils import logger, run_tasks_concurrently, confirm_action
1414

@@ -84,7 +84,6 @@ def push(self):
8484
return False
8585

8686
files_to_push = self.unique_local_file_names | self.out_of_sync_file_names
87-
8887
to_upload = list(filter(lambda x: split(x)[1][0] != ".", files_to_push))
8988
logger.info(f"Uploading {len(to_upload)} items to Cloudinary folder '{self.remote_dir}'")
9089

@@ -96,7 +95,7 @@ def push(self):
9695
}
9796
uploads = []
9897
for file in to_upload:
99-
folder = "/".join([self.remote_dir, *split(file)[:-1]])
98+
folder = get_destination_folder(self.remote_dir, file)
10099

101100
uploads.append((self.local_files[file]['path'], {**options, 'folder': folder}))
102101

cloudinary_cli/modules/upload_dir.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from cloudinary_cli.utils.api_utils import upload_file
77
from cloudinary_cli.utils.utils import parse_option_value, logger, run_tasks_concurrently
8+
from cloudinary_cli.utils.file_utils import get_destination_folder
89

910

1011
@command("upload_dir", help="""Upload a folder of assets, maintaining the folder structure.""")
@@ -43,10 +44,11 @@ def upload_dir(directory, optional_parameter, optional_parameter_parsed, transfo
4344
for root, _, files in walk(dir_to_upload):
4445
for fi in files:
4546
file_path = abspath(path_join(dir_to_upload, root, fi))
46-
mod_folder = path_join(folder, dirname(file_path[len(parent) + 1:]))
47+
4748
if split(file_path)[1][0] == ".":
4849
continue
49-
options = {**options, "folder": mod_folder}
50+
51+
options = {**options, "folder": get_destination_folder(folder, file_path, parent=parent)}
5052
uploads.append((file_path, options, items, skipped))
5153

5254
run_tasks_concurrently(upload_file, uploads, concurrent_workers)

cloudinary_cli/utils/file_utils.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from os import walk, path, listdir, rmdir
1+
from os import walk, path, listdir, rmdir, sep
2+
from os.path import split, relpath, abspath
23

34
from cloudinary_cli.defaults import logger
45
from cloudinary_cli.utils.utils import etag
@@ -30,3 +31,22 @@ def delete_empty_dirs(root, remove_root=False):
3031
if len(files) == 0 and remove_root:
3132
logger.debug(f"Removing empty folder '{root}'")
3233
rmdir(root)
34+
35+
36+
def get_destination_folder(cloudinary_folder: str, file_path: str, parent: str = None) -> str:
37+
"""
38+
:param cloudinary_folder: Destination folder in Cloudinary
39+
:param file_path: Path to the local file
40+
:param parent: Parent folder of the directory to upload/sync
41+
:return: Value passed to `folder` when uploading to Cloudinary
42+
"""
43+
folder_path = []
44+
45+
parent_path = abspath(parent) if parent else None
46+
splitted = split(relpath(file_path, parent_path))
47+
48+
if splitted[0]:
49+
folder_path = splitted[0].split(sep)
50+
51+
return "/".join([cloudinary_folder, *folder_path]).strip("/")
52+

test/test_file_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import unittest
2+
3+
from cloudinary_cli.utils.file_utils import get_destination_folder
4+
5+
6+
class UtilsTest(unittest.TestCase):
7+
def test_get_destination_folder(self):
8+
""" should parse option values correctly """
9+
10+
self.assertEqual("1/2/3", get_destination_folder("1", "2/3/file.jpg"))
11+
self.assertEqual("1", get_destination_folder("1", "file.jpg"))
12+
self.assertEqual("cloudinaryfolder/myfolder/subfolder",
13+
get_destination_folder("cloudinaryfolder",
14+
"/Users/user/myfolder/subfolder/file.jpg",
15+
parent="/Users/user/"))
16+

0 commit comments

Comments
 (0)