Skip to content

Instantly share code, notes, and snippets.

@acappelli
Created July 11, 2017 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acappelli/5fd7b271fe945d09e60adbf9f6606d04 to your computer and use it in GitHub Desktop.
Save acappelli/5fd7b271fe945d09e60adbf9f6606d04 to your computer and use it in GitHub Desktop.
/*
* Kontalk Android client
* Copyright (C) 2017 Kontalk Devteam <devteam@kontalk.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kontalk.upload;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import android.content.Context;
import android.net.Uri;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.internal.Util;
import okio.BufferedSink;
import okio.Okio;
import okio.Source;
import org.kontalk.client.ClientHTTPConnection;
import org.kontalk.service.ProgressListener;
import org.kontalk.util.CountingRequestBody;
import org.kontalk.util.Preferences;
/**
* Simple HTTP upload via PUT.
*
* @author Daniele Ricci
* @author Andrea Cappelli
*/
public class HTPPFileUploadConnection implements UploadConnection {
private final Context mContext;
private final String mUrl;
private HttpsURLConnection currentRequest;
private OkHttpClient client;
private Call call;
private Request request;
private final static int CONNECT_TIMEOUT = 15000;
private final static int READ_TIMEOUT = 40000;
/**
* Minimum delay for progress notification updates in milliseconds.
*/
private static final int PROGRESS_PUBLISH_DELAY = 1000;
public HTPPFileUploadConnection(Context context, String url) {
mContext = context;
mUrl = url;
}
@Override
public void abort() {
try {
//currentRequest.disconnect();
call.cancel();
}
catch (Exception ignored) {
}
}
@Override
public String upload(final Uri uri, final long length, String mime, boolean encrypt, String[] to, final ProgressListener listener) throws IOException {
try {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
boolean acceptAnyCertificate = Preferences.getAcceptAnyCertificate(mContext);
builder.sslSocketFactory(ClientHTTPConnection.setupSSLSocketFactory(mContext,
null, null, acceptAnyCertificate));
if (acceptAnyCertificate) {
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
}
client = builder.connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
.readTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS)
.build();
final MediaType MEDIA_TYPE = MediaType
.parse(mime != null ? mime : "application/octet-stream");
RequestBody requestBody = new RequestBody() {
@Override
public long contentLength() throws IOException {
return length;
}
@Override
public MediaType contentType() {
return MEDIA_TYPE;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
InputStream inMessage = mContext.getContentResolver().openInputStream(uri);
Source source = null;
try {
source = Okio.source(inMessage);
sink.writeAll(source);
}
finally {
Util.closeQuietly(source);
}
}
};
CountingRequestBody countingRequestBody = new CountingRequestBody(requestBody,
new CountingRequestBody.Listener() {
@Override
public void onRequestProgress(long bytesWritten, long contentLength) {
listener.progress(HTPPFileUploadConnection.this, bytesWritten);
}
});
Request request = new Request.Builder()
.url(mUrl)
.method("PUT", countingRequestBody)
.build();
call = client.newCall(request);
Response response = call.execute();
if (response.code() != 200) {
throw new IOException(response.code() + " " + response.message());
}
return null;
}
catch (Exception e) {
throw innerException("upload error", e);
}
}
private IOException innerException(String detail, Throwable cause) {
return new IOException(detail, cause);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment