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 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 fileUpload() throws Exception { DCObject result = fileUpload(false, "", ""); return result.pull("url"); }