Search in sources :

Example 1 with FuelError

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);
        }
    });
}
Also used : Request(com.github.kittinunf.fuel.core.Request) Function2(kotlin.jvm.functions.Function2) URL(java.net.URL) Response(com.github.kittinunf.fuel.core.Response) FuelError(com.github.kittinunf.fuel.core.FuelError) UploadItem(com.thecoolguy.rumaan.fileio.data.models.UploadItem) File(java.io.File) Pair(kotlin.Pair)

Aggregations

FuelError (com.github.kittinunf.fuel.core.FuelError)1 Request (com.github.kittinunf.fuel.core.Request)1 Response (com.github.kittinunf.fuel.core.Response)1 UploadItem (com.thecoolguy.rumaan.fileio.data.models.UploadItem)1 File (java.io.File)1 URL (java.net.URL)1 Pair (kotlin.Pair)1 Function2 (kotlin.jvm.functions.Function2)1