diff options
| author | Maksymilian Jopek <maks@jopek.eu> | 2022-11-10 00:35:19 +0100 |
|---|---|---|
| committer | Maksymilian Jopek <maks@jopek.eu> | 2022-11-10 00:35:19 +0100 |
| commit | 0cc57833b2e4aef20799dd25baaac6162128287e (patch) | |
| tree | d3dd6ca63c374c8a45a7d9d85ad67ecda8b4aea1 /android/app/src/main/java | |
| download | submitto-dati-inator-master.tar.gz submitto-dati-inator-master.tar.zst submitto-dati-inator-master.zip | |
App is able to share single file.
Server is outside of vcs for time being and is able to process multiple files.
Authorization is done with HTTP Basi Authorization.
Nginx is listing files.
Diffstat (limited to 'android/app/src/main/java')
| -rw-r--r-- | android/app/src/main/java/com/jopek/submittodatiinator/MainActivity.java | 86 | ||||
| -rw-r--r-- | android/app/src/main/java/com/jopek/submittodatiinator/Requests.java | 151 |
2 files changed, 237 insertions, 0 deletions
diff --git a/android/app/src/main/java/com/jopek/submittodatiinator/MainActivity.java b/android/app/src/main/java/com/jopek/submittodatiinator/MainActivity.java new file mode 100644 index 0000000..467c5be --- /dev/null +++ b/android/app/src/main/java/com/jopek/submittodatiinator/MainActivity.java @@ -0,0 +1,86 @@ +package com.jopek.submittodatiinator; + +import android.annotation.SuppressLint; +import android.app.ProgressDialog; +import android.content.Intent; +import android.database.Cursor; +import android.net.Uri; +import android.os.Bundle; +import android.provider.MediaStore; +import android.provider.OpenableColumns; +import android.util.Log; +import android.widget.TextView; + +import androidx.appcompat.app.AppCompatActivity; + +public class MainActivity extends AppCompatActivity { + /********** File Path *************/ + TextView messageText; + ProgressDialog dialog = null; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + messageText = findViewById(R.id.tv); + + //dialog = ProgressDialog.show(MainActivity.this, "", "Uploading file...", true); + + Intent intent = getIntent(); + String action = intent.getAction(); + String type = intent.getType(); + Uri fileUri = null; + + if (Intent.ACTION_SEND.equals(action) && type != null) { + Object object = intent.getParcelableExtra(Intent.EXTRA_STREAM); + if(object != null && object instanceof Uri) fileUri = (Uri) object; +// if (type.startsWith("image/")) { +// fileUri = intent.getParcelableExtra(Intent.EXTRA_STREAM); +// } +// if (type.startsWith("video/")) { +// fileUri = intent.getParcelableExtra(Intent.EXTRA_STREAM); +// } +// if (type.startsWith("application/")) { +// fileUri = intent.getParcelableExtra(Intent.EXTRA_STREAM); +// } + } + + Log.d("maks", "45: fileUri: " + fileUri); + if (fileUri != null) { +// String sourceFileUri = getRealPathFromURI(fileUri); + String sourceFileUri = fileUri.getPath(); + Uri finalFileUri = fileUri; + new Thread(() -> { + runOnUiThread(() -> messageText.setText("uploading started...")); + Log.d("maks", "onCreate: " + sourceFileUri); + Requests.ReturnValues retVal = Requests.uploadFile(this, finalFileUri); + switch (retVal) { + case MALFORMED_URL: + case NO_FILE: + case SERVER_ERROR: + runOnUiThread(() -> messageText.setText("error while uploading...")); + break; + case OK: + runOnUiThread(() -> messageText.setText("File uploaded!")); + break; + } + }).start(); + } + } + + public String getRealPathFromURI(Uri contentUri) { + Cursor cursor = null; + try { + cursor = this.getContentResolver().query(contentUri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null); + cursor.moveToFirst(); + String filePath = cursor.getString(0); + cursor.close(); + return filePath; + } finally { + if (cursor != null) { + cursor.close(); + } + } + } +}
\ No newline at end of file diff --git a/android/app/src/main/java/com/jopek/submittodatiinator/Requests.java b/android/app/src/main/java/com/jopek/submittodatiinator/Requests.java new file mode 100644 index 0000000..fafc87d --- /dev/null +++ b/android/app/src/main/java/com/jopek/submittodatiinator/Requests.java @@ -0,0 +1,151 @@ +package com.jopek.submittodatiinator; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.provider.OpenableColumns; +import android.util.Log; + +import java.io.DataOutputStream; +import java.io.File; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.Random; + +public class Requests { + public static final String TAG = "REQUESTS"; + + static final String uploadServerUri = "https://jopek.eu/maks/submitto-dati-inator/upload.php"; + private final static String lineEnd = "\r\n"; + private final static String twoHyphens = "--"; + + public static ReturnValues uploadFile(Context context, Uri sourceFileUri) { + final String boundary = "------------------------" + getRandomString(16); + int serverResponseCode = 0; + HttpURLConnection conn = null; + DataOutputStream dos = null; + int bytesRead, bytesAvailable, bufferSize; + byte[] buffer; + int maxBufferSize = 1 * 1024 * 1024; + String filename = getFileName(context, sourceFileUri); +// File sourceFile = new File(sourceFileUri); + +// if (!sourceFile.isFile()) { +// Log.e(TAG, "Source File not exist :" + sourceFileUri); +// return ReturnValues.NO_FILE; +// } else { + try { + // open a URL connection to the Servlet +// InputStream fileInputStream = new FileInputStream(sourceFile); + InputStream fileInputStream = context.getContentResolver().openInputStream(sourceFileUri); + + URL url = new URL(uploadServerUri); + + // Open a HTTP connection to the URL + 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(2048); + conn.setRequestMethod("POST"); + conn.setRequestProperty("Connection", "Keep-Alive"); + conn.setRequestProperty("ENCTYPE", "multipart/form-data"); + conn.setRequestProperty("Authorization", "Basic maks:<pass>"); + conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); + + dos = new DataOutputStream(conn.getOutputStream()); + + dos.writeBytes(twoHyphens + boundary + lineEnd); + dos.writeBytes("Content-Disposition: form-data; name=" + filename + ";filename=" + filename + "" + lineEnd); + dos.writeBytes(lineEnd); + + // create a buffer of maximum size + bytesAvailable = fileInputStream.available(); + + bufferSize = Math.min(bytesAvailable, maxBufferSize); + buffer = new byte[bufferSize]; + + // read file and write it into form... + bytesRead = fileInputStream.read(buffer, 0, bufferSize); + + while (bytesRead > 0) { + dos.write(buffer, 0, bufferSize); + bytesAvailable = fileInputStream.available(); + bufferSize = Math.min(bytesAvailable, maxBufferSize); + bytesRead = fileInputStream.read(buffer, 0, bufferSize); + } + + // send multipart form data necessary after file data... + dos.writeBytes(lineEnd); + dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); + + // Responses from the server (code and message) + serverResponseCode = conn.getResponseCode(); + String serverResponseMessage = conn.getResponseMessage(); + + Log.i(TAG, "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode); + + //close the streams + fileInputStream.close(); + dos.flush(); + dos.close(); + if (serverResponseCode != 200) return ReturnValues.SERVER_ERROR; + } catch (MalformedURLException e) { + e.printStackTrace(); + Log.e(TAG, "error: " + e.getMessage(), e); + return ReturnValues.MALFORMED_URL; + } catch (Exception e) { + e.printStackTrace(); + Log.e(TAG, "Exception : " + e.getMessage(), e); + return ReturnValues.SERVER_ERROR; + } + return ReturnValues.OK; +// } + } + + public static String getRandomString(int length) { + int leftLimit = 48; // numeral '0' + int rightLimit = 122; // letter 'z' + Random random = new Random(); + + return random.ints(leftLimit, rightLimit + 1).filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97)).limit(length).collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString(); + } + + @SuppressLint("Range") + public static String getFileName(Context context, Uri uri) { + String result = null; + if (uri.getScheme().equals("content")) { + Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); + try { + if (cursor != null && cursor.moveToFirst()) { + result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); + } + } finally { + cursor.close(); + } + } + if (result == null) { + result = uri.getPath(); + int cut = result.lastIndexOf('/'); + if (cut != -1) { + result = result.substring(cut + 1); + } + } + try { + return URLEncoder.encode(result, String.valueOf(StandardCharsets.UTF_8)); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + return result; + } + + public enum ReturnValues { + OK, NO_FILE, MALFORMED_URL, SERVER_ERROR, + } +} |
