-
Notifications
You must be signed in to change notification settings - Fork 2
/
cloudinary-export-csv.php
90 lines (80 loc) · 2.7 KB
/
cloudinary-export-csv.php
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
<?php
/*
Download list of all stored resources on Cloudinary as CSV file.
https://atakanau.blogspot.com/2018/12/cloudinary-yuklu-tum-dosyalar-listeleme.html
*/
function src_read($resource_type,$next_cursor=false,$first=false){
// use your parameters
$API_KEY = ''; // <-- your API KEY
$API_SECRET = ''; // <-- your API SECRET
$CLOUD_NAME = ''; // <-- your CLOUD NAME
$result = new stdClass();
$result -> output = '';
$result -> first = $first;
$output_new = '';
$handle = curl_init();
$url = 'https://'.$API_KEY.':'.$API_SECRET.'@api.cloudinary.com/v1_1/'.$CLOUD_NAME.'/resources/'.$resource_type
.'?max_results=500'
.( $next_cursor ? '&next_cursor='.$next_cursor : '' )
;
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$readed = curl_exec($handle);
curl_close($handle);
$data=json_decode($readed, true);
$result -> next_cursor = isset($data['next_cursor']) ? $data['next_cursor'] : false;
if( isset($data['resources']) && count($data['resources']) ){
if( $result -> first ){
$result -> first = false;
foreach($data['resources'] as $rsc){
// fix uncommon columns
if($resource_type=='raw'){
$rsc = insert_column($rsc,2,array('format' => ''));
$rsc = insert_column($rsc,8,array('width' => '','height' => ''));
}
elseif($resource_type=='image' || $resource_type=='video'){
$rsc = insert_column($rsc,11,array('access_mode' => ''));
}
foreach ($rsc as $key => $value){
$output_new .= "$key\t";
}
$output_new .= "\r\n";
break;
}
}
foreach($data['resources'] as $rsc){
// fix uncommon columns
if($resource_type=='raw'){
$rsc = insert_column($rsc,2,array('format' => ''));
$rsc = insert_column($rsc,8,array('width' => '','height' => ''));
}
elseif($resource_type=='image' || $resource_type=='video'){
$rsc = insert_column($rsc,11,array('access_mode' => ''));
}
foreach ($rsc as $key => $value){
$output_new .= "$value\t";
}
$output_new .= "\r\n";
}
$result -> output = $output_new;
}
return $result;
}
function insert_column($row,$afterIndex,$newVal){
return array_merge(array_slice($row,0,$afterIndex), $newVal,array_slice($row,$afterIndex));
}
$output = '';
$first = true;
$next_cursor = false;
$resource_types = ["raw","image","video"];
foreach($resource_types as $resource_type){
do{
$r = src_read($resource_type,$next_cursor,$first);
$output .= $r -> output;
$first = $r-> first;
}while($next_cursor = $r -> next_cursor);
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"cloudinary-all-resources-list.csv\"");
echo $output;
?>