-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCloudKey.java
More file actions
178 lines (153 loc) · 5.62 KB
/
CloudKey.java
File metadata and controls
178 lines (153 loc) · 5.62 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
package net.dmcloud.cloudkey;
import java.io.*;
import java.util.Map;
import net.dmcloud.util.*;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.codehaus.jackson.map.ObjectMapper;
public class CloudKey extends Api
{
public CloudKey(String _user_id, String _api_key)
{
super(_user_id, _api_key, CloudKey.API_URL, CloudKey.CDN_URL, "");
}
public CloudKey(String _user_id, String _api_key, String _base_url, String _cdn_url, String _proxy)
{
super(_user_id, _api_key, _base_url, _cdn_url, _proxy);
}
public String mediaGetEmbedUrl(String id) throws DCException
{
return this.mediaGetEmbedUrl(CloudKey.API_URL, id, CloudKey.SECLEVEL_NONE, "", "", "", null, null, 0);
}
public String mediaGetEmbedUrl(String id, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws DCException
{
return this.mediaGetEmbedUrl(CloudKey.API_URL, id, seclevel, asnum, ip, useragent, countries, referers, expires);
}
public String mediaGetEmbedUrl(String url, String id, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws DCException
{
String _url = url + "/embed/" + this.user_id + "/" + id;
return Helpers.sign_url(_url, this.api_key, seclevel, asnum, ip, useragent, countries, referers, expires);
}
public String mediaGetStreamUrl(String id) throws DCException
{
return this.mediaGetStreamUrl(CloudKey.API_URL, id, "mp4_h264_aac", CloudKey.SECLEVEL_NONE, "", "", "", null, null, 0, "", false);
}
public String mediaGetStreamUrl(String id, String asset_name) throws DCException
{
return this.mediaGetStreamUrl(CloudKey.API_URL, id, asset_name, CloudKey.SECLEVEL_NONE, "", "", "", null, null, 0, "", false);
}
public String mediaGetStreamUrl(String id, String asset_name, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires, String extension, Boolean download) throws DCException
{
return this.mediaGetStreamUrl(CloudKey.API_URL, id, asset_name, seclevel, asnum, ip, useragent, countries, referers, expires, extension, download);
}
public String mediaGetStreamUrl(String url, String id, String asset_name, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires, String extension, Boolean download) throws DCException
{
if (extension.equals(""))
{
String[] parts = asset_name.split("\\_");
extension = (!parts[0].equals(asset_name)) ? parts[0] : extension;
}
if (asset_name.length() >= 15 && asset_name.substring(0, 15).equals("jpeg_thumbnail_"))
{
return CloudKey.STATIC_URL + this.user_id + "/" + id + "/" + asset_name + "." + extension;
}
else
{
String _url = this.cdn_url + "/route/" + this.user_id + "/" + id + "/" + asset_name + ((!extension.equals("")) ? "." + extension : "");
return Helpers.sign_url(_url, this.api_key, seclevel, asnum, ip, useragent, countries, referers, expires) + (download ? "&throttle=0&helper=0&cache=0" : "");
}
}
public String mediaCreate() throws Exception
{
return mediaCreate("");
}
public String mediaCreate(String url) throws Exception
{
return mediaCreate(url, null, null);
}
public String mediaCreate(String url, DCArray assets_names, DCObject meta) throws Exception
{
DCObject args = DCObject.create().push("url", url);
if (assets_names != null && assets_names.size() > 0)
{
args.push("assets_names", assets_names);
}
if (meta != null && meta.size() > 0)
{
args.push("meta", meta);
}
DCObject result = this.call("media.create", args);
return result.pull("id");
}
public String mediaCreate(File f) throws Exception
{
return this.mediaCreate(f, null, null);
}
public String mediaCreate(File f, DCArray assets_names, DCObject meta) throws Exception
{
String upload_url = this.fileUpload();
PostMethod filePost = null;
try
{
filePost = new PostMethod(upload_url);
Part[] parts = {
new FilePart("file", f)
};
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
int status = client.executeMethod(filePost);
if (status == HttpStatus.SC_OK)
{
ObjectMapper mapper = new ObjectMapper();
DCObject json_response = DCObject.create(mapper.readValue(filePost.getResponseBodyAsString(), Map.class));
return this.mediaCreate(json_response.pull("url"), assets_names, meta);
}
else
{
throw new DCException("Upload failed.");
}
}
catch (Exception e)
{
throw new DCException("Upload failed: " + e.getMessage());
}
finally
{
if (filePost != null)
{
filePost.releaseConnection();
}
}
}
public void mediaDelete(String id) throws Exception
{
this.call("media.delete", DCObject.create().push("id", id));
}
public String fileUpload() throws Exception
{
DCObject result = fileUpload(false, "", "");
return result.pull("url");
}
public DCObject fileUpload(Boolean status, String jsonp_cb, String target) throws Exception
{
DCObject args = DCObject.create();
if (status)
{
args.push("status", true);
}
if (!jsonp_cb.equals(""))
{
args.push("jsonp_cb", jsonp_cb);
}
if (!target.equals(""))
{
args.push("target", target);
}
return (DCObject) this.call("file.upload", args);
}
}