This repository was archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
171 lines (117 loc) · 3.84 KB
/
index.js
File metadata and controls
171 lines (117 loc) · 3.84 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
"use strict";
var _ = require("lodash"),
Q = require("q"),
cloudinary = require("cloudinary");
/**
*
* Get resources that have been tagged by the given argument
*
* @param {String} tag
* @param {Object} options
* @return {Promise}
*
*/
var getResources = function(tag, options){
var dfd = Q.defer();
var getter = tag ? cloudinary.api.resources_by_tag : cloudinary.api.resources,
args = tag ? [tag] : [];
try {
args = args.concat(function(list){
if(list.error){
dfd.reject(list.error);
return;
}
dfd.resolve(list);
});
args = args.concat(_.extend({
cloud_name: options.cloud_name,
api_key: options.api_key,
api_secret: options.api_secret,
max_results: 100,
tags: true
}, options));
getter.apply({}, args);
}
catch(e){
dfd.reject(e);
}
return dfd.promise;
};
/**
*
* Cloudinary stats operations
* @class
*
* @type {Object} cOptions
*
*/
var CloudinaryStats = function(cOptions){
return {
ls: function(tag, options){
options.maxPages = options.maxPages || 999999;
var currentPage = 0;
/// recursive call to resources
var recursiveCalculateResourcesSize = function(size, next_cursor){
var rOptions = _.extend({}, options, cOptions, { next_cursor: next_cursor });
return getResources(tag, rOptions).then(function(r){
r = _.extend({ resources: [] }, r);
var pageSize = 0;
_.each(r.resources, function(resource){
if(!options.format){
console.log(
_.map(_.keys(resource), function(field){
return field + ":" + resource[field];
}).join("\t"));
}
else {
console.log(
_.map(options.format.split(","), function(field){
return resource[field];
}).join("\t"));
}
pageSize += resource.bytes;
});
return _.extend(r, { pageSize: pageSize })
})
.then(function(r){
size = r.pageSize + size;
currentPage++;
if(currentPage < options.maxPages && r.next_cursor){
return recursiveCalculateResourcesSize(size, r.next_cursor);
}
/// if a format is specified ignore the totals
if(options.format){
return;
}
console.log("\n");
console.log([
"Pages",
currentPage
].join("\t"));
console.log([
"Size",
size
].join("\t"));
});
};
return recursiveCalculateResourcesSize(0);
},
rm: function(id){
var dfd = Q.defer();
cloudinary.api.delete_resources(id, function(r){
console.log(r)
if(r.error){
dfd.reject(r.error);
return;
}
dfd.resolve(r);
}, {
cloud_name: cOptions.cloud_name,
api_key: cOptions.api_key,
api_secret: cOptions.api_secret,
});
return dfd.promise;
}
};
};
module.exports = CloudinaryStats;