/
GithubHttp.java
158 lines (143 loc) · 7.01 KB
/
GithubHttp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.zestedesavoir.zestwriter.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zestedesavoir.zestwriter.model.Container;
import com.zestedesavoir.zestwriter.model.Content;
import com.zestedesavoir.zestwriter.model.Extract;
import com.zestedesavoir.zestwriter.model.MetaContent;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Executor;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zeroturnaround.zip.ZipUtil;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static com.zestedesavoir.zestwriter.utils.ZdsHttp.toSlug;
public class GithubHttp {
static Logger logger = LoggerFactory.getLogger(GithubHttp.class);
public static String getGithubZipball(String owner, String repo, String destFolder) throws IOException {
CloseableHttpClient httpclient = HttpClients.custom()
.setRedirectStrategy(new LaxRedirectStrategy())
.build();
String urlForGet = "https://api.github.com/repos/" + owner + "/" + repo + "/zipball/";
HttpGet get = new HttpGet(urlForGet);
HttpResponse response = httpclient.execute(get);
InputStream is = response.getEntity().getContent();
String filePath = destFolder + File.separator + repo + ".zip";
FileOutputStream fos = new FileOutputStream(new File(filePath));
int inByte;
while ((inByte = is.read()) != -1)
fos.write(inByte);
is.close();
fos.close();
return filePath;
}
public static File unzipOnlineContent(String zipFilePath, String destFolder) {
String dirname = Paths.get(zipFilePath).getFileName().toString();
dirname = dirname.substring(0, dirname.length() - 4);
// create output directory is not exists
System.out.println("dirname = "+dirname);
File folder = new File(destFolder + File.separator + dirname);
logger.debug("Tentative de dezippage de " + zipFilePath + " dans " + folder.getAbsolutePath());
if (!folder.exists()) {
folder.mkdir();
logger.info("Dézippage dans " + folder.getAbsolutePath() + " réalisé avec succès");
} else {
logger.debug("Le répertoire dans lequel vous souhaitez dezipper existe déjà ");
}
ZipUtil.unpack(new File(zipFilePath), folder);
return folder;
}
public static Content loadManifest(String folder, String owner, String repo) throws IOException {
String projecUrl = "https://api.github.com/repos/"+owner+"/"+repo;
String title = null;
logger.debug("Tentative de connexion à l'url : "+projecUrl);
String github_user = System.getProperty("zw.github_user");
String github_token = System.getProperty("zw.github_token");
Executor executor = null;
if(github_user != null && !github_user.equals("") && github_token != null && !github_token.equals("")) {
executor = Executor
.newInstance()
.auth(new HttpHost("api.github.com"), github_user, github_token);
logger.debug("Authentification avec "+github_user+":"+github_token);
} else {
executor = Executor.newInstance();
}
String json = executor.execute(Request.Get(projecUrl)).returnContent().asString();
ObjectMapper mapper = new ObjectMapper();
Map map = mapper.readValue(json, Map.class);
if(map.containsKey("description")) {
title = (String) map.get("description");
}
Content current = new Content("container", toSlug(title), title, "introduction.md", "conclusion.md", new ArrayList<MetaContent>(), 2, "CC-BY", title, "TUTORIAL");
// read all directory
current.getChildren ().addAll (loadDirectory (folder.length () + File.separator.length (), new File(folder)));
current.setBasePath (folder);
File fileIntro = new File (current.getFilePath (), "introduction.md");
File fileConclu = new File (current.getFilePath (), "conclusion.md");
try {
if(!fileIntro.exists ()) {
fileIntro.createNewFile ();
}
if(!fileConclu.exists ()) {
fileConclu.createNewFile ();
}
} catch (IOException e) {
e.printStackTrace ();
}
return current;
}
private static List<MetaContent> loadDirectory(int countBase, File folder) {
List<MetaContent> metas = new ArrayList<> ();
File[] listF = folder.listFiles();
for(File file : listF) {
String name = file.getName();
if(file.isDirectory ()) {
String intro = file.getAbsolutePath ().substring (countBase)+File.separator+"introduction.md";
String conclu = file.getAbsolutePath ().substring (countBase)+File.separator+"conclusion.md";
intro = intro.replace (File.separator, "/");
conclu = conclu.replace (File.separator, "/");
MetaContent container = new Container ("container", ZdsHttp.toSlug (name), name, intro ,conclu, new ArrayList<MetaContent> ());
((Container)container).getChildren().addAll (loadDirectory (countBase, file));
metas.add(container);
File fileIntro = new File (file, "introduction.md");
File fileConclu = new File (file, "conclusion.md");
try {
if(!fileIntro.exists ()) {
fileIntro.createNewFile ();
}
if(!fileConclu.exists ()) {
fileConclu.createNewFile ();
}
} catch (IOException e) {
e.printStackTrace ();
}
} else if(file.isFile ()) {
int pos = name.lastIndexOf(".");
if (name.endsWith ("md")) {
if((! name.equals ("introduction.md")) && (! name.equals ("conclusion.md")) ) {
if (pos > 0) {
name = name.substring (0, pos);
}
String text = file.getAbsolutePath ().substring (countBase);
text = text.replace (File.separator, "/");
MetaContent extract = new Extract ("extract", ZdsHttp.toSlug (name), name, text);
metas.add (extract);
}
}
}
}
return metas;
}
}