From 00a44a1df8c1da0ec5665be6ce77de46052bf4d4 Mon Sep 17 00:00:00 2001 From: Him188 <Him188@mamoe.net> Date: Wed, 25 Mar 2020 09:44:54 +0800 Subject: [PATCH] Fix build --- .gitignore | 2 + buildSrc/src/main/kotlin/GitToken.kt | 8 --- buildSrc/src/main/kotlin/GitUploader.java | 73 --------------------- buildSrc/src/main/kotlin/upload/GitToken.kt | 69 +++++++++++++++++++ 4 files changed, 71 insertions(+), 81 deletions(-) delete mode 100644 buildSrc/src/main/kotlin/GitToken.kt delete mode 100644 buildSrc/src/main/kotlin/GitUploader.java create mode 100644 buildSrc/src/main/kotlin/upload/GitToken.kt diff --git a/.gitignore b/.gitignore index c74264b8b..d6367b15d 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,5 @@ local.properties # Maven publishing credits keys.properties /plugins/ + +token.txt \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/GitToken.kt b/buildSrc/src/main/kotlin/GitToken.kt deleted file mode 100644 index c2fc20e15..000000000 --- a/buildSrc/src/main/kotlin/GitToken.kt +++ /dev/null @@ -1,8 +0,0 @@ - - -fun getGitToken():String{ - with(System.getProperty("user.dir") + "/token.txt"){ - println("reading token file in " + this) - return File(this).readText() - } -} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/GitUploader.java b/buildSrc/src/main/kotlin/GitUploader.java deleted file mode 100644 index d519fa51a..000000000 --- a/buildSrc/src/main/kotlin/GitUploader.java +++ /dev/null @@ -1,73 +0,0 @@ -import java.io.*; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.Base64; - -public class GitUploader { - public static boolean create(String url, File file) { - long begin = System.currentTimeMillis(); - System.out.println("上传开始..."); - //StringBuffer result = new StringBuffer(); - BufferedReader in = null; - HttpURLConnection conn = null; - try { - URL realUrl = new URL(url); - conn = (HttpURLConnection) realUrl.openConnection(); - conn.setConnectTimeout(120000); - conn.setReadTimeout(120000); - // 设置 - conn.setDoOutput(true); // 需要输出 - conn.setDoInput(true); // 需要输入 - conn.setUseCaches(false); // 不允许缓存 - conn.setRequestMethod("PUT"); // 设置PUT方式连接 - - conn.setRequestProperty("Content-Type", "application/json"); - conn.setRequestProperty("Authorization", "token " + getGitToken()); - conn.setRequestProperty("User-Agent", "Github File Uploader App"); - conn.connect(); - // 传输数据 - DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); - // 传输json头部 - dos.writeBytes("{\"message\":\".\",\"content\":\""); - // 传输文件内容 - byte[] buffer = new byte[1024 * 1002]; // 3的倍数 - RandomAccessFile raf = new RandomAccessFile(file, "r"); - long size = raf.read(buffer); - while (size > -1) { - if (size == buffer.length) { - dos.write(Base64.getEncoder().encode(buffer)); - } else { - byte tmp[] = new byte[(int) size]; - System.arraycopy(buffer, 0, tmp, 0, (int) size); - dos.write(Base64.getEncoder().encode(tmp)); - } - size = raf.read(buffer); - } - raf.close(); - // 传输json尾部 - dos.writeBytes("\"}"); - dos.flush(); - dos.close(); - - in = new BufferedReader(new InputStreamReader(conn.getInputStream())); - String line; - while ((line = in.readLine()) != null) { - //result.append(line).append("\n"); - } - } catch (Exception e) { - System.out.println("发送PUT请求出现异常!"); - e.printStackTrace(); - return false; - } finally { - try { - in.close(); - } catch (Exception e2) { - } - } - long end = System.currentTimeMillis(); - System.out.printf("上传结束,耗时 %ds\n", (end - begin) / 1000); - //result.toString() - return true; - } - -} diff --git a/buildSrc/src/main/kotlin/upload/GitToken.kt b/buildSrc/src/main/kotlin/upload/GitToken.kt new file mode 100644 index 000000000..dc1d5698a --- /dev/null +++ b/buildSrc/src/main/kotlin/upload/GitToken.kt @@ -0,0 +1,69 @@ +package upload + +import java.io.* +import java.net.HttpURLConnection +import java.net.URL +import java.util.* + +object GitToken { + + private fun getGitToken(): String { + with(System.getProperty("user.dir") + "/token.txt") { + println("reading token file in $this") + return File(this).readText() + } + } + + fun upload(file: File, url: String) { + val begin = System.currentTimeMillis() + println("上传开始...") + //StringBuffer result = new StringBuffer(); + //StringBuffer result = new StringBuffer(); + var `in`: BufferedReader? = null + var conn: HttpURLConnection? = null + conn = URL(url).openConnection() as HttpURLConnection + conn.connectTimeout = 120000 + conn.readTimeout = 120000 + // 设置 + conn.doOutput = true // 需要输出 + conn.doInput = true // 需要输入 + conn.useCaches = false // 不允许缓存 + conn.requestMethod = "PUT" // 设置PUT方式连接 + conn.setRequestProperty("Content-Type", "application/json") + conn.setRequestProperty("Authorization", "token " + getGitToken()) + conn.setRequestProperty("User-Agent", "Github File Uploader App") + conn.connect() + // 传输数据 + val dos = DataOutputStream(conn.outputStream) + // 传输json头部 + dos.writeBytes("{\"message\":\".\",\"content\":\"") + // 传输文件内容 + val buffer = ByteArray(1024 * 1002) // 3的倍数 + val raf = RandomAccessFile(file, "r") + var size: Long = raf.read(buffer).toLong() + while (size > -1) { + if (size == buffer.size.toLong()) { + dos.write(Base64.getEncoder().encode(buffer)) + } else { + val tmp = ByteArray(size.toInt()) + System.arraycopy(buffer, 0, tmp, 0, size.toInt()) + dos.write(Base64.getEncoder().encode(tmp)) + } + size = raf.read(buffer).toLong() + } + raf.close() + // 传输json尾部 + dos.writeBytes("\"}") + dos.flush() + dos.close() + `in` = BufferedReader(InputStreamReader(conn.inputStream)) + var line: String? + while (`in`.readLine().also { line = it } != null) { + //result.append(line).append("\n"); + } + val end = System.currentTimeMillis() + System.out.printf("Upload finished within %d seconds\n", (end - begin) / 1000) + //result.toString() + //result.toString() + } +} \ No newline at end of file