Updates upload

This commit is contained in:
stom79 2017-11-18 11:25:04 +01:00
parent 9584db81fd
commit f199446c70
2 changed files with 16 additions and 11 deletions

View File

@ -120,7 +120,6 @@ import fr.gouv.etalab.mastodon.asynctasks.RetrieveEmojiAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveSearchAccountsAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrieveSearchAccountsAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveSearchAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrieveSearchAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.UpdateDescriptionAttachmentAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.UpdateDescriptionAttachmentAsyncTask;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account; import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Attachment; import fr.gouv.etalab.mastodon.client.Entities.Attachment;
@ -130,6 +129,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Mention;
import fr.gouv.etalab.mastodon.client.Entities.Results; import fr.gouv.etalab.mastodon.client.Entities.Results;
import fr.gouv.etalab.mastodon.client.Entities.Status; import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.client.Entities.StoredStatus; import fr.gouv.etalab.mastodon.client.Entities.StoredStatus;
import fr.gouv.etalab.mastodon.client.HttpsConnection;
import fr.gouv.etalab.mastodon.drawers.CustomEmojiAdapter; import fr.gouv.etalab.mastodon.drawers.CustomEmojiAdapter;
import fr.gouv.etalab.mastodon.drawers.EmojisSearchAdapter; import fr.gouv.etalab.mastodon.drawers.EmojisSearchAdapter;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveEmojiInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveEmojiInterface;
@ -353,7 +353,7 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
toot_picture_container.setVisibility(View.VISIBLE); toot_picture_container.setVisibility(View.VISIBLE);
picture_scrollview.setVisibility(View.VISIBLE); picture_scrollview.setVisibility(View.VISIBLE);
toot_picture.setEnabled(false); toot_picture.setEnabled(false);
new API(getApplicationContext()).uploadMedia(bs, TootActivity.this); new HttpsConnection(getApplicationContext()).upload(bs, TootActivity.this);
} }
toot_content.setText(String.format("\n\nvia @%s\n\n%s\n\n", tootMention, urlMention)); toot_content.setText(String.format("\n\nvia @%s\n\n%s\n\n", tootMention, urlMention));
toot_space_left.setText(String.valueOf(toot_content.length())); toot_space_left.setText(String.valueOf(toot_content.length()));
@ -422,7 +422,7 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
InputStream bis = new ByteArrayInputStream(binaryData); InputStream bis = new ByteArrayInputStream(binaryData);
toot_picture_container.setVisibility(View.VISIBLE); toot_picture_container.setVisibility(View.VISIBLE);
toot_picture.setEnabled(false); toot_picture.setEnabled(false);
new API(getApplicationContext()).uploadMedia(bis, TootActivity.this); new HttpsConnection(getApplicationContext()).upload(bis, TootActivity.this);
f.write(binaryData); f.write(binaryData);
f.close(); f.close();
} catch (IOException e) { } catch (IOException e) {
@ -574,7 +574,7 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
String patternTag = "^(.|\\s)*(#([\\w-]{2,}))$"; String patternTag = "^(.|\\s)*(#([\\w-]{2,}))$";
final Pattern tPattern = Pattern.compile(patternTag); final Pattern tPattern = Pattern.compile(patternTag);
String patternEmoji = "^(.|\\s)*(:([\\w_]{1,}))$"; String patternEmoji = "^(.|\\s)*(:([\\w_]+))$";
final Pattern ePattern = Pattern.compile(patternEmoji); final Pattern ePattern = Pattern.compile(patternEmoji);
toot_cw_content.addTextChangedListener(new TextWatcher() { toot_cw_content.addTextChangedListener(new TextWatcher() {
@ -721,7 +721,7 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
toot_picture_container.setVisibility(View.VISIBLE); toot_picture_container.setVisibility(View.VISIBLE);
picture_scrollview.setVisibility(View.VISIBLE); picture_scrollview.setVisibility(View.VISIBLE);
toot_picture.setEnabled(false); toot_picture.setEnabled(false);
new API(getApplicationContext()).uploadMedia(inputStream, TootActivity.this); new HttpsConnection(getApplicationContext()).upload(inputStream, TootActivity.this);
count++; count++;
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
@ -750,7 +750,7 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
InputStream inputStream = getContentResolver().openInputStream(data.getData()); InputStream inputStream = getContentResolver().openInputStream(data.getData());
toot_picture_container.setVisibility(View.VISIBLE); toot_picture_container.setVisibility(View.VISIBLE);
toot_picture.setEnabled(false); toot_picture.setEnabled(false);
new API(getApplicationContext()).uploadMedia(inputStream, TootActivity.this); new HttpsConnection(getApplicationContext()).upload(inputStream, TootActivity.this);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show();
toot_picture.setEnabled(true); toot_picture.setEnabled(true);

View File

@ -14,6 +14,7 @@ package fr.gouv.etalab.mastodon.client;
* You should have received a copy of the GNU General Public License along with Mastalab; if not, * You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import org.json.JSONObject; import org.json.JSONObject;
@ -45,6 +46,8 @@ import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import fr.gouv.etalab.mastodon.client.Entities.Attachment; import fr.gouv.etalab.mastodon.client.Entities.Attachment;
import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAttachmentInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAttachmentInterface;
@ -65,7 +68,7 @@ public class HttpsConnection {
public HttpsConnection(){} public HttpsConnection(){}
HttpsConnection(Context context){ public HttpsConnection(Context context){
this.context = context; this.context = context;
} }
@ -164,13 +167,15 @@ public class HttpsConnection {
} }
public void upload(final String urlConnection, final int timeout, final InputStream inputStream, final OnRetrieveAttachmentInterface listener, final HashMap<String, String> paramaters, final String token) { public void upload(final InputStream inputStream, final OnRetrieveAttachmentInterface listener) {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
final URL url = new URL(urlConnection); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
final URL url = new URL("https://"+Helper.getLiveInstance(context)+"/api/v1/media");
ByteArrayOutputStream ous = null; ByteArrayOutputStream ous = null;
try { try {
try { try {
@ -193,7 +198,7 @@ public class HttpsConnection {
byte[] pixels = ous.toByteArray(); byte[] pixels = ous.toByteArray();
httpsURLConnection = (HttpsURLConnection) url.openConnection(); httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setConnectTimeout(timeout * 1000); httpsURLConnection.setConnectTimeout(240 * 1000);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory()); httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
httpsURLConnection.setDoInput(true); httpsURLConnection.setDoInput(true);
@ -257,7 +262,7 @@ public class HttpsConnection {
listener.onRetrieveAttachment(attachment, null); listener.onRetrieveAttachment(attachment, null);
}catch (Exception e) { }catch (Exception e) {
listener.onUpdateProgress(101); listener.onUpdateProgress(101);
fr.gouv.etalab.mastodon.client.Entities.Error error = new fr.gouv.etalab.mastodon.client.Entities.Error(); Error error = new Error();
error.setError(e.getMessage()); error.setError(e.getMessage());
listener.onRetrieveAttachment(null, error); listener.onRetrieveAttachment(null, error);
} }