From 4f402587eeec396d8664463bc51f3e913fb38feb Mon Sep 17 00:00:00 2001 From: Ezerous Date: Sat, 21 Jan 2017 20:00:58 +0200 Subject: [PATCH] Improved DownloadService --- .../mthmmy/services/DownloadService.java | 53 +++++++++++++++---- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java b/app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java index 97d05b53..53bcb8c9 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java +++ b/app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java @@ -4,6 +4,7 @@ import android.app.IntentService; import android.content.Context; import android.content.Intent; import android.os.Environment; +import android.provider.MediaStore; import java.io.File; import java.io.FileNotFoundException; @@ -66,18 +67,52 @@ public class DownloadService extends IntentService { Request request = new Request.Builder().url(downloadLink).build(); Response response = client.newCall(request).execute(); - String filename = response.headers("Content-Disposition").toString().split("\"")[1]; + String contentType = response.headers("Content-Type").toString(); //check if link provides a binary file + if(contentType.equals("[application/octet-stream]")) + { + String fileName = response.headers("Content-Disposition").toString().split("\"")[1]; + + File dirPath = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "mthmmy"); + if(!dirPath.isDirectory()) + { + if(dirPath.mkdirs()) + Report.i(TAG, "mTHMMY's directory created successfully!"); + else + Report.e(TAG, "Couldn't create mTHMMY's directory..."); + } + + + String nameFormat; + String[] tokens = fileName.split("\\.(?=[^\\.]+$)"); + + if(tokens.length!=2) + { + Report.w(TAG, "Error getting file extension"); + nameFormat = fileName + "(%d)"; + } + else + nameFormat = tokens[0] + "(%d)." + tokens[1]; - File dirPath = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "mthmmy"); - if(!dirPath.isDirectory()) - dirPath.mkdirs(); - File file = new File(dirPath, filename); + File file = new File(dirPath, fileName); - sink = Okio.buffer(Okio.sink(file)); - sink.writeAll(response.body().source()); - sink.flush(); - Report.i(TAG, "Download OK!"); + for (int i = 1;;i++) { + if (!file.exists()) { + break; + } + + file = new File(dirPath, String.format(nameFormat, i)); + } + Report.v(TAG, "Start saving file " + file.getName()); + + + sink = Okio.buffer(Okio.sink(file)); + sink.writeAll(response.body().source()); + sink.flush(); + Report.i(TAG, "Download OK!"); + } + else + Report.e(TAG, "Response not a binary!"); } catch (FileNotFoundException e){ Report.e(TAG, "FileNotFound", e);