| 1 | package tw.nekomimi.nkmr |
| 2 | |
| 3 | import android.graphics.BitmapFactory |
| 4 | import com.google.gson.Gson |
| 5 | import okhttp3.OkHttpClient |
| 6 | import okhttp3.Request |
| 7 | import java.io.* |
| 8 | import kotlin.concurrent.thread |
| 9 | import kotlin.math.roundToInt |
| 10 | import android.graphics.Color |
| 11 | import org.telegram.messenger.FileLog |
| 12 | import java.nio.charset.Charset |
| 13 | |
| 14 | |
| 15 | object MiniCDNDrive { |
| 16 | |
| 17 | data class metaJSON( |
| 18 | var time: Long = 0, |
| 19 | var filename: String, |
| 20 | var size: Long = 0, |
| 21 | var sha1: String, |
| 22 | var block: Array<metaJSON_Block>, |
| 23 | ) |
| 24 | |
| 25 | data class metaJSON_Block( |
| 26 | var size: Long = 0, |
| 27 | var sha1: String, |
| 28 | var url: String |
| 29 | ) |
| 30 | |
| 31 | interface CallbackPercent { |
| 32 | fun callbackPercent(percent: Int); |
| 33 | } |
| 34 | |
| 35 | fun Download(f: File, metaURL: String, callbackPercent: CallbackPercent, callbackFinished: Runnable) { |
| 36 | thread { |
| 37 | var output = FileOutputStream(f, false) |
| 38 | val client = OkHttpClient(); |
| 39 | |
| 40 | val request = Request.Builder() |
| 41 | .url(meta2Real(metaURL)) |
| 42 | .build() |
| 43 | val response = client.newCall(request).execute() |
| 44 | val data = readPhotoBytes(response.body!!.bytes()) |
| 45 | |
| 46 | val meta = Gson().fromJson(String(data, Charset.forName("UTF-8")), metaJSON::class.java) |
| 47 | |
| 48 | var counter = 0 |
| 49 | for (block in meta.block) { |
| 50 | lateinit var data2: ByteArray |
| 51 | val try_max = 3 |
| 52 | |
| 53 | for (i in 0 until try_max) { |
| 54 | try { |
| 55 | val url = block.url.replace("http://", "https://") |
| 56 | |
| 57 | val request2 = Request.Builder() |
| 58 | .url(url) |
| 59 | .build() |
| 60 | val response2 = client.newCall(request2).execute() |
| 61 | data2 = readPhotoBytes(response2.body!!.bytes()) |
| 62 | break |
| 63 | } catch (e: Exception) { |
| 64 | if (i == try_max - 1) { |
| 65 | throw e |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | output.write(data2) |
| 70 | |
| 71 | //TODO progress |
| 72 | counter++ |
| 73 | callbackPercent.callbackPercent((counter * 100.0 / meta.block.size).roundToInt()) |
| 74 | } |
| 75 | |
| 76 | output.close() |
| 77 | callbackFinished.run() |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | fun meta2Real(meta: String): String { |
| 82 | val regexp = Regex("bdex://([a-fA-F0-9]{40})") |
| 83 | val matches = regexp.find(meta)!! |
| 84 | |
| 85 | return "https://i0.hdslb.com/bfs/album/${matches.groupValues[1]}.png" |
| 86 | } |
| 87 | |
| 88 | fun readPhotoBytes(input: ByteArray): ByteArray { |
| 89 | val img = BitmapFactory.decodeByteArray(input, 0, input.size) |
| 90 | |
| 91 | val buf = ArrayList<Byte>() |
| 92 | |
| 93 | for (y in 0 until img.width) { |
| 94 | for (x in 0 until img.height) { |
| 95 | val colour: Int = img.getPixel(x, y) |
| 96 | buf.add(Color.red(colour).toByte()) |
| 97 | buf.add(Color.green(colour).toByte()) |
| 98 | buf.add(Color.blue(colour).toByte()) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | val buf1 = buf.toByteArray() |
| 103 | val length = buf1.readUInt32LE(0).toInt() |
| 104 | |
| 105 | return buf1.copyOfRange(4, 4 + length) |
| 106 | } |
| 107 | |
| 108 | private fun ByteArray.readUInt32LE(offset: Int = 0): Long { |
| 109 | return (((this[offset + 3].toInt() and 0xFF).toLong() shl 24) or |
| 110 | ((this[offset + 2].toInt() and 0xFF).toLong() shl 16) or |
| 111 | ((this[offset + 1].toInt() and 0xFF).toLong() shl 8) or |
| 112 | (this[offset].toInt() and 0xFF).toLong()) |
| 113 | } |
| 114 | |
| 115 | } |