change library path, basic http auth support

pull/4/head
nico202 2018-05-24 18:20:00 +02:00
parent a6d270b7a4
commit c96312b251
6 changed files with 83 additions and 29 deletions

View File

@ -29,7 +29,8 @@ public class BeetsFetcher {
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;
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<Item> parseJson(InputStream stream, int size, boolean isAlbums) throws IOException {
JsonReader reader = new JsonReader(new BufferedReader(new InputStreamReader(stream, "UTF-8")));
List<Item> items = new ArrayList<>();
List<ArrayList<Item>> albums = new ArrayList<>();

View File

@ -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();
}

View File

@ -136,7 +136,11 @@ public class LibraryService extends IntentService {
List<Item> 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();
}

View File

@ -2,6 +2,8 @@
<resources>
<string name="address">http://localhost:8337</string>
<integer name="threads">2</integer>
<string name="username"></string>
<string name="password"></string>
<integer name="size">10</integer>
<bool name="random">true</bool>

View File

@ -8,6 +8,8 @@
<string name="synchronizing">Synchronizing</string>
<string name="already_synchronizing">Already synchronizing!</string>
<string name="music_updated">Music Updated!</string>
<string name="library_path">library</string>
<string name="default_config_name">Default</string>
<string name="rename">Rename</string>
</resources>

View File

@ -14,5 +14,26 @@
android:key="threads"
android:summary="Number of threads to use for downloading"
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>