Skip to content

Commit

Permalink
Merge pull request #67 from docspell/fix/duplicates
Browse files Browse the repository at this point in the history
Fix/duplicates
  • Loading branch information
eikek authored May 28, 2022
2 parents 3e2efd2 + 9836352 commit 7348ffe
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 197 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
compileSdkVersion 30
buildToolsVersion "30.0.2"

defaultConfig {
applicationId "org.docspell.docspellshare"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.docspell.docspellshare.R;
import org.docspell.docspellshare.data.Option;
import org.docspell.docspellshare.data.UrlItem;
import org.docspell.docspellshare.http.HttpRequest;
import org.docspell.docspellshare.http.UploadRequest;
import org.docspell.docspellshare.http.ProgressListener;
import org.docspell.docspellshare.http.UploadManager;
import org.docspell.docspellshare.util.DataStore;
Expand Down Expand Up @@ -129,7 +129,7 @@ private List<Uri> findFiles(Intent intent) {
}

void handleFiles(List<Uri> uris, String url) {
HttpRequest.Builder req = HttpRequest.newBuilder().setUrl(url);
UploadRequest.Builder req = UploadRequest.newBuilder().setUrl(url);
ContentResolver resolver = getContentResolver();
for (Uri uri : uris) {
req.addFile(resolver, uri, parseFilenameFromUri(uri));
Expand Down
81 changes: 81 additions & 0 deletions app/src/main/java/org/docspell/docspellshare/http/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.docspell.docspellshare.http;

import androidx.annotation.NonNull;

import org.docspell.docspellshare.data.Option;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import okhttp3.ConnectionSpec;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okio.BufferedSink;
import okio.Okio;
import okio.Source;

public final class Client {
private static final int CHUNK_SIZE = 16 * 1024;
private static final OkHttpClient client =
new OkHttpClient.Builder()
.connectionSpecs(
Arrays.asList(
ConnectionSpec.MODERN_TLS,
ConnectionSpec.COMPATIBLE_TLS,
ConnectionSpec.CLEARTEXT))
.readTimeout(5, TimeUnit.MINUTES)
.writeTimeout(5, TimeUnit.MINUTES)
.addInterceptor(new UserAgentInterceptor())
.socketFactory(new RestrictedSocketFactory(CHUNK_SIZE))
.followRedirects(true)
.followSslRedirects(true)
.build();

public static OkHttpClient get() {
return client;
}

public interface DataPart {
InputStream getData() throws IOException;

String getName();

Option<String> getType();

/** Return -1, if unknown. */
long getTotalSize();
}

static RequestBody createPartBody(DataPart part, ProgressListener listener) {
final String octetStream = "application/octet-stream";
return new RequestBody() {
@Override
public MediaType contentType() {
final MediaType mt = MediaType.parse(part.getType().orElse(octetStream));
return mt != null ? mt : MediaType.get(octetStream);
}

@Override
public long contentLength() {
return part.getTotalSize();
}

@Override
public void writeTo(@NonNull BufferedSink sink) throws IOException {
try (InputStream in = part.getData();
Source source = Okio.source(in)) {
long total = 0;
long read;
while ((read = source.read(sink.getBuffer(), CHUNK_SIZE)) != -1) {
total += read;
sink.flush();
listener.onProgress(part.getName(), total, part.getTotalSize());
}
}
}
};
}
}
152 changes: 0 additions & 152 deletions app/src/main/java/org/docspell/docspellshare/http/HttpRequest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import android.os.Process;
import android.util.Log;

import org.json.JSONException;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -30,16 +33,16 @@ public void setProgress(ProgressListener listener) {
}
}

public void submit(HttpRequest request) {
public void submit(UploadRequest request) {
executorService.submit(new UploadWorker(request, progress.get()));
}

static class UploadWorker implements Runnable {

private final HttpRequest request;
private final UploadRequest request;
private final ProgressListener listener;

UploadWorker(HttpRequest request, ProgressListener listener) {
UploadWorker(UploadRequest request, ProgressListener listener) {
this.request = request;
this.listener = listener;
}
Expand All @@ -55,7 +58,7 @@ public void run() {
} finally {
resp.close();
}
} catch (IOException e) {
} catch (IOException | JSONException e) {
Log.e("upload", "Error uploading!", e);
listener.onException(e);
}
Expand Down
Loading

0 comments on commit 7348ffe

Please sign in to comment.