-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_utils.py
More file actions
438 lines (337 loc) · 15.3 KB
/
Copy pathapi_utils.py
File metadata and controls
438 lines (337 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import logging
from os import path, makedirs
import cloudinary
import requests
from click import style, launch
from cloudinary import Search, SearchFolders, uploader, api
from cloudinary.exceptions import AuthorizationRequired
from cloudinary.utils import cloudinary_url
from cloudinary_cli.defaults import logger
from cloudinary_cli.utils.config_utils import is_valid_cloudinary_config, user_config_names
from cloudinary_cli.utils.file_utils import (normalize_file_extension, posix_rel_path, get_destination_folder,
populate_duplicate_name)
from cloudinary_cli.utils.json_utils import print_json, write_json_to_file
from cloudinary_cli.utils.utils import log_exception, confirm_action, get_command_params, merge_responses, \
normalize_list_params, ConfigurationError, print_api_help, duplicate_values, should_dump_responses
import re
from cloudinary.utils import is_remote_url
PAGINATION_MAX_RESULTS = 500
_cursor_fields = {"resource": "derived_next_cursor"}
# Selector-style destructive bulk Admin API methods.
# These can match an open-ended set of assets without the caller enumerating each target,
# so an interactive confirmation is required (bypassable via --force).
DESTRUCTIVE_BULK_API_METHODS = {
"delete_all_resources",
"delete_resources_by_prefix",
"delete_resources_by_tag",
}
# Server-enforced cap on how many resources a single destructive bulk call can affect
# (NConfig.max_resource_count_for_delete). Used purely for clearer prompt wording.
MAX_DESTRUCTIVE_BULK_PER_CALL = 1000
# Public, unauthenticated API methods that must run without a Cloudinary configuration.
PUBLIC_API_METHODS = {
"provisioning": {"create_agent_account"},
}
def is_public_api_method(api_name, method_name):
return method_name in PUBLIC_API_METHODS.get(api_name, set())
def is_destructive_bulk_api_method(method_name):
return method_name in DESTRUCTIVE_BULK_API_METHODS
def confirm_destructive_bulk_api_method(api_name, method_name, force=False):
if force or not is_destructive_bulk_api_method(method_name):
return True
display_api_name = api_name.capitalize()
if not confirm_action(
f"This will delete up to {MAX_DESTRUCTIVE_BULK_PER_CALL} matching assets via "
f"{display_api_name} API method '{method_name}'. Continue? (y/N)"):
logger.info("Stopping.")
return False
return True
def query_cld_folder(folder, folder_mode, status=None):
files = {}
folder = folder.strip('/') # omit redundant leading slash and duplicate trailing slashes in query
folder_key = "asset_folder" if folder_mode == "dynamic" else "folder"
folder_query = f"{folder}/*" if folder else "*"
status_value = "(active OR pending)" if status == "all" else status
status_query = f" AND status:{status_value}" if status_value else ""
search = Search().expression(f"{folder_key}:\"{folder_query}\"{status_query}").with_field("image_analysis").max_results(500)
logger.debug(f"Search expression: {search.to_json()}")
next_cursor = True
while next_cursor:
res = call_api(search.execute)
for asset in res['resources']:
rel_path = _relative_path(asset, folder)
rel_display_path = _relative_display_path(asset, folder)
path_key = rel_display_path if folder_mode == "dynamic" else rel_path
normalized_path_key = normalize_file_extension(path_key)
files[asset["asset_id"]] = {
"asset_id": asset['asset_id'],
"normalized_path": normalized_path_key,
"normalized_unique_path": normalized_path_key,
"type": asset['type'],
"resource_type": asset['resource_type'],
"public_id": asset['public_id'],
"format": asset['format'],
"etag": asset.get('etag', '0'),
"relative_path": rel_path, # save for inner use
"access_mode": asset.get('access_mode', 'public'),
"created_at": asset.get('created_at'),
# dynamic folder mode fields
"asset_folder": asset.get('asset_folder'),
"display_name": asset.get('display_name'),
"relative_display_path": rel_display_path
}
# use := when switch to python 3.8
next_cursor = res.get('next_cursor')
search.next_cursor(next_cursor)
return files
def cld_folder_exists(folder):
folder = folder.strip('/') # omit redundant leading slash and duplicate trailing slashes in query
if not folder:
return True # root folder
res = call_api(SearchFolders().expression(f"path=\"{folder}\"").execute)
return res.get("total_count", 0) > 0
def _display_path(asset):
if asset.get("display_name") is None:
return ""
if asset["resource_type"] == "raw" or asset["type"] == 'fetch':
normalized_display_name = asset["display_name"]
else:
normalized_display_name = ".".join(filter(None, [asset["display_name"], asset.get("format", None)]))
return "/".join([asset.get("asset_folder", ""), normalized_display_name])
def _relative_display_path(asset, folder):
if asset.get("display_name") is None:
return ""
return posix_rel_path(_display_path(asset), folder)
def _relative_path(asset, folder):
source = asset_source(asset)
if not source.startswith(folder):
return source
return posix_rel_path(asset_source(asset), folder)
def regen_derived_version(public_id, delivery_type, res_type,
eager_trans, eager_async,
eager_notification_url):
options = {"type": delivery_type, "resource_type": res_type,
"eager": eager_trans, "eager_async": eager_async,
"eager_notification_url": eager_notification_url,
"overwrite": True, "invalidate": True}
try:
exp_res = call_api(uploader.explicit, public_id, **options)
derived_url = f'{exp_res.get("eager")[0].get("secure_url")}'
msg = ('Processing' if options.get('eager_async') else 'Regenerated') + f' {derived_url}'
logger.info(style(msg, fg="green"))
except Exception as e:
error_msg = (f"Failed to regenerate {public_id} of type: "
f"{options.get('type')} and resource_type: "
f"{options.get('resource_type')}")
log_exception(e, error_msg)
def upload_file(file_path, options, uploaded=None, failed=None):
uploaded = uploaded if uploaded is not None else {}
failed = failed if failed is not None else {}
verbose = should_dump_responses()
try:
size = 0 if is_remote_url(file_path) else path.getsize(file_path)
# Fresh options copy: upload_large mutates it (sets public_id), so a retry stays independent.
if size > 20000000:
# upload_large recovers from a token 401 per chunk (SDK oauth_token_refresh_callback),
# resuming the upload; a whole-file retry here would restart from byte 0.
result = uploader.upload_large(file_path, **dict(options))
else:
result = call_api(uploader.upload, file_path, **dict(options))
disp_path = _display_path(result)
if "batch_id" in result:
starting_msg = "Uploading"
disp_str = f"asynchronously with batch_id: {result['batch_id']}"
else:
starting_msg = "Successfully uploaded"
disp_str = f"as {result['public_id']}" if not disp_path \
else f"as {disp_path} with public_id: {result['public_id']}"
logger.info(style(f"{starting_msg} {file_path} {disp_str}", fg="green"))
if verbose:
print_json(result)
uploaded[file_path] = {"path": asset_source(result), "display_path": disp_path}
except Exception as e:
log_exception(e, f"Failed uploading {file_path}")
failed[file_path] = str(e)
def get_default_upload_options(folder_mode):
options = {
'resource_type': 'auto'
}
if folder_mode == 'fixed':
options = {
**options,
'use_filename': True,
'unique_filename': False,
'invalidate': True,
}
if folder_mode == 'dynamic':
options = {
**options,
'use_filename_as_display_name': True,
}
return options
def get_destination_folder_options(file, remote_dir, folder_mode, parent=None):
destination_folder = get_destination_folder(remote_dir, file, parent)
if folder_mode == "dynamic":
return {"asset_folder": destination_folder}
return {"folder": destination_folder}
def download_file(remote_file, local_path, downloaded=None, failed=None):
downloaded = downloaded if downloaded is not None else {}
failed = failed if failed is not None else {}
makedirs(path.dirname(local_path), exist_ok=True)
if remote_file['type'] in ("private", "authenticated") or remote_file['access_mode'] == "authenticated":
sign_url = True
else:
sign_url = False
download_url = cloudinary_url(asset_source(remote_file), resource_type=remote_file['resource_type'],
type=remote_file['type'], sign_url=sign_url)[0]
result = requests.get(download_url)
if result.status_code != 200:
err = result.headers.get('x-cld-error')
msg = f"Failed downloading: {download_url}, status code: {result.status_code}, " \
f"details: {err}"
logger.error(msg)
failed[download_url] = err
return
with open(local_path, "wb") as f:
f.write(result.content)
downloaded[remote_file['relative_path']] = local_path
logger.info(style("Downloaded '{}' to '{}'".format(remote_file['relative_path'], local_path), fg="green"))
def asset_source(asset_details):
"""
Public ID of the transformable file (image/video) does not include file extension.
It needs to be added in order to download the file properly (without creating a derived asset).
Raw files are accessed using only public_id.
Fetched files are not altered as well.
:param asset_details: The details of the asset.
:rtype asset_details: dict
:return:
"""
base_name = asset_details.get('public_id', '')
if not base_name:
return base_name
if asset_details['resource_type'] == 'raw' or asset_details['type'] == 'fetch':
return base_name
return base_name + '.' + asset_details.get('format', '')
def get_folder_mode():
"""
Returns folder mode of the cloud.
:return: String representing folder mode. Can be "fixed" or "dynamic".
"""
try:
config_res = call_api(api.config, settings="true")
mode = config_res["settings"]["folder_mode"]
logger.debug(f"Using {mode} folder mode")
except Exception as e:
log_exception(e, f"Failed getting cloud configuration")
raise
return mode
def call_api(func, *args, **kwargs):
"""
Run an SDK call (function-style API or Search().execute), retrying once on an OAuth 401 after
invalidating the rejected token, then log at debug and re-raise on failure.
"""
config = cloudinary.config()
token = getattr(config, "oauth_token", None) # the token this request will carry
# Pin the token so the value sent is provably the value handed to invalidate_token: without it the
# SDK re-reads the self-refreshing oauth_token and a peer rotation between reads breaks the decision.
pinned = dict(kwargs, oauth_token=token) if token else kwargs
try:
try:
return func(*args, **pinned)
except AuthorizationRequired:
if not getattr(config, "has_oauth", False) or not config.invalidate_token(token):
raise
return func(*args, **kwargs) # retry unpinned: the SDK re-reads the freshly rotated token
except Exception:
logger.debug(f"Failed calling '{func.__name__}' with args: {args} and optional args {kwargs}", exc_info=True)
raise
def handle_command(
params,
optional_parameter,
optional_parameter_parsed,
module,
module_name):
try:
func, args, kwargs = get_command_params(params, optional_parameter, optional_parameter_parsed, module,
module_name)
except Exception as e:
log_exception(e)
return False
try:
return call_api(func, *args, **kwargs)
except Exception as e:
log_exception(e)
return False
def handle_api_command(
params,
optional_parameter,
optional_parameter_parsed,
ls,
save,
doc,
doc_url,
api_instance,
api_name,
auto_paginate=False,
force=False,
filter_fields=None,
return_data=False):
"""
Used by Admin and Upload API commands
"""
if doc:
return launch(doc_url)
if ls or len(params) < 1:
return print_api_help(api_instance)
try:
func, args, kwargs = get_command_params(params, optional_parameter, optional_parameter_parsed, api_instance,
api_name)
except Exception as e:
log_exception(e)
return False
if not confirm_destructive_bulk_api_method(api_name, func.__name__, force):
return False
if not is_public_api_method(api_name, func.__name__) and not is_valid_cloudinary_config():
if user_config_names():
raise ConfigurationError("No default Cloudinary configuration is set. "
"Select one with `-C <name>` or set a default with `cld config -d <name>`.")
raise ConfigurationError("No Cloudinary configuration found.")
try:
res = call_api(func, *args, **kwargs)
except Exception as e:
log_exception(e)
return False
if auto_paginate:
res = handle_auto_pagination(res, func, args, kwargs, force, filter_fields)
if return_data:
return res
print_json(res)
if save:
write_json_to_file(res, save)
def handle_auto_pagination(res, func, args, kwargs, force, filter_fields):
cursor_field = _cursor_fields.get(func.__name__, "next_cursor")
if cursor_field not in res:
return res
if not force:
if not confirm_action(
"Using auto pagination will use multiple API calls.\n" +
f"You currently have {res.rate_limit_remaining} Admin API calls remaining. Continue? (y/N)"):
logger.info("Stopping. Please run again without -A.")
return res
else:
logger.info("Continuing. You may use the -F flag to force auto_pagination.")
fields_to_keep = []
if filter_fields:
fields_to_keep = normalize_list_params(filter_fields)
kwargs['max_results'] = PAGINATION_MAX_RESULTS
all_results = res
# We have many different APIs that have different fields that we paginate.
# The field is unknown before we perform the second call and then compare results and find the field.
pagination_field = None
while res.get(cursor_field, None):
kwargs[cursor_field] = res.get(cursor_field, None)
res = call_api(func, *args, **kwargs)
all_results, pagination_field = merge_responses(all_results, res, fields_to_keep=fields_to_keep,
pagination_field=pagination_field)
all_results.pop(cursor_field, None)
return all_results