Skip to content

Commit

Permalink
update sth. at 2022-10-09 14:16:33
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhaiai committed Oct 9, 2022
1 parent 3eeab1c commit a5674f3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/main/java/ff/jnezha/jnt/cs/GithubHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import ff.jnezha.jnt.org.json.JSONArray;
import ff.jnezha.jnt.org.json.JSONException;
import ff.jnezha.jnt.org.json.JSONObject;
import ff.jnezha.jnt.utils.FileUtils;
import ff.jnezha.jnt.utils.HttpType;
import ff.jnezha.jnt.utils.Logger;
import ff.jnezha.jnt.utils.TextUitls;
import ff.jnezha.jnt.utils.*;

import java.io.File;
import java.util.HashMap;
Expand Down Expand Up @@ -84,29 +81,29 @@ public static String append(String owner, String repo, String path, String conte
}

public static String append(String owner, String repo, String path, String token, String contentWillBase64,
String commitMsg) {
String commitMsg) {
return append(owner, repo, path, token, contentWillBase64, commitMsg, "", "");
}

public static String append(String owner, String repo, String path, String token, String contentWillBase64,
String commitMsg, String username, String email) {
String commitMsg, String username, String email) {
StringBuilder sb = new StringBuilder();
sb.append(getContent(owner, repo, path, token)).append("\r\n").append(contentWillBase64);
return updateContent(owner, repo, path, token, sb.toString(), commitMsg, username, email);
}

public static String updateContent(String owner, String repo, String path, String contentWillBase64,
String commitMsg) {
String commitMsg) {
return updateContent(owner, repo, path, token, contentWillBase64, commitMsg);
}

public static String updateContent(String owner, String repo, String path, String token, String contentWillBase64,
String commitMsg) {
String commitMsg) {
return updateContent(owner, repo, path, token, contentWillBase64, commitMsg, "", "");
}

public static String updateContent(String owner, String repo, String path, String token, String contentWillBase64,
String commitMsg, String username, String email) {
String commitMsg, String username, String email) {

String content = TextUitls.encodeBase64ToString(contentWillBase64);
String base = "https://api.github.com/repos/%s/%s/contents%s";
Expand Down Expand Up @@ -180,7 +177,7 @@ public static String deleteFile(String owner, String repo, String path, String t
* @return a {@link java.lang.String} object.
*/
public static String deleteFile(String owner, String repo, String path, String token, String commitMsg,
String username, String email) {
String username, String email) {

String base = "https://api.github.com/repos/%s/%s/contents%s";
String uploadUrl = String.format(base, owner, repo, path);
Expand Down Expand Up @@ -217,7 +214,7 @@ public static void deleteDir(String owner, String repo, String path, String toke
}

public static void deleteDir(String owner, String repo, String path, String token, String commitMsg,
String username, String email) {
String username, String email) {

String base = "https://api.github.com/repos/%s/%s/contents%s";
// String uploadUrl = String.format(base, owner, repo, path);
Expand Down Expand Up @@ -246,7 +243,7 @@ public static void deleteDir(String owner, String repo, String path, String toke
}

private static String realDelFileBySha(String url, String sha, String token, String commitMsg, String username,
String email) {
String email) {

if (TextUitls.isEmpty(url) || TextUitls.isEmpty(sha) || TextUitls.isEmpty(token)) {
return "";
Expand Down Expand Up @@ -275,12 +272,12 @@ private static String realDelFileBySha(String url, String sha, String token, Str
* @return a {@link java.lang.String} object.
*/
public static String createFile(String owner, String repo, String path, String token, String contentWillBase64,
String commitMsg) {
String commitMsg) {
return createFile(true, owner, repo, path, token, contentWillBase64, commitMsg, "", "");
}

public static String createFile(String owner, String repo, String path, String contentWillBase64,
String commitMsg) {
String commitMsg) {
return createFile(true, owner, repo, path, token, contentWillBase64, commitMsg, "", "");
}

Expand All @@ -293,12 +290,12 @@ public static String createFile(String owner, String repo, String path, String t
}

public static String createFile(boolean isNeedBase64, String owner, String repo, String path, String uploadContent,
String commitMsg, String username, String email) {
String commitMsg, String username, String email) {
return createFile(isNeedBase64, owner, repo, path, token, uploadContent, commitMsg, username, email);
}

public static String createFile(boolean isNeedBase64, String owner, String repo, String path, String token,
String uploadContent, String commitMsg, String username, String email) {
String uploadContent, String commitMsg, String username, String email) {
try {
Map<String, ShaInfo> shas = getSha(owner, repo, path, token);
if (shas != null && shas.size() > 0) {
Expand All @@ -324,7 +321,15 @@ public static String createFile(boolean isNeedBase64, String owner, String repo,
if (TextUitls.isEmpty(res)) {
return "";
}
return new JSONObject(res).optJSONObject("content").optString("download_url", "");
JSONObject o1 = new JSONObject(res);
if (!JsonHelper.has(o1, "content")) {
return "";
}
JSONObject o2 = o1.optJSONObject("content");
if (!JsonHelper.has(o2, "download_url")) {
return "";
}
return o2.optString("download_url", "");
} catch (Throwable e) {
Logger.e(e);
}
Expand Down Expand Up @@ -534,16 +539,16 @@ static class ShaInfo {

// dir request
public ShaInfo(String __name, String __path, String __sha, long __size, String __url, String __html_url,
String __git_url, String __download_url, String __type, JSONObject ___links, String ___links_key_self,
String ___links_key_git, String ___links_key_html) {
String __git_url, String __download_url, String __type, JSONObject ___links, String ___links_key_self,
String ___links_key_git, String ___links_key_html) {
this(__name, __path, __sha, __size, __url, __html_url, __git_url, __download_url, __type, ___links,
___links_key_self, ___links_key_git, ___links_key_html, null, null);
}

public ShaInfo(String __name, String __path, String __sha, long __size, String __url, String __html_url,
String __git_url, String __download_url, String __type, JSONObject ___links, String ___links_key_self,
String ___links_key_git, String ___links_key_html
// only in 文件path请求的文件
String __git_url, String __download_url, String __type, JSONObject ___links, String ___links_key_self,
String ___links_key_git, String ___links_key_html
// only in 文件path请求的文件
, String __content, String __encoding) {
this.name = __name;
this.path = __path;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/ff/jnezha/jnt/utils/JsonHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ff.jnezha.jnt.utils;

import ff.jnezha.jnt.org.json.JSONObject;

public class JsonHelper {

public static boolean has(JSONObject obj, String key) {
if (obj == null || obj.length() <= 0) {
return false;
}
if (obj.has(key)) {
return true;
}
return false;
}
}

0 comments on commit a5674f3

Please sign in to comment.