use of com.thebluealliance.imgur.responses.UploadResponse in project the-blue-alliance-android by the-blue-alliance.
the class ImgurSuggestionService method onHandleIntent.
@Override
protected void onHandleIntent(Intent intent) {
TbaLogger.d("IMGUR SERVICE START");
String filepath = intent.getStringExtra(EXTRA_FILEPATH);
String title = intent.getStringExtra(EXTRA_TITLE);
String description = intent.getStringExtra(EXTRA_DESCRIPTION);
String teamKey = intent.getStringExtra(EXTRA_TEAMKEY);
int year = intent.getIntExtra(EXTRA_YEAR, 0);
ImgurUploadNotification notification = new ImgurUploadNotification(getApplicationContext());
notification.onUploadStarting();
boolean successful = true;
// Get a wake lock so any long uploads will not be interrupted
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
wakeLock.acquire();
// TODO should we catch each exception individually and handle them better (e.g. retyr?)
try {
String authToken = getAuthHeader();
File file = new File(filepath);
RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), file);
RequestBody titlePart = RequestBody.create(MediaType.parse("text/plain"), title);
RequestBody descPart = RequestBody.create(MediaType.parse("text/plain"), description);
Response<UploadResponse> response = mImgurApi.uploadImage(authToken, titlePart, descPart, body).execute();
if (response != null && response.isSuccessful()) {
UploadResponse uploadResponse = response.body();
TbaLogger.d("Uploaded imgur image: " + uploadResponse.data.link);
String link = uploadResponse.data.link;
String deletehash = uploadResponse.data.deletehash;
TbaLogger.d("Imgur link: " + link);
// Do suggestion
String authHeader = mGceAuthController.getAuthHeader();
ModelsMobileApiMessagesMediaSuggestionMessage message = buildSuggestionMessage(teamKey, year, link, deletehash);
Response<ModelsMobileApiMessagesBaseResponse> suggestionResponse = mTeamMediaApi.suggestion(authHeader, message).execute();
if (suggestionResponse != null && suggestionResponse.isSuccessful()) {
// Yay, everything worked!
} else {
// Crap
// TODO handle this
successful = false;
}
} else {
TbaLogger.e("Error uploading imgur image\n" + response.code() + " " + response.message());
successful = false;
}
} catch (Exception e) {
// Something broke
successful = false;
e.printStackTrace();
} finally {
if (successful) {
notification.onUploadSuccess();
} else {
notification.onUploadFailure();
}
// Delete the temp cached image
new File(filepath).delete();
wakeLock.release();
}
}
Aggregations