Commit 861aca47 authored by Greg Messner's avatar Greg Messner
Browse files

Simplified logic around filename construction in downloadExport() (#383).

parent 2228180f
...@@ -120,35 +120,39 @@ public class ImportExportApi extends AbstractApi { ...@@ -120,35 +120,39 @@ public class ImportExportApi extends AbstractApi {
Response response = getWithAccepts(Response.Status.OK, null, MediaType.MEDIA_TYPE_WILDCARD, Response response = getWithAccepts(Response.Status.OK, null, MediaType.MEDIA_TYPE_WILDCARD,
"projects", getProjectIdOrPath(projectIdOrPath), "export", "download"); "projects", getProjectIdOrPath(projectIdOrPath), "export", "download");
try {
if (directory == null) if (directory == null) {
directory = new File(System.getProperty("java.io.tmpdir")); directory = new File(System.getProperty("java.io.tmpdir"));
}
if(filename == null) {
// No filename provided if (filename == null) {
String disposition = response.getHeaderString("Content-Disposition");
if (disposition == null) { // No filename provided
// On GitLab.com the Content-Disposition returned is null String disposition = response.getHeaderString("Content-Disposition");
if (projectIdOrPath instanceof Project) { if (disposition == null) {
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('/', '_')); // On GitLab.com the Content-Disposition returned is null
// filename = "2019-06-10_10-28-52_namespace-group_test-project_export.tar.gz" String name = null;
} else if(projectIdOrPath instanceof String) { if (projectIdOrPath instanceof Project) {
String template = "%1$tY-%1$tm-%1$td_%1$tH-%1$tM-%1$tS_%2$s_export.tar.gz"; name = ((Project) projectIdOrPath).getPathWithNamespace().replace('/', '_');
filename = String.format(template, new Date(), projectIdOrPath); } else if(projectIdOrPath instanceof String) {
// filename = "2019-06-10_10-28-52_test-project_export.tar.gz" name = (String)projectIdOrPath;
} else if(projectIdOrPath instanceof Integer) { } 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"; name = "projectid-" + projectIdOrPath;
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");
} }
// template = "YYYY-MM-DD_HH-MM-SS_{name}_export.tar.gz"
final 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(), name);
} else {
filename = disposition.replaceFirst("(?i)^.*filename=\"?([^\"]+)\"?.*$", "$1");
} }
File file = new File(directory, filename); }
try {
File file = new File(directory, filename);
InputStream in = response.readEntity(InputStream.class); InputStream in = response.readEntity(InputStream.class);
Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING); Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
return (file); return (file);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment