|
1 | 1 | package com.dmcloud; |
2 | 2 |
|
3 | | -import java.util.ArrayList; |
4 | 3 | import java.util.Map; |
| 4 | +import org.codehaus.jackson.map.ObjectMapper; |
5 | 5 |
|
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; |
7 | 16 |
|
| 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 | + |
8 | 27 | public CloudKey(String _user_id, String _api_key) throws Exception |
9 | 28 | { |
10 | | - super(_user_id, _api_key, CLOUDKEY_API_URL, CLOUDKEY_CDN_URL, ""); |
| 29 | + this(_user_id, _api_key, CLOUDKEY_API_URL, CLOUDKEY_CDN_URL, ""); |
11 | 30 | } |
12 | 31 |
|
13 | 32 | public CloudKey(String _user_id, String _api_key, String _base_url, String _cdn_url, String _proxy) throws Exception |
14 | 33 | { |
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; |
16 | 43 | } |
17 | | - |
18 | | - public String mediaCreate() throws Exception |
| 44 | + |
| 45 | + public DCObject call(String call, DCObject args) throws Exception |
19 | 46 | { |
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")); |
21 | 86 | } |
22 | 87 |
|
23 | | - public String mediaCreate(String url) throws Exception |
| 88 | + public String get_embed_url(String id) throws CloudKey_Exception |
24 | 89 | { |
25 | | - return mediaCreate(url, null, null); |
| 90 | + return this.get_embed_url(CLOUDKEY_API_URL, id, CloudKey.CLOUDKEY_SECLEVEL_NONE, "", "", "", null, null, 0); |
26 | 91 | } |
27 | 92 |
|
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 |
29 | 94 | { |
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); |
41 | 97 | } |
42 | 98 |
|
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 |
49 | 100 | { |
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); |
52 | 102 | } |
53 | 103 |
|
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 |
55 | 105 | { |
56 | | - DCObject args = DCObject.create(); |
57 | | - if (status) |
| 106 | + if (extension == "") |
58 | 107 | { |
59 | | - args.push("status", true); |
| 108 | + String[] parts = asset_name.split("\\_"); |
| 109 | + extension = (parts[0] != asset_name) ? parts[0] : extension; |
60 | 110 | } |
61 | | - if (jsonp_cb) |
| 111 | + if (asset_name.length() >= 15 && asset_name.substring(0, 15) == "jpeg_thumbnail_") |
62 | 112 | { |
63 | | - args.push("jsonp_cb", "?"); |
| 113 | + return CLOUDKEY_STATIC_URL + this.user_id + "/" + id + "/" + asset_name + "." + extension; |
64 | 114 | } |
65 | | - if (target != "") |
| 115 | + else |
66 | 116 | { |
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" : ""); |
68 | 119 | } |
69 | | - return (DCObject) this.call("file.upload", args); |
70 | 120 | } |
71 | 121 | } |
0 commit comments