Skip to content

Commit 2228180

Browse files
PhilippeViennegmessner
authored andcommitted
Fix for null pointer #382 (#383)
* Fix for null pointer #382 * Added method with filename parameter for download function #382
1 parent b4b55b0 commit 2228180

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/main/java/org/gitlab4j/api/ImportExportApi.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.net.URL;
77
import java.nio.file.Files;
88
import java.nio.file.StandardCopyOption;
9+
import java.util.Date;
910
import java.util.Map;
1011

1112
import javax.ws.rs.core.Form;
@@ -100,6 +101,22 @@ public ExportStatus getExportStatus(Object projectIdOrPath) throws GitLabApiExce
100101
* @throws GitLabApiException if any exception occurs
101102
*/
102103
public File downloadExport(Object projectIdOrPath, File directory) throws GitLabApiException {
104+
return downloadExport(projectIdOrPath, directory, null);
105+
}
106+
107+
/**
108+
* Download the finished export.
109+
*
110+
* <pre><code>GitLab Endpoint: GET /projects/:id/export/download</code></pre>
111+
*
112+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
113+
* @param directory the File instance of the directory to save the export file to, if null will use "java.io.tmpdir"
114+
* @param filename Name to give to the downloaded file. If null then we try to get from Content-Disposition header
115+
* or to compute one from parameters
116+
* @return a File instance pointing to the download of the project export file
117+
* @throws GitLabApiException if any exception occurs
118+
*/
119+
public File downloadExport(Object projectIdOrPath, File directory, String filename) throws GitLabApiException {
103120

104121
Response response = getWithAccepts(Response.Status.OK, null, MediaType.MEDIA_TYPE_WILDCARD,
105122
"projects", getProjectIdOrPath(projectIdOrPath), "export", "download");
@@ -108,8 +125,28 @@ public File downloadExport(Object projectIdOrPath, File directory) throws GitLab
108125
if (directory == null)
109126
directory = new File(System.getProperty("java.io.tmpdir"));
110127

111-
String disposition = response.getHeaderString("Content-Disposition");
112-
String filename = disposition.replaceFirst("(?i)^.*filename=\"?([^\"]+)\"?.*$", "$1");
128+
if(filename == null) {
129+
// No filename provided
130+
String disposition = response.getHeaderString("Content-Disposition");
131+
if (disposition == null) {
132+
// On GitLab.com the Content-Disposition returned is null
133+
if (projectIdOrPath instanceof Project) {
134+
String template = "%1$tY-%1$tm-%1$td_%1$tH-%1$tM-%1$tS_%2$s_export.tar.gz";
135+
filename = String.format(template, new Date(), ((Project) projectIdOrPath).getPathWithNamespace().replace('/', '_'));
136+
// filename = "2019-06-10_10-28-52_namespace-group_test-project_export.tar.gz"
137+
} else if(projectIdOrPath instanceof String) {
138+
String template = "%1$tY-%1$tm-%1$td_%1$tH-%1$tM-%1$tS_%2$s_export.tar.gz";
139+
filename = String.format(template, new Date(), projectIdOrPath);
140+
// filename = "2019-06-10_10-28-52_test-project_export.tar.gz"
141+
} else if(projectIdOrPath instanceof Integer) {
142+
String template = "%1$tY-%1$tm-%1$td_%1$tH-%1$tM-%1%tS_projectid-%2$d_export.tar.gz";
143+
filename = String.format(template, new Date(), projectIdOrPath);
144+
// filename = "2019-06-10_10-28-52_projectid_3115610_export.tar.gz"
145+
}
146+
} else {
147+
filename = disposition.replaceFirst("(?i)^.*filename=\"?([^\"]+)\"?.*$", "$1");
148+
}
149+
}
113150
File file = new File(directory, filename);
114151

115152
InputStream in = response.readEntity(InputStream.class);

0 commit comments

Comments
 (0)