Adds file name

This commit is contained in:
stom79 2018-04-23 07:00:12 +02:00
parent 789afba048
commit ec2dd65e4b
5 changed files with 45 additions and 19 deletions

View File

@ -103,6 +103,7 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
private ImageView pp_actionBar;
private final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE_HEADER = 754;
private final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE_PICTURE = 755;
private String avatarName, headerName;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -400,7 +401,8 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
}
}).start();
GlideApp.get(getApplicationContext()).clearMemory();
new UpdateCredentialAsyncTask(getApplicationContext(), profile_username, profile_note, profile_picture, header_picture, profile_privacy, EditProfileActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new UpdateCredentialAsyncTask(getApplicationContext(), profile_username, profile_note, profile_picture, avatarName, header_picture, headerName, profile_privacy, EditProfileActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@ -452,6 +454,7 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
return;
}
Uri fileUri = data.getData();
headerName = Helper.getFileName(EditProfileActivity.this, fileUri);
try {
InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(fileUri);
assert inputStream != null;
@ -469,6 +472,7 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
return;
}
Uri fileUri = data.getData();
avatarName = Helper.getFileName(EditProfileActivity.this, fileUri);
try {
InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(fileUri);
assert inputStream != null;

View File

@ -32,14 +32,14 @@ import fr.gouv.etalab.mastodon.interfaces.OnUpdateCredentialInterface;
public class UpdateCredentialAsyncTask extends AsyncTask<Void, Void, Void> {
private String display_name, note;
private String display_name, note, avatarName, headerName;
private ByteArrayInputStream avatar, header;
private API.accountPrivacy privacy;
private APIResponse apiResponse;
private OnUpdateCredentialInterface listener;
private WeakReference<Context> contextReference;
public UpdateCredentialAsyncTask(Context context, String display_name, String note, ByteArrayInputStream avatar, ByteArrayInputStream header, API.accountPrivacy privacy, OnUpdateCredentialInterface onUpdateCredentialInterface){
public UpdateCredentialAsyncTask(Context context, String display_name, String note, ByteArrayInputStream avatar, String avatarName, ByteArrayInputStream header, String headerName, API.accountPrivacy privacy, OnUpdateCredentialInterface onUpdateCredentialInterface){
this.contextReference = new WeakReference<>(context);
this.display_name = display_name;
this.note = note;
@ -47,11 +47,13 @@ public class UpdateCredentialAsyncTask extends AsyncTask<Void, Void, Void> {
this.header = header;
this.listener = onUpdateCredentialInterface;
this.privacy = privacy;
this.avatarName = avatarName;
this.headerName = headerName;
}
@Override
protected Void doInBackground(Void... params) {
apiResponse = new API(this.contextReference.get()).updateCredential(display_name, note, avatar, header, privacy);
apiResponse = new API(this.contextReference.get()).updateCredential(display_name, note, avatar, avatarName, header, headerName, privacy);
return null;
}

View File

@ -143,7 +143,7 @@ public class API {
* Update credential of the authenticated user *synchronously*
* @return APIResponse
*/
public APIResponse updateCredential(String display_name, String note, ByteArrayInputStream avatar, ByteArrayInputStream header, accountPrivacy privacy) {
public APIResponse updateCredential(String display_name, String note, ByteArrayInputStream avatar, String avatarName, ByteArrayInputStream header, String headerName, accountPrivacy privacy) {
HashMap<String, String> requestParams = new HashMap<>();
if( display_name != null)
@ -162,7 +162,7 @@ public class API {
requestParams.put("locked",privacy==accountPrivacy.LOCKED?"true":"false");
try {
new HttpsConnection(context).patch(getAbsoluteUrl("/accounts/update_credentials"), 60, requestParams, avatar, header, prefKeyOauthTokenT);
new HttpsConnection(context).patch(getAbsoluteUrl("/accounts/update_credentials"), 60, requestParams, avatar, avatarName, header, headerName, prefKeyOauthTokenT);
} catch (HttpsConnection.HttpsConnectionException e) {
e.printStackTrace();
setError(e.getStatusCode(), e);

View File

@ -628,7 +628,7 @@ public class HttpsConnection {
@SuppressWarnings("SameParameterValue")
void patch(String urlConnection, int timeout, HashMap<String, String> paramaters, InputStream avatar, InputStream header, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
void patch(String urlConnection, int timeout, HashMap<String, String> paramaters, InputStream avatar, String avatarName, InputStream header, String headerName, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
String twoHyphens = "--";
String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
String lineEnd = "\r\n";
@ -676,7 +676,7 @@ public class HttpsConnection {
lengthSentAvatar = pixelsAvatar.length;
lengthSentAvatar += 2 * (twoHyphens + boundary + twoHyphens + lineEnd).getBytes().length;
lengthSentAvatar += ("Content-Disposition: form-data; name=\"avatar\";filename=\"avatar.png\"" + lineEnd).getBytes().length;
lengthSentAvatar += ("Content-Disposition: form-data; name=\"avatar\";filename=\""+avatarName+"\"" + lineEnd).getBytes().length;
lengthSentAvatar += 2 * (lineEnd).getBytes().length;
}
@ -704,7 +704,7 @@ public class HttpsConnection {
lengthSentHeader = pixelsHeader.length;
lengthSentHeader += 2 * (twoHyphens + boundary + twoHyphens + lineEnd).getBytes().length;
lengthSentHeader += ("Content-Disposition: form-data; name=\"header\";filename=\"header.png\"" + lineEnd).getBytes().length;
lengthSentHeader += ("Content-Disposition: form-data; name=\"header\";filename=\""+headerName+"\"" + lineEnd).getBytes().length;
lengthSentHeader += 2 * (lineEnd).getBytes().length;
}
@ -725,11 +725,14 @@ public class HttpsConnection {
httpsURLConnection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
httpsURLConnection.setRequestMethod("POST");
}
httpsURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpsURLConnection.setRequestProperty("Cache-Control", "no-cache");
if( lengthSent > 0) {
httpsURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpsURLConnection.setRequestProperty("Cache-Control", "no-cache");
}
httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpsURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ boundary);
if( lengthSent > 0)
httpsURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ boundary);
if (token != null)
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
@ -737,16 +740,15 @@ public class HttpsConnection {
if( lengthSent > 0)
httpsURLConnection.setFixedLengthStreamingMode(lengthSent+postDataBytes.length);
httpsURLConnection.getOutputStream().write(postDataBytes);
OutputStream outputStream = httpsURLConnection.getOutputStream();
outputStream.write(postDataBytes);
if(lengthSentAvatar > 0){
OutputStream outPutStream = httpsURLConnection.getOutputStream();
DataOutputStream request = new DataOutputStream(outPutStream);
DataOutputStream request = new DataOutputStream(outputStream);
int totalSize = pixelsAvatar.length;
int bytesTransferred = 0;
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
request.writeBytes("Content-Disposition: form-data; name=\"avatar\";filename=\"avatar.png\"" + lineEnd);
request.writeBytes("Content-Disposition: form-data; name=\"avatar\";filename=\""+avatarName+"\"" + lineEnd);
request.writeBytes(lineEnd);
while (bytesTransferred < totalSize) {
int nextChunkSize = totalSize - bytesTransferred;
@ -755,6 +757,7 @@ public class HttpsConnection {
}
request.write(pixelsAvatar, bytesTransferred, nextChunkSize);
bytesTransferred += nextChunkSize;
request.flush();
}
request.writeBytes(lineEnd);
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
@ -768,7 +771,7 @@ public class HttpsConnection {
OutputStream outPutStream = httpsURLConnection.getOutputStream();
DataOutputStream request = new DataOutputStream(outPutStream);
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
request.writeBytes("Content-Disposition: form-data; name=\"header\";filename=\"header.png\"" + lineEnd);
request.writeBytes("Content-Disposition: form-data; name=\"header\";filename=\""+headerName+"\"" + lineEnd);
request.writeBytes(lineEnd);
while (bytesTransferred < totalSize) {
int nextChunkSize = totalSize - bytesTransferred;
@ -777,6 +780,7 @@ public class HttpsConnection {
}
request.write(pixelsHeader, bytesTransferred, nextChunkSize);
bytesTransferred += nextChunkSize;
request.flush();
}
request.writeBytes(lineEnd);
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

View File

@ -21,6 +21,7 @@ import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
@ -29,6 +30,7 @@ import android.graphics.Rect;
import android.graphics.RectF;
import android.os.CountDownTimer;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.support.annotation.Nullable;
import android.support.customtabs.CustomTabsIntent;
import android.support.media.ExifInterface;
@ -2104,6 +2106,20 @@ public class Helper {
return bs;
}
public static String getFileName(Context context, Uri uri) {
ContentResolver resolver = context.getContentResolver();
Cursor returnCursor =
resolver.query(uri, null, null, null, null);
assert returnCursor != null;
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
String name = returnCursor.getString(nameIndex);
returnCursor.close();
Random r = new Random();
int suf = r.nextInt(9999 - 1000) + 1000;
return String.valueOf(suf)+name;
}
@SuppressWarnings("WeakerAccess")
public static void largeLog(String content) {
if (content.length() > 4000) {