use of com.github.kittinunf.fuel.core.FuelError in project file.io-app by rumaan.
the class UploadRepository method uploadFile.
void uploadFile(final FileModel fileModel, final Upload resultCallback) {
final File file = fileModel.getFile();
// create an upload item from the file model
final UploadItem uploadItem = new UploadItem();
uploadItem.setFileName(file.getName());
// set up query parameters
// NOTE: here getDaysToExpire() actually returns the number of weeks selected by the user.
String link = "https://file.io/?expires=" + fileModel.getDaysToExpire();
Log.i(TAG, "Request Link: " + link);
// TODO: change the file object to InputStream
Fuel.upload(link).source(new Function2<Request, URL, File>() {
@Override
public File invoke(Request request, URL url) {
return file;
}
}).name(new Function0<String>() {
@Override
public String invoke() {
return "file";
}
}).progress(new Function2<Long, Long, Unit>() {
@Override
public Unit invoke(Long bytesUploaded, Long totalBytes) {
int p = (int) (((float) bytesUploaded / totalBytes) * 100);
resultCallback.progress(p);
return null;
}
}).responseString(new Handler<String>() {
@Override
public void success(Request request, Response response, String res) {
// Parse the JSON from the response.
Pair<String, Integer> parsedResults = getParsedResults(res);
if (parsedResults != null) {
// Set the URL
uploadItem.setUrl(parsedResults.getFirst());
// Set the Days after which the link will expire
uploadItem.setDaysToExpire(parsedResults.getSecond());
// Set the upload time
uploadItem.setDate(getCurrentDate());
// Insert the Object intro the Database
insert(uploadItem);
// Pass out the received upload link to listeners
resultCallback.onUpload(parsedResults.getFirst());
} else {
failure(request, response, new FuelError(new NullPointerException("Data formed from JSON maybe was null."), null, response));
}
}
@Override
public void failure(Request request, Response response, FuelError fuelError) {
Crashlytics.logException(fuelError);
Log.e(TAG, "failure: " + fuelError.getMessage(), fuelError.getException());
resultCallback.onError(fuelError);
}
});
}
Aggregations