Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/main/java/org/gitlab4j/api/ImportExportApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Date;
import java.util.Map;

import javax.ws.rs.core.Form;
Expand Down Expand Up @@ -100,6 +101,22 @@ public ExportStatus getExportStatus(Object projectIdOrPath) throws GitLabApiExce
* @throws GitLabApiException if any exception occurs
*/
public File downloadExport(Object projectIdOrPath, File directory) throws GitLabApiException {
return downloadExport(projectIdOrPath, directory, null);
}

/**
* Download the finished export.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/export/download</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param directory the File instance of the directory to save the export file to, if null will use "java.io.tmpdir"
* @param filename Name to give to the downloaded file. If null then we try to get from Content-Disposition header
* or to compute one from parameters
* @return a File instance pointing to the download of the project export file
* @throws GitLabApiException if any exception occurs
*/
public File downloadExport(Object projectIdOrPath, File directory, String filename) throws GitLabApiException {

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

String disposition = response.getHeaderString("Content-Disposition");
String filename = disposition.replaceFirst("(?i)^.*filename=\"?([^\"]+)\"?.*$", "$1");
if(filename == null) {
// No filename provided
String disposition = response.getHeaderString("Content-Disposition");
if (disposition == null) {
// On GitLab.com the Content-Disposition returned is null
if (projectIdOrPath instanceof Project) {
String template = "%1$tY-%1$tm-%1$td_%1$tH-%1$tM-%1$tS_%2$s_export.tar.gz";
filename = String.format(template, new Date(), ((Project) projectIdOrPath).getPathWithNamespace().replace('/', '_'));
// filename = "2019-06-10_10-28-52_namespace-group_test-project_export.tar.gz"
} else if(projectIdOrPath instanceof String) {
String template = "%1$tY-%1$tm-%1$td_%1$tH-%1$tM-%1$tS_%2$s_export.tar.gz";
filename = String.format(template, new Date(), projectIdOrPath);
// filename = "2019-06-10_10-28-52_test-project_export.tar.gz"
} else if(projectIdOrPath instanceof Integer) {
String template = "%1$tY-%1$tm-%1$td_%1$tH-%1$tM-%1%tS_projectid-%2$d_export.tar.gz";
filename = String.format(template, new Date(), projectIdOrPath);
// filename = "2019-06-10_10-28-52_projectid_3115610_export.tar.gz"
}
} else {
filename = disposition.replaceFirst("(?i)^.*filename=\"?([^\"]+)\"?.*$", "$1");
}
}
File file = new File(directory, filename);

InputStream in = response.readEntity(InputStream.class);
Expand Down