From e26a25e72fc4f1ec82a8edb8b7b7f12c7a9e2c8a Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sat, 7 Nov 2020 15:57:10 +0900 Subject: [PATCH] Fix Download request exceeding 10kb by creating temp file --- .../instagrabber/utils/DownloadUtils.java | 19 ++++++++++++-- .../instagrabber/workers/DownloadWorker.java | 25 ++++++++++++++++--- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java b/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java index 9a8241f9..5192d5a7 100644 --- a/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java @@ -6,6 +6,7 @@ import android.content.Context; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.os.Environment; +import android.util.Log; import android.webkit.MimeTypeMap; import android.widget.Toast; @@ -23,7 +24,10 @@ import androidx.work.WorkRequest; import com.google.gson.Gson; +import java.io.BufferedWriter; import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; @@ -43,6 +47,8 @@ import static awais.instagrabber.utils.Constants.FOLDER_PATH; import static awais.instagrabber.utils.Constants.FOLDER_SAVE_TO; public final class DownloadUtils { + private static final String TAG = "DownloadUtils"; + public static final String WRITE_PERMISSION = Manifest.permission.WRITE_EXTERNAL_STORAGE; public static final String[] PERMS = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}; @@ -311,11 +317,20 @@ public final class DownloadUtils { final DownloadWorker.DownloadRequest request = DownloadWorker.DownloadRequest.builder() .setUrlToFilePathMap(urlFilePathMap) .build(); + final String requestJson = new Gson().toJson(request); + final File tempFile = getTempFile(); + try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) { + writer.write(requestJson); + } catch (IOException e) { + Log.e(TAG, "download: Error writing request to file", e); + //noinspection ResultOfMethodCallIgnored + tempFile.delete(); + return; + } final WorkRequest downloadWorkRequest = new OneTimeWorkRequest.Builder(DownloadWorker.class) .setInputData( new Data.Builder() - .putString(DownloadWorker.KEY_DOWNLOAD_REQUEST_JSON, - new Gson().toJson(request)) + .putString(DownloadWorker.KEY_DOWNLOAD_REQUEST_JSON, tempFile.getAbsolutePath()) .build() ) .setConstraints(constraints) diff --git a/app/src/main/java/awais/instagrabber/workers/DownloadWorker.java b/app/src/main/java/awais/instagrabber/workers/DownloadWorker.java index 79e4793e..1468efe8 100644 --- a/app/src/main/java/awais/instagrabber/workers/DownloadWorker.java +++ b/app/src/main/java/awais/instagrabber/workers/DownloadWorker.java @@ -41,6 +41,7 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Scanner; import java.util.Set; import java.util.concurrent.ExecutionException; @@ -59,10 +60,10 @@ import static awais.instagrabber.utils.Utils.logCollector; public class DownloadWorker extends Worker { private static final String TAG = "DownloadWorker"; - public static final String PROGRESS = "PROGRESS"; - public static final String URL = "URL"; private static final String DOWNLOAD_GROUP = "DOWNLOAD_GROUP"; + public static final String PROGRESS = "PROGRESS"; + public static final String URL = "URL"; public static final String KEY_DOWNLOAD_REQUEST_JSON = "download_request_json"; public static final int DOWNLOAD_NOTIFICATION_INTENT_REQUEST_CODE = 2020; public static final int DELETE_IMAGE_REQUEST_CODE = 2030; @@ -77,7 +78,21 @@ public class DownloadWorker extends Worker { @NonNull @Override public Result doWork() { - final String downloadRequestString = getInputData().getString(KEY_DOWNLOAD_REQUEST_JSON); + final String downloadRequestFilePath = getInputData().getString(KEY_DOWNLOAD_REQUEST_JSON); + if (TextUtils.isEmpty(downloadRequestFilePath)) { + return Result.failure(new Data.Builder() + .putString("error", "downloadRequest is empty or null") + .build()); + } + final String downloadRequestString; + final File requestFile = new File(downloadRequestFilePath); + try (Scanner scanner = new Scanner(requestFile)) { + downloadRequestString = scanner.useDelimiter("\\A").next(); + } catch (Exception e) { + return Result.failure(new Data.Builder() + .putString("error", e.getLocalizedMessage()) + .build()); + } if (TextUtils.isEmpty(downloadRequestString)) { return Result.failure(new Data.Builder() .putString("error", "downloadRequest is empty or null") @@ -100,6 +115,10 @@ public class DownloadWorker extends Worker { final Map urlToFilePathMap = downloadRequest.getUrlToFilePathMap(); download(urlToFilePathMap); new Handler(Looper.getMainLooper()).postDelayed(() -> showSummary(urlToFilePathMap), 500); + final boolean deleted = requestFile.delete(); + if (!deleted) { + Log.w(TAG, "doWork: requestFile not deleted!"); + } return Result.success(); }