From c96312b2517d8d37564453c157e0cd3cb2c82bb9 Mon Sep 17 00:00:00 2001 From: nico202 Date: Thu, 24 May 2018 18:20:00 +0200 Subject: [PATCH] change library path, basic http auth support --- .../music_cyclon/service/BeetsFetcher.java | 7 +- .../music_cyclon/service/DownloadTask.java | 66 ++++++++++++------- .../music_cyclon/service/LibraryService.java | 14 +++- app/src/main/res/values/default_config.xml | 2 + app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/preferences.xml | 21 ++++++ 6 files changed, 83 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/max/music_cyclon/service/BeetsFetcher.java b/app/src/main/java/max/music_cyclon/service/BeetsFetcher.java index d044427..7a04846 100644 --- a/app/src/main/java/max/music_cyclon/service/BeetsFetcher.java +++ b/app/src/main/java/max/music_cyclon/service/BeetsFetcher.java @@ -29,7 +29,8 @@ public class BeetsFetcher { this.resources = resources; } - public List fetch(SynchronizeConfig config) throws IOException { + public List fetch(SynchronizeConfig config, + String username, String password) throws IOException { StringBuilder get; if (config.isAlbum(resources)) { @@ -46,8 +47,11 @@ public class BeetsFetcher { get.append("?expand"); OkHttpClient client = new OkHttpClient(); + String auth = okhttp3.Credentials.basic(username != null ? username : "", + password != null ? password : ""); Request request = new Request.Builder() .url(address + get) + .header("Authorization", auth) .build(); Response response = client.newCall(request).execute(); @@ -67,7 +71,6 @@ public class BeetsFetcher { private List parseJson(InputStream stream, int size, boolean isAlbums) throws IOException { JsonReader reader = new JsonReader(new BufferedReader(new InputStreamReader(stream, "UTF-8"))); - List items = new ArrayList<>(); List> albums = new ArrayList<>(); diff --git a/app/src/main/java/max/music_cyclon/service/DownloadTask.java b/app/src/main/java/max/music_cyclon/service/DownloadTask.java index 70905f7..0745429 100644 --- a/app/src/main/java/max/music_cyclon/service/DownloadTask.java +++ b/app/src/main/java/max/music_cyclon/service/DownloadTask.java @@ -23,26 +23,38 @@ public class DownloadTask implements Runnable { private final SynchronizeConfig config; private final String url; private final String itemPath; - + private final String libraryPath; + private final String username; + private final String password; private final FileTracker tracker; private final ProgressUpdater progressUpdater; private CountDownLatch itemsLeftLatch; public static final OkHttpClient CLIENT = new OkHttpClient(); - public DownloadTask(SynchronizeConfig config, String url, String itemPath, - FileTracker tracker, ProgressUpdater progressUpdater) { + public DownloadTask(SynchronizeConfig config, String url, + String libraryPath, String itemPath, + FileTracker tracker, ProgressUpdater progressUpdater, + String username, String password + ) { this.config = config; this.url = url; this.itemPath = itemPath; + this.libraryPath = libraryPath; + + this.username = username; + this.password = password; this.tracker = tracker; this.progressUpdater = progressUpdater; } private InputStream prepareConnection() throws IOException { + String auth = okhttp3.Credentials.basic(username != null ? username : "", + password != null ? password : ""); Request request = new Request.Builder() .url(url) + .header("Authorization", auth) .build(); Response response = CLIENT.newCall(request).execute(); @@ -61,35 +73,41 @@ public class DownloadTask implements Runnable { @Override public void run() { - File root = new File(Environment.getExternalStorageDirectory(), "library"); + File root = new File(Environment.getExternalStorageDirectory(), + libraryPath); + if (itemPath != null) { + try { + File target = new File(root, itemPath); + if (! target.exists()) { + Adler32 checksum = new Adler32(); - try { - File target = new File(root, itemPath); - Adler32 checksum = new Adler32(); + InputStream input = prepareConnection(); - InputStream input = prepareConnection(); + if (input != null) { + Log.d("DOWNLOAD", "Writing file: " + target); + FileOutputStream output = FileUtils.openOutputStream(target); - if (input != null) { + byte[] buffer = new byte[4 * 1024]; + int n; + while (-1 != (n = input.read(buffer))) { + output.write(buffer, 0, n); + checksum.update(buffer, 0, n); + } - FileOutputStream output = FileUtils.openOutputStream(target); + output.flush(); + output.close(); + input.close(); + } - byte[] buffer = new byte[4 * 1024]; - int n; - while (-1 != (n = input.read(buffer))) { - output.write(buffer, 0, n); - checksum.update(buffer, 0, n); + tracker.track(config, target, checksum.getValue()); } - - output.flush(); - output.close(); - input.close(); + } catch (IOException e) { + Log.e("DOWNLOAD", "Failed to download", e); } - - tracker.track(config, target, checksum.getValue()); - } catch (IOException e) { - Log.e("DOWNLOAD", "Failed to download", e); + Log.i("DOWNLOAD", "Success"); + } else { + Log.e("DOWNLOAD", "Missing download path, FAILED!"); } - progressUpdater.increment(); itemsLeftLatch.countDown(); } diff --git a/app/src/main/java/max/music_cyclon/service/LibraryService.java b/app/src/main/java/max/music_cyclon/service/LibraryService.java index 58c291e..2ac4e6c 100644 --- a/app/src/main/java/max/music_cyclon/service/LibraryService.java +++ b/app/src/main/java/max/music_cyclon/service/LibraryService.java @@ -136,7 +136,11 @@ public class LibraryService extends IntentService { List items; try { updater.showOngoingMessage("Fetching music information for %s", config.getName()); - items = fetcher.fetch(config); + items = fetcher.fetch(config, + globalSettings.getString("username", null), + globalSettings.getString("password", null)); + Log.d("LISTOUT", "Length: " + items.size()); + } catch (IOException e) { Log.wtf("WTF", e); updater.showMessage("Remote not available"); @@ -149,7 +153,12 @@ public class LibraryService extends IntentService { for (Item item : items) { String url = address + "/item/" + item.getID() + "/file"; - tasks.add(new DownloadTask(config, url, item.getPath(), tracker, updater)); + tasks.add(new DownloadTask(config, url, + globalSettings.getString("library_path", "library"), + config.getName() + item.getPath(), tracker, updater, + globalSettings.getString("username", null), + globalSettings.getString("password", null) + )); } } @@ -176,7 +185,6 @@ public class LibraryService extends IntentService { poweramp.putExtra(PowerampAPI.Scanner.EXTRA_FULL_RESCAN, true); startService(poweramp); - finished(); } diff --git a/app/src/main/res/values/default_config.xml b/app/src/main/res/values/default_config.xml index a3c2da4..1b2d867 100644 --- a/app/src/main/res/values/default_config.xml +++ b/app/src/main/res/values/default_config.xml @@ -2,6 +2,8 @@ http://localhost:8337 2 + + 10 true diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 338dd10..dab879d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -8,6 +8,8 @@ Synchronizing Already synchronizing! Music Updated! + + library Default Rename diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 51cf3af..04385ad 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -14,5 +14,26 @@ android:key="threads" android:summary="Number of threads to use for downloading" android:title="Threads" /> + + + \ No newline at end of file