Skip to content

Commit d0e98dc

Browse files
Fix fetch video overlay
1 parent d4f35b2 commit d0e98dc

2 files changed

Lines changed: 34 additions & 14 deletions

File tree

cloudinary/utils.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,8 +1205,21 @@ def __process_text_options(layer, layer_parameter):
12051205

12061206

12071207
def process_layer(layer, layer_parameter):
1208-
if isinstance(layer, string_types) and layer.startswith("fetch:"):
1209-
layer = {"url": layer[len('fetch:'):]}
1208+
if isinstance(layer, string_types):
1209+
resource_type = None
1210+
if layer.startswith("fetch:"):
1211+
url = layer[len('fetch:'):]
1212+
elif layer.find(":fetch:", 0, 12) != -1:
1213+
resource_type, _, url = layer.split(":", 2)
1214+
else:
1215+
# nothing to process, a raw string, keep as is.
1216+
return layer
1217+
1218+
# handle remote fetch URL
1219+
layer = {"url": url, "type": "fetch"}
1220+
if resource_type:
1221+
layer["resource_type"] = resource_type
1222+
12101223
if not isinstance(layer, dict):
12111224
return layer
12121225

@@ -1215,19 +1228,19 @@ def process_layer(layer, layer_parameter):
12151228
type = layer.get("type")
12161229
public_id = layer.get("public_id")
12171230
format = layer.get("format")
1218-
fetch = layer.get("url")
1231+
fetch_url = layer.get("url")
12191232
components = list()
12201233

12211234
if text is not None and resource_type is None:
12221235
resource_type = "text"
12231236

1224-
if fetch and resource_type is None:
1225-
resource_type = "fetch"
1237+
if fetch_url and type is None:
1238+
type = "fetch"
12261239

12271240
if public_id is not None and format is not None:
12281241
public_id = public_id + "." + format
12291242

1230-
if public_id is None and resource_type != "text" and resource_type != "fetch":
1243+
if public_id is None and resource_type != "text" and type != "fetch":
12311244
raise ValueError("Must supply public_id for for non-text " + layer_parameter)
12321245

12331246
if resource_type is not None and resource_type != "image":
@@ -1251,8 +1264,6 @@ def process_layer(layer, layer_parameter):
12511264

12521265
if text is not None:
12531266
var_pattern = VAR_NAME_RE
1254-
match = re.findall(var_pattern, text)
1255-
12561267
parts = filter(lambda p: p is not None, re.split(var_pattern, text))
12571268
encoded_text = []
12581269
for part in parts:
@@ -1262,11 +1273,9 @@ def process_layer(layer, layer_parameter):
12621273
encoded_text.append(smart_escape(smart_escape(part, r"([,/])")))
12631274

12641275
text = ''.join(encoded_text)
1265-
# text = text.replace("%2C", "%252C")
1266-
# text = text.replace("/", "%252F")
12671276
components.append(text)
1268-
elif resource_type == "fetch":
1269-
b64 = base64_encode_url(fetch)
1277+
elif type == "fetch":
1278+
b64 = base64_encode_url(fetch_url)
12701279
components.append(b64)
12711280
else:
12721281
public_id = public_id.replace("/", ':')

test/test_utils.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
VIDEO_UPLOAD_PATH = DEFAULT_ROOT_PATH + 'video/upload/'
4848
TEST_ID = 'test'
4949

50-
FETCH_URL = "http://cloudinary.com/images/logo.png"
50+
FETCH_URL = "https://cloudinary.com/images/logo.png"
51+
FETCH_VIDEO_URL = "https://demo-res.cloudinary.com/videos/dog.mp4"
5152

5253
IMAGE_VERSION = "1234"
5354
IMAGE_VERSION_STR = "v" + IMAGE_VERSION
@@ -438,6 +439,14 @@ def test_fetch_overlay(self):
438439
+ "l_fetch:aHR0cDovL2Nsb3VkaW5hcnkuY29tL2ltYWdlcy9vbGRfbG9nby5wbmc=/"
439440
+ "test"))
440441

442+
"""should support video overlay"""
443+
self.__test_cloudinary_url(
444+
options={"overlay": "video:fetch:" + FETCH_VIDEO_URL},
445+
expected_url=(
446+
DEFAULT_UPLOAD_PATH
447+
+ "l_video:fetch:aHR0cHM6Ly9kZW1vLXJlcy5jbG91ZGluYXJ5LmNvbS92aWRlb3MvZG9nLm1wNA==/"
448+
+ "test"))
449+
441450
self.__test_cloudinary_url(
442451
options={
443452
"overlay": {
@@ -940,7 +949,9 @@ def test_overlay_options(self):
940949
"subtitles:Arial_40:sample_sub_he.srt"),
941950
({'url': "https://upload.wikimedia.org/wikipedia/commons/2/2b/고창갯벌.jpg"},
942951
"fetch:aHR0cHM6Ly91cGxvYWQud2lraW1lZGlhLm9yZy93aWtpcGVkaWEvY29"
943-
"tbW9ucy8yLzJiLyVFQSVCMyVBMCVFQyVCMCVCRCVFQSVCMCVBRiVFQiVCMiU4Qy5qcGc=")
952+
"tbW9ucy8yLzJiLyVFQSVCMyVBMCVFQyVCMCVCRCVFQSVCMCVBRiVFQiVCMiU4Qy5qcGc="),
953+
({'url': FETCH_VIDEO_URL, "resource_type": "video"},
954+
"video:fetch:aHR0cHM6Ly9kZW1vLXJlcy5jbG91ZGluYXJ5LmNvbS92aWRlb3MvZG9nLm1wNA==")
944955
]
945956

946957
for options, expected in tests:

0 commit comments

Comments
 (0)