mirror of
https://github.com/jlengrand/openapi-generator.git
synced 2026-05-16 00:21:19 +00:00
Support file downloading in Java okhttp-gson client
This commit is contained in:
@@ -22,6 +22,7 @@ import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
@@ -35,6 +36,9 @@ import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.text.ParseException;
|
||||
|
||||
import okio.BufferedSink;
|
||||
import okio.Okio;
|
||||
|
||||
import {{invokerPackage}}.auth.Authentication;
|
||||
import {{invokerPackage}}.auth.HttpBasicAuth;
|
||||
import {{invokerPackage}}.auth.ApiKeyAuth;
|
||||
@@ -490,6 +494,10 @@ public class ApiClient {
|
||||
if (response == null || returnType == null)
|
||||
return null;
|
||||
|
||||
// Handle file downloading.
|
||||
if (returnType.equals(File.class))
|
||||
return (T) downloadFileFromResponse(response);
|
||||
|
||||
String respBody;
|
||||
try {
|
||||
if (response.body() != null)
|
||||
@@ -538,6 +546,53 @@ public class ApiClient {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Download file from the given response.
|
||||
*/
|
||||
public File downloadFileFromResponse(Response response) throws ApiException {
|
||||
try {
|
||||
File file = prepareDownloadFile(response);
|
||||
BufferedSink sink = Okio.buffer(Okio.sink(file));
|
||||
sink.writeAll(response.body().source());
|
||||
sink.close();
|
||||
return file;
|
||||
} catch (IOException e) {
|
||||
throw new ApiException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public File prepareDownloadFile(Response response) throws IOException {
|
||||
String filename = null;
|
||||
String contentDisposition = response.header("Content-Disposition");
|
||||
if (contentDisposition != null && !"".equals(contentDisposition)) {
|
||||
// Get filename from the Content-Disposition header.
|
||||
Pattern pattern = Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
|
||||
Matcher matcher = pattern.matcher(contentDisposition);
|
||||
if (matcher.find())
|
||||
filename = matcher.group(1);
|
||||
}
|
||||
|
||||
String prefix = null;
|
||||
String suffix = null;
|
||||
if (filename == null) {
|
||||
prefix = "download-";
|
||||
suffix = "";
|
||||
} else {
|
||||
int pos = filename.lastIndexOf(".");
|
||||
if (pos == -1) {
|
||||
prefix = filename + "-";
|
||||
} else {
|
||||
prefix = filename.substring(0, pos) + "-";
|
||||
suffix = filename.substring(pos);
|
||||
}
|
||||
// File.createTempFile requires the prefix to be at least three characters long
|
||||
if (prefix.length() < 3)
|
||||
prefix = "download-";
|
||||
}
|
||||
|
||||
return File.createTempFile(prefix, suffix);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #execute(Call, Type)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user