Fix upload MMS without file name and size

This commit is contained in:
lionel 2022-02-07 23:41:03 +01:00
parent cba7fc2555
commit 057038cca7

View File

@ -7,19 +7,14 @@ import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.OpenableColumns;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.FileProvider;
import androidx.preference.PreferenceManager;
@ -32,7 +27,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
@ -41,9 +35,7 @@ import java.nio.charset.StandardCharsets;
import java.text.CharacterIterator;
import java.text.SimpleDateFormat;
import java.text.StringCharacterIterator;
import java.util.ConcurrentModificationException;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
@ -141,7 +133,7 @@ public class Transfer {
public void handleSendText(String sharedText) {
if (sharedText != null) {
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMdd_HHmmss");
SimpleDateFormat sdfDate = new SimpleDateFormat("text_yyyyMMdd_HHmmss");
Date now = new Date();
String strDate = sdfDate.format(now);
new Thread(() -> {
@ -171,83 +163,94 @@ public class Transfer {
HttpURLConnection conn = null;
DataOutputStream request = null;
String boundary = "*****";
int maxBufferSize = 1 * 1024 * 1024;
final int maxBufferSize = 1 * 1024 * 1024;
byte[] buffer = new byte[maxBufferSize];
int bufferLength;
long loaded = 0;
Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
String fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
String fileNameUTF8 = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1);
long fileSize = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
String type = activity.getContentResolver().getType(uri);
String fileName = null;
int col_name = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
if (col_name != -1)
fileName = cursor.getString(col_name);
if(fileName == null)
fileName = type.split("/")[0] + "_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + "." + type.split("/")[1];
String fileNameUTF8 = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1);
long fileSize = -1;
int col_size = cursor.getColumnIndex(OpenableColumns.SIZE);
if (col_size != -1)
fileSize = cursor.getLong(col_size);
url = new URL(protocol, host, port, root + "/upload.php");
url = checkRedirection(url);
Log.d("URL", url.toString());
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setChunkedStreamingMode(maxBufferSize);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
conn.connect();
request = new DataOutputStream(conn.getOutputStream());
request.writeBytes("--" + boundary + "\r\n");
request.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileNameUTF8 + "\"\r\n");
request.writeBytes("Content-Type: " + type + "\r\n\r\n");
InputStream in = activity.getContentResolver().openInputStream(uri);
if (fileSize < 1)
fileSize = in.available();
Progress p = new Progress(fileName, fileSize, Progress.UPLOAD);
if(url != null) {
while ((bufferLength = in.read(buffer)) > 0) {
request.write(buffer, 0, bufferLength);
loaded+= (long) bufferLength;
p.loaded = loaded;
p.percent = ((loaded * 100) / fileSize);
}
Log.d("URL", url.toString());
request.writeBytes("\r\n");
request.writeBytes("--" + boundary + "--\r\n");
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setChunkedStreamingMode(maxBufferSize);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
in.close();
conn.connect();
int responseCode = conn.getResponseCode();
request = new DataOutputStream(conn.getOutputStream());
if (responseCode == HttpURLConnection.HTTP_OK) {
message = "File " + fileName + " successful upload";
p.stopProgress();
}
else {
String s = conn.getResponseMessage();
throw new HttpErrorException("error code: " + responseCode + " " + s);
}
request.writeBytes("--" + boundary + "\r\n");
request.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileNameUTF8 + "\"\r\n");
request.writeBytes("Content-Type: " + type + "\r\n\r\n");
request.flush();
InputStream in = activity.getContentResolver().openInputStream(uri);
request.close();
while ((bufferLength = in.read(buffer)) > 0) {
request.write(buffer, 0, bufferLength);
loaded+= (long) bufferLength;
p.loaded = loaded;
p.percent = ((loaded * 100) / fileSize);
}
conn.disconnect();
request.writeBytes("\r\n");
request.writeBytes("--" + boundary + "--\r\n");
in.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
message = "File " + fileName + " successful upload";
p.stopProgress();
}
else {
String s = conn.getResponseMessage();
throw new HttpErrorException("error code: " + responseCode + " " + s);
}
request.flush();
request.close();
conn.disconnect();
System.out.println(message);
if (activity != null) {
String finalMessage = message;
activity.runOnUiThread(() ->
Snackbar.make(activity.findViewById(R.id.view_pager), finalMessage, Snackbar.LENGTH_LONG)
.setAction("Action", null).show());
}
System.out.println(message);
if (activity != null) {
String finalMessage = message;
activity.runOnUiThread(() ->
Snackbar.make(activity.findViewById(R.id.view_pager), finalMessage, Snackbar.LENGTH_LONG)
.setAction("Action", null).show());
}
return message;