private void uploadLink( @Nullable String title, @Nullable String description, @NonNull String url) { LogUtil.v(TAG, "Received URL to upload"); ApiClient client = new ApiClient(Endpoints.UPLOAD.getUrl(), ApiClient.HttpRequest.POST); FormEncodingBuilder builder = new FormEncodingBuilder().add("image", url).add("type", "URL"); if (!TextUtils.isEmpty(title)) { builder.add("title", title); } if (!TextUtils.isEmpty(description)) { builder.add("description", description); } RequestBody body = builder.build(); try { if (mNotificationId <= 0) mNotificationId = url.hashCode(); onUploadStarted(); JSONObject json = client.doWork(body); int status = json.getInt(ApiClient.KEY_STATUS); if (status == ApiClient.STATUS_OK) { handleResponse(json.getJSONObject(ApiClient.KEY_DATA)); } else { onUploadFailed(title, description, url, false); } } catch (IOException | JSONException e) { LogUtil.e(TAG, "Error uploading url", e); onUploadFailed(title, description, url, false); } }
private void uploadPhoto( @Nullable String title, @Nullable String description, @NonNull String filePath) { final File file = new File(filePath); if (!FileUtil.isFileValid(file)) { LogUtil.w(TAG, "File is invalid, bailing"); return; } LogUtil.v(TAG, "Received File to upload"); ApiClient client = new ApiClient(Endpoints.UPLOAD.getUrl(), ApiClient.HttpRequest.POST); MediaType type; if (file.getAbsolutePath().endsWith("png")) { type = MediaType.parse("image/png"); } else if (file.getAbsolutePath().endsWith("gif")) { type = MediaType.parse("image/gif"); } else { type = MediaType.parse("image/jpeg"); } MultipartBuilder builder = new MultipartBuilder() .type(MultipartBuilder.FORM) .addPart( Headers.of("Content-Disposition", "form-data; name=\"image\""), new ProgressRequestBody(file, type, this)); builder.addPart( Headers.of("Content-Disposition", "form-data; name=\"type\""), RequestBody.create(null, "file")); if (!TextUtils.isEmpty(title)) { builder.addPart( Headers.of("Content-Disposition", "form-data; name=\"title\""), RequestBody.create(null, title)); } if (!TextUtils.isEmpty(description)) { builder.addPart( Headers.of("Content-Disposition", "form-data; name=\"description\""), RequestBody.create(null, description)); } RequestBody body = builder.build(); try { if (mNotificationId <= 0) mNotificationId = file.hashCode(); onUploadStarted(); JSONObject json = client.doWork(body); int status = json.getInt(ApiClient.KEY_STATUS); if (status == ApiClient.STATUS_OK) { handleResponse(json.getJSONObject(ApiClient.KEY_DATA)); } else { onUploadFailed(title, description, filePath, true); } } catch (IOException | JSONException e) { LogUtil.e(TAG, "Error uploading file", e); onUploadFailed(title, description, filePath, true); } }