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