Skip to content

Commit 861aca4

Browse files
committed
Simplified logic around filename construction in downloadExport() (#383).
1 parent 2228180 commit 861aca4

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

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

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -120,35 +120,39 @@ public File downloadExport(Object projectIdOrPath, File directory, String filena
120120

121121
Response response = getWithAccepts(Response.Status.OK, null, MediaType.MEDIA_TYPE_WILDCARD,
122122
"projects", getProjectIdOrPath(projectIdOrPath), "export", "download");
123-
try {
124123

125-
if (directory == null)
126-
directory = new File(System.getProperty("java.io.tmpdir"));
127-
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");
124+
if (directory == null) {
125+
directory = new File(System.getProperty("java.io.tmpdir"));
126+
}
127+
128+
if (filename == null) {
129+
130+
// No filename provided
131+
String disposition = response.getHeaderString("Content-Disposition");
132+
if (disposition == null) {
133+
134+
// On GitLab.com the Content-Disposition returned is null
135+
String name = null;
136+
if (projectIdOrPath instanceof Project) {
137+
name = ((Project) projectIdOrPath).getPathWithNamespace().replace('/', '_');
138+
} else if(projectIdOrPath instanceof String) {
139+
name = (String)projectIdOrPath;
140+
} else if(projectIdOrPath instanceof Integer) {
141+
name = "projectid-" + projectIdOrPath;
148142
}
143+
144+
// template = "YYYY-MM-DD_HH-MM-SS_{name}_export.tar.gz"
145+
final String template = "%1$tY-%1$tm-%1$td_%1$tH-%1$tM-%1$tS_%2$s_export.tar.gz";
146+
filename = String.format(template, new Date(), name);
147+
148+
} else {
149+
filename = disposition.replaceFirst("(?i)^.*filename=\"?([^\"]+)\"?.*$", "$1");
149150
}
150-
File file = new File(directory, filename);
151+
}
151152

153+
try {
154+
155+
File file = new File(directory, filename);
152156
InputStream in = response.readEntity(InputStream.class);
153157
Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
154158
return (file);

0 commit comments

Comments
 (0)