-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from docspell/fix/duplicates
Fix/duplicates
- Loading branch information
Showing
9 changed files
with
209 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
app/src/main/java/org/docspell/docspellshare/http/Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
152
app/src/main/java/org/docspell/docspellshare/http/HttpRequest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.