Example #1
0
  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();
      }
    }
  }
Example #2
0
 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);
 }
Example #3
0
 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");
 }
Example #4
0
 public String fileUpload() throws Exception {
   DCObject result = fileUpload(false, "", "");
   return result.pull("url");
 }
Example #5
0
 public void mediaDelete(String id) throws Exception {
   this.call("media.delete", DCObject.create().push("id", id));
 }