change library path, basic http auth support
parent
a6d270b7a4
commit
c96312b251
|
@ -29,7 +29,8 @@ public class BeetsFetcher {
|
||||||
this.resources = resources;
|
this.resources = resources;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Item> fetch(SynchronizeConfig config) throws IOException {
|
public List<Item> fetch(SynchronizeConfig config,
|
||||||
|
String username, String password) throws IOException {
|
||||||
StringBuilder get;
|
StringBuilder get;
|
||||||
|
|
||||||
if (config.isAlbum(resources)) {
|
if (config.isAlbum(resources)) {
|
||||||
|
@ -46,8 +47,11 @@ public class BeetsFetcher {
|
||||||
get.append("?expand");
|
get.append("?expand");
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
String auth = okhttp3.Credentials.basic(username != null ? username : "",
|
||||||
|
password != null ? password : "");
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(address + get)
|
.url(address + get)
|
||||||
|
.header("Authorization", auth)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = client.newCall(request).execute();
|
Response response = client.newCall(request).execute();
|
||||||
|
@ -67,7 +71,6 @@ public class BeetsFetcher {
|
||||||
|
|
||||||
private List<Item> parseJson(InputStream stream, int size, boolean isAlbums) throws IOException {
|
private List<Item> parseJson(InputStream stream, int size, boolean isAlbums) throws IOException {
|
||||||
JsonReader reader = new JsonReader(new BufferedReader(new InputStreamReader(stream, "UTF-8")));
|
JsonReader reader = new JsonReader(new BufferedReader(new InputStreamReader(stream, "UTF-8")));
|
||||||
|
|
||||||
List<Item> items = new ArrayList<>();
|
List<Item> items = new ArrayList<>();
|
||||||
List<ArrayList<Item>> albums = new ArrayList<>();
|
List<ArrayList<Item>> albums = new ArrayList<>();
|
||||||
|
|
||||||
|
|
|
@ -23,26 +23,38 @@ public class DownloadTask implements Runnable {
|
||||||
private final SynchronizeConfig config;
|
private final SynchronizeConfig config;
|
||||||
private final String url;
|
private final String url;
|
||||||
private final String itemPath;
|
private final String itemPath;
|
||||||
|
private final String libraryPath;
|
||||||
|
private final String username;
|
||||||
|
private final String password;
|
||||||
private final FileTracker tracker;
|
private final FileTracker tracker;
|
||||||
private final ProgressUpdater progressUpdater;
|
private final ProgressUpdater progressUpdater;
|
||||||
private CountDownLatch itemsLeftLatch;
|
private CountDownLatch itemsLeftLatch;
|
||||||
public static final OkHttpClient CLIENT = new OkHttpClient();
|
public static final OkHttpClient CLIENT = new OkHttpClient();
|
||||||
|
|
||||||
|
|
||||||
public DownloadTask(SynchronizeConfig config, String url, String itemPath,
|
public DownloadTask(SynchronizeConfig config, String url,
|
||||||
FileTracker tracker, ProgressUpdater progressUpdater) {
|
String libraryPath, String itemPath,
|
||||||
|
FileTracker tracker, ProgressUpdater progressUpdater,
|
||||||
|
String username, String password
|
||||||
|
) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.itemPath = itemPath;
|
this.itemPath = itemPath;
|
||||||
|
this.libraryPath = libraryPath;
|
||||||
|
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
|
||||||
this.tracker = tracker;
|
this.tracker = tracker;
|
||||||
this.progressUpdater = progressUpdater;
|
this.progressUpdater = progressUpdater;
|
||||||
}
|
}
|
||||||
|
|
||||||
private InputStream prepareConnection() throws IOException {
|
private InputStream prepareConnection() throws IOException {
|
||||||
|
String auth = okhttp3.Credentials.basic(username != null ? username : "",
|
||||||
|
password != null ? password : "");
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(url)
|
.url(url)
|
||||||
|
.header("Authorization", auth)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = CLIENT.newCall(request).execute();
|
Response response = CLIENT.newCall(request).execute();
|
||||||
|
@ -61,35 +73,41 @@ public class DownloadTask implements Runnable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
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 {
|
InputStream input = prepareConnection();
|
||||||
File target = new File(root, itemPath);
|
|
||||||
Adler32 checksum = new Adler32();
|
|
||||||
|
|
||||||
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];
|
tracker.track(config, target, checksum.getValue());
|
||||||
int n;
|
|
||||||
while (-1 != (n = input.read(buffer))) {
|
|
||||||
output.write(buffer, 0, n);
|
|
||||||
checksum.update(buffer, 0, n);
|
|
||||||
}
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
output.flush();
|
Log.e("DOWNLOAD", "Failed to download", e);
|
||||||
output.close();
|
|
||||||
input.close();
|
|
||||||
}
|
}
|
||||||
|
Log.i("DOWNLOAD", "Success");
|
||||||
tracker.track(config, target, checksum.getValue());
|
} else {
|
||||||
} catch (IOException e) {
|
Log.e("DOWNLOAD", "Missing download path, FAILED!");
|
||||||
Log.e("DOWNLOAD", "Failed to download", e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
progressUpdater.increment();
|
progressUpdater.increment();
|
||||||
itemsLeftLatch.countDown();
|
itemsLeftLatch.countDown();
|
||||||
}
|
}
|
||||||
|
|
|
@ -136,7 +136,11 @@ public class LibraryService extends IntentService {
|
||||||
List<Item> items;
|
List<Item> items;
|
||||||
try {
|
try {
|
||||||
updater.showOngoingMessage("Fetching music information for %s", config.getName());
|
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) {
|
} catch (IOException e) {
|
||||||
Log.wtf("WTF", e);
|
Log.wtf("WTF", e);
|
||||||
updater.showMessage("Remote not available");
|
updater.showMessage("Remote not available");
|
||||||
|
@ -149,7 +153,12 @@ public class LibraryService extends IntentService {
|
||||||
|
|
||||||
for (Item item : items) {
|
for (Item item : items) {
|
||||||
String url = address + "/item/" + item.getID() + "/file";
|
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);
|
poweramp.putExtra(PowerampAPI.Scanner.EXTRA_FULL_RESCAN, true);
|
||||||
startService(poweramp);
|
startService(poweramp);
|
||||||
|
|
||||||
|
|
||||||
finished();
|
finished();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
<resources>
|
<resources>
|
||||||
<string name="address">http://localhost:8337</string>
|
<string name="address">http://localhost:8337</string>
|
||||||
<integer name="threads">2</integer>
|
<integer name="threads">2</integer>
|
||||||
|
<string name="username"></string>
|
||||||
|
<string name="password"></string>
|
||||||
|
|
||||||
<integer name="size">10</integer>
|
<integer name="size">10</integer>
|
||||||
<bool name="random">true</bool>
|
<bool name="random">true</bool>
|
||||||
|
|
|
@ -8,6 +8,8 @@
|
||||||
<string name="synchronizing">Synchronizing</string>
|
<string name="synchronizing">Synchronizing</string>
|
||||||
<string name="already_synchronizing">Already synchronizing!</string>
|
<string name="already_synchronizing">Already synchronizing!</string>
|
||||||
<string name="music_updated">Music Updated!</string>
|
<string name="music_updated">Music Updated!</string>
|
||||||
|
|
||||||
|
<string name="library_path">library</string>
|
||||||
<string name="default_config_name">Default</string>
|
<string name="default_config_name">Default</string>
|
||||||
<string name="rename">Rename</string>
|
<string name="rename">Rename</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
|
@ -14,5 +14,26 @@
|
||||||
android:key="threads"
|
android:key="threads"
|
||||||
android:summary="Number of threads to use for downloading"
|
android:summary="Number of threads to use for downloading"
|
||||||
android:title="Threads" />
|
android:title="Threads" />
|
||||||
|
<EditTextPreference
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:inputType="number"
|
||||||
|
android:key="library_path"
|
||||||
|
android:summary="Where to save the music"
|
||||||
|
android:title="Library Path" />
|
||||||
|
<EditTextPreference
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:inputType="number"
|
||||||
|
android:key="username"
|
||||||
|
android:summary="HTTP auth username, if required!"
|
||||||
|
android:title="HTTP username" />
|
||||||
|
<EditTextPreference
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:inputType="number"
|
||||||
|
android:key="password"
|
||||||
|
android:summary="HTTP auth password, if required!"
|
||||||
|
android:title="HTTP Password" />
|
||||||
|
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
Loading…
Reference in New Issue