Exemplo n.º 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();
      }
    }
  }
Exemplo n.º 2
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");
 }
Exemplo n.º 3
0
 public String fileUpload() throws Exception {
   DCObject result = fileUpload(false, "", "");
   return result.pull("url");
 }