]>
Commit | Line | Data |
---|---|---|
e9a08ae2 JR |
1 | package im.jfr.i |
2 | ||
3 | import android.content.ClipData | |
4 | import android.content.ClipboardManager | |
5 | import android.content.Context | |
6 | import android.content.Intent | |
7 | import android.net.Uri | |
8 | import android.os.Build | |
9 | import androidx.appcompat.app.AppCompatActivity | |
10 | import android.os.Bundle | |
11 | import android.os.Parcelable | |
12 | import android.widget.TextView | |
13 | import android.widget.Toast | |
14 | import org.json.JSONObject | |
15 | import java.net.HttpURLConnection | |
16 | import java.net.URL | |
732657bf | 17 | import kotlin.system.exitProcess |
e9a08ae2 JR |
18 | |
19 | class ShareActivity : AppCompatActivity() { | |
20 | override fun onCreate(savedInstanceState: Bundle?) { | |
21 | super.onCreate(savedInstanceState) | |
22 | setContentView(R.layout.activity_share) | |
23 | ||
732657bf JR |
24 | when (intent?.action) { |
25 | Intent.ACTION_SEND -> { | |
e9a08ae2 | 26 | if (intent.type?.startsWith("image/") == true) { |
732657bf | 27 | Thread { |
e9a08ae2 | 28 | handleImage(intent) |
732657bf | 29 | }.start() |
e9a08ae2 JR |
30 | } else { |
31 | assert(false) { "Uhoh! Unsupported MIME" } | |
32 | } | |
33 | } | |
34 | else -> { | |
35 | assert(false) { "Uhoh! Unsupported intent" } | |
36 | } | |
37 | } | |
38 | } | |
39 | ||
732657bf | 40 | private fun handleImage(intent: Intent) { |
e9a08ae2 | 41 | val hyphens = "--".toByteArray() |
732657bf | 42 | val boundary = "o3C5AoN2J8vyaYPh9Ewd4xvQwS5DjpJFSeRNF6sAPhS4QrbNL".toByteArray() |
e9a08ae2 JR |
43 | val crlf = "\r\n".toByteArray() |
44 | ||
45 | val url = URL("https://i.jfr.im/upload.php").openConnection() as HttpURLConnection | |
46 | url.doOutput = true | |
47 | url.requestMethod = "POST" | |
48 | url.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary.toString(Charsets.UTF_8)) | |
49 | val output = url.outputStream | |
732657bf | 50 | output.write(hyphens + boundary + crlf) |
e9a08ae2 JR |
51 | (intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let { |
52 | output.write("Content-Disposition: form-data; name=\"files[]\";filename=\"$it\"\r\n".toByteArray()) | |
53 | output.write(("Content-Type: "+intent.type+"\r\n").toByteArray()) | |
54 | output.write(crlf) | |
55 | val input = contentResolver.openInputStream(it) | |
56 | input?.copyTo(output) | |
57 | /* | |
58 | val buffer = ByteArray(102400) | |
59 | var len = input.read(buffer) | |
60 | while (len != -1) { | |
61 | output.write(buffer, 0, len) | |
62 | len = input.read(buffer) | |
63 | } | |
64 | */ | |
732657bf | 65 | input?.close() |
e9a08ae2 JR |
66 | output.write(crlf) |
67 | output.write(hyphens + boundary + hyphens + crlf) | |
68 | } | |
69 | assert(url.responseCode == 200) { "Response was not 200, " + url.responseCode.toString() + " " + url.responseMessage } | |
70 | val tv = findViewById<TextView>(R.id.resultView) | |
71 | val jsonStr = url.inputStream.readBytes().toString(Charsets.UTF_8) | |
72 | val jsonObj = JSONObject(jsonStr) | |
73 | assert(jsonObj.optBoolean("success", false)) { "Response JSON not success" } | |
732657bf | 74 | val uploadedLink = jsonObj.getJSONArray("files").getJSONObject(0).getString("url") |
e9a08ae2 JR |
75 | (getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager).setPrimaryClip(ClipData.newPlainText(uploadedLink, uploadedLink)) |
76 | ||
77 | tv.post { | |
78 | tv.text = uploadedLink | |
79 | if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2) | |
80 | Toast.makeText(applicationContext, "Copied", Toast.LENGTH_SHORT).show() | |
81 | ||
82 | runOnUiThread { | |
732657bf | 83 | exitProcess(0) |
e9a08ae2 JR |
84 | } |
85 | } | |
86 | } | |
87 | } |