198 lines
6.3 KiB
Java
198 lines
6.3 KiB
Java
package com.localtransfer;
|
|
|
|
import android.database.Cursor;
|
|
import android.net.Uri;
|
|
import android.provider.OpenableColumns;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
|
|
import androidx.core.content.FileProvider;
|
|
|
|
import com.google.android.material.snackbar.Snackbar;
|
|
|
|
import java.io.DataOutputStream;
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStreamWriter;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.sql.Timestamp;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.ConcurrentModificationException;
|
|
import java.util.Date;
|
|
|
|
public class UploadFile extends Transfer {
|
|
|
|
public Uri uri;
|
|
|
|
public UploadFile() {
|
|
this.id = View.generateViewId();
|
|
instances.add(this);
|
|
info = "Upload complete";
|
|
state = STATE_NOT_REQUESTED;
|
|
drawable = R.drawable.ic_upload_24;
|
|
}
|
|
|
|
public static void handleSendFile(Uri uri) {
|
|
if (uri != null) {
|
|
UploadFile file = new UploadFile();
|
|
file.uri = uri;
|
|
|
|
Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);
|
|
cursor.moveToFirst();
|
|
|
|
file.name = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
|
|
file.size = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
|
|
file.sizeSI = Transfer.humanReadableByteCountBin(file.size);
|
|
|
|
file.AddTransfer();
|
|
}
|
|
}
|
|
|
|
public static void handleSendText(String sharedText) {
|
|
if (sharedText != null) {
|
|
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMdd_HHmmss");
|
|
Date now = new Date();
|
|
String strDate = sdfDate.format(now);
|
|
Uri uri = null;
|
|
|
|
try {
|
|
File outputFile = new File(activity.getCacheDir(), strDate + ".txt");
|
|
FileOutputStream fileOut = new FileOutputStream(outputFile);
|
|
OutputStreamWriter OutWriter = new OutputStreamWriter(fileOut);
|
|
OutWriter.append(sharedText);
|
|
OutWriter.close();
|
|
|
|
fileOut.flush();
|
|
fileOut.close();
|
|
|
|
uri = FileProvider.getUriForFile(activity, activity.getPackageName(), outputFile);
|
|
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
handleSendFile(uri);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void startTransfer() {
|
|
super.startTransfer();
|
|
|
|
startedTime = new Timestamp(System.currentTimeMillis());
|
|
|
|
state = STATE_RUNNING;
|
|
|
|
try {
|
|
uploadFile();
|
|
} catch (IOException e) {
|
|
final String ExceptionName = e.getClass().getSimpleName();
|
|
final String ExceptionMess = e.getMessage();
|
|
|
|
state = Transfer.STATE_FAILED;
|
|
error = ExceptionName + ": " + ExceptionMess;
|
|
progressEnd();
|
|
|
|
if(ExceptionName != null && ExceptionMess != null) {
|
|
errorSnackbar(ExceptionMess);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
private String uploadFile() throws IOException {
|
|
String message = null;
|
|
URL url = null;
|
|
HttpURLConnection conn = null;
|
|
DataOutputStream request = null;
|
|
String boundary = "*****";
|
|
final int maxBufferSize = 1 * 1024 * 1024;
|
|
byte[] buffer = new byte[maxBufferSize];
|
|
int bufferLength;
|
|
loaded = 0;
|
|
|
|
Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);
|
|
cursor.moveToFirst();
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
size = in.available();
|
|
|
|
while ((bufferLength = in.read(buffer)) > 0) {
|
|
request.write(buffer, 0, bufferLength);
|
|
loaded+= bufferLength;
|
|
percent = ((loaded * 100) / size);
|
|
progressUpdateDelay();
|
|
}
|
|
|
|
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";
|
|
state = Transfer.STATE_SUCCESS;
|
|
progressEnd();
|
|
}
|
|
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());
|
|
}
|
|
|
|
return message;
|
|
}
|
|
}
|