Skip to content

Commit 046b8d6

Browse files
committed
refactoring CloudKey & add CloudKey_Media
1 parent 55452b9 commit 046b8d6

File tree

4 files changed

+156
-160
lines changed

4 files changed

+156
-160
lines changed

cloudkey.jar

-3 Bytes
Binary file not shown.

src/com/dmcloud/CloudKey.java

Lines changed: 88 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,121 @@
11
package com.dmcloud;
22

3-
import java.util.ArrayList;
43
import java.util.Map;
4+
import org.codehaus.jackson.map.ObjectMapper;
55

6-
public class CloudKey extends CloudKey_LL {
6+
public class CloudKey
7+
{
8+
public static int CLOUDKEY_SECLEVEL_NONE = 0;
9+
public static int CLOUDKEY_SECLEVEL_DELEGATE = 1 << 0;
10+
public static int CLOUDKEY_SECLEVEL_ASNUM = 1 << 1;
11+
public static int CLOUDKEY_SECLEVEL_IP = 1 << 2;
12+
public static int CLOUDKEY_SECLEVEL_USERAGENT = 1 << 3;
13+
public static int CLOUDKEY_SECLEVEL_USEONCE = 1 << 4;
14+
public static int CLOUDKEY_SECLEVEL_COUNTRY = 1 << 5;
15+
public static int CLOUDKEY_SECLEVEL_REFERER = 1 << 6;
716

17+
public static String CLOUDKEY_API_URL = "http://api.dmcloud.net";
18+
public static String CLOUDKEY_CDN_URL = "http://cdn.dmcloud.net";
19+
public static String CLOUDKEY_STATIC_URL = "http://static.dmcloud.net";
20+
21+
protected String user_id = null;
22+
protected String api_key = null;
23+
protected String base_url = null;
24+
protected String cdn_url = null;
25+
protected String proxy = null;
26+
827
public CloudKey(String _user_id, String _api_key) throws Exception
928
{
10-
super(_user_id, _api_key, CLOUDKEY_API_URL, CLOUDKEY_CDN_URL, "");
29+
this(_user_id, _api_key, CLOUDKEY_API_URL, CLOUDKEY_CDN_URL, "");
1130
}
1231

1332
public CloudKey(String _user_id, String _api_key, String _base_url, String _cdn_url, String _proxy) throws Exception
1433
{
15-
super(_user_id, _api_key, _base_url, _cdn_url, _proxy);
34+
if (_user_id == null || _api_key == null)
35+
{
36+
throw new CloudKey_Exception("You must provide valid user_id and api_key parameters");
37+
}
38+
this.user_id = _user_id;
39+
this.api_key = _api_key;
40+
this.base_url = _base_url;
41+
this.cdn_url = _cdn_url;
42+
this.proxy = _proxy;
1643
}
17-
18-
public String mediaCreate() throws Exception
44+
45+
public DCObject call(String call, DCObject args) throws Exception
1946
{
20-
return mediaCreate("", null, null);
47+
ObjectMapper mapper = new ObjectMapper();
48+
49+
DCObject jo = DCObject.create()
50+
.push("call", call)
51+
.push("args", args);
52+
53+
jo.push("auth", this.user_id + ":" + CloudKey_Helpers.md5(this.user_id + CloudKey_Helpers.normalize(jo) + this.api_key));
54+
55+
String json_encoded = mapper.writeValueAsString(jo);
56+
String response = CloudKey_Helpers.curl(this.base_url + "/api", json_encoded);
57+
DCObject json_response = DCObject.create(mapper.readValue(response, Map.class));
58+
if (json_response.containsKey("error"))
59+
{
60+
String message = "";
61+
int error_code = Integer.parseInt(json_response.pull("error.code"));
62+
switch (error_code)
63+
{
64+
case 200 : message = "ProcessorException"; break;
65+
case 300 : message = "TransportException"; break;
66+
case 400 : message = "AuthenticationErrorException"; break;
67+
case 410 : message = "RateLimitExceededException"; break;
68+
case 500 : message = "SerializerException"; break;
69+
70+
case 600 : message = "InvalidRequestException"; break;
71+
case 610 : message = "InvalidObjectException"; break;
72+
case 620 : message = "InvalidMethodException"; break;
73+
case 630 : message = "InvalidParamException"; break;
74+
75+
case 1000 : message = "ApplicationException"; break;
76+
case 1010 : message = "NotFoundException"; break;
77+
case 1020 : message = "ExistsException"; break;
78+
case 1030 : message = "LimitExceededException"; break;
79+
80+
default : message = "RPCException (error:" + json_response.pull("error.code").toString() + ")"; break;
81+
}
82+
message += " : " + json_response.pull("error.message").toString();
83+
throw new CloudKey_Exception(message, error_code);
84+
}
85+
return DCObject.create((Map)json_response.get("result"));
2186
}
2287

23-
public String mediaCreate(String url) throws Exception
88+
public String get_embed_url(String id) throws CloudKey_Exception
2489
{
25-
return mediaCreate(url, null, null);
90+
return this.get_embed_url(CLOUDKEY_API_URL, id, CloudKey.CLOUDKEY_SECLEVEL_NONE, "", "", "", null, null, 0);
2691
}
2792

28-
public String mediaCreate(String url, DCArray assets_names, DCObject meta) throws Exception
93+
public String get_embed_url(String url, String id, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws CloudKey_Exception
2994
{
30-
DCObject args = DCObject.create().push("url", url);
31-
if (assets_names != null && assets_names.size() > 0)
32-
{
33-
args.push("assets_names", assets_names);
34-
}
35-
if (meta != null && meta.size() > 0)
36-
{
37-
args.push("meta", meta);
38-
}
39-
DCObject result = this.call("media.create", args);
40-
return result.pull("id");
95+
String _url = url + "/embed/" + this.user_id + "/" + id;
96+
return CloudKey_Helpers.sign_url(_url, this.api_key, seclevel, asnum, ip, useragent, countries, referers, expires);
4197
}
4298

43-
public void mediaDelete(String id) throws Exception
44-
{
45-
this.call("media.delete", DCObject.create().push("id", id));
46-
}
47-
48-
public String upload() throws Exception
99+
public String get_stream_url(String id) throws CloudKey_Exception
49100
{
50-
DCObject result = upload(false, false, "");
51-
return result.pull("url");
101+
return this.get_stream_url(CLOUDKEY_API_URL, id, "mp4_h264_aac", CloudKey.CLOUDKEY_SECLEVEL_NONE, "", "", "", null, null, 0, "", false);
52102
}
53103

54-
public DCObject upload(Boolean status, Boolean jsonp_cb, String target) throws Exception
104+
public String get_stream_url(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 CloudKey_Exception
55105
{
56-
DCObject args = DCObject.create();
57-
if (status)
106+
if (extension == "")
58107
{
59-
args.push("status", true);
108+
String[] parts = asset_name.split("\\_");
109+
extension = (parts[0] != asset_name) ? parts[0] : extension;
60110
}
61-
if (jsonp_cb)
111+
if (asset_name.length() >= 15 && asset_name.substring(0, 15) == "jpeg_thumbnail_")
62112
{
63-
args.push("jsonp_cb", "?");
113+
return CLOUDKEY_STATIC_URL + this.user_id + "/" + id + "/" + asset_name + "." + extension;
64114
}
65-
if (target != "")
115+
else
66116
{
67-
args.push("target", target);
117+
String _url = this.cdn_url + "/route/" + this.user_id + "/" + id + "/" + asset_name + ((extension != "") ? "." + extension : "");
118+
return CloudKey_Helpers.sign_url(_url, this.api_key, seclevel, asnum, ip, useragent, countries, referers, expires) + (download ? "&throttle=0&helper=0&cache=0" : "");
68119
}
69-
return (DCObject) this.call("file.upload", args);
70120
}
71121
}

src/com/dmcloud/CloudKey_LL.java

Lines changed: 0 additions & 122 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.dmcloud;
2+
3+
public class CloudKey_Media extends CloudKey
4+
{
5+
public CloudKey_Media(String _user_id, String _api_key) throws Exception
6+
{
7+
super(_user_id, _api_key, CLOUDKEY_API_URL, CLOUDKEY_CDN_URL, "");
8+
}
9+
10+
public CloudKey_Media(String _user_id, String _api_key, String _base_url, String _cdn_url, String _proxy) throws Exception
11+
{
12+
super(_user_id, _api_key, _base_url, _cdn_url, _proxy);
13+
}
14+
15+
public String create() throws Exception
16+
{
17+
return create("", null, null);
18+
}
19+
20+
public String create(String url) throws Exception
21+
{
22+
return create(url, null, null);
23+
}
24+
25+
public String create(String url, DCArray assets_names, DCObject meta) throws Exception
26+
{
27+
DCObject args = DCObject.create().push("url", url);
28+
if (assets_names != null && assets_names.size() > 0)
29+
{
30+
args.push("assets_names", assets_names);
31+
}
32+
if (meta != null && meta.size() > 0)
33+
{
34+
args.push("meta", meta);
35+
}
36+
DCObject result = this.call("media.create", args);
37+
return result.pull("id");
38+
}
39+
40+
public void delete(String id) throws Exception
41+
{
42+
this.call("media.delete", DCObject.create().push("id", id));
43+
}
44+
45+
public String upload() throws Exception
46+
{
47+
DCObject result = upload(false, false, "");
48+
return result.pull("url");
49+
}
50+
51+
public DCObject upload(Boolean status, Boolean jsonp_cb, String target) throws Exception
52+
{
53+
DCObject args = DCObject.create();
54+
if (status)
55+
{
56+
args.push("status", true);
57+
}
58+
if (jsonp_cb)
59+
{
60+
args.push("jsonp_cb", "?");
61+
}
62+
if (target != "")
63+
{
64+
args.push("target", target);
65+
}
66+
return (DCObject) this.call("file.upload", args);
67+
}
68+
}

0 commit comments

Comments
 (0)