|
|
@@ -24,8 +24,8 @@ import java.nio.charset.StandardCharsets;
|
|
|
@Service
|
|
|
public class GetObjectService {
|
|
|
private final FileMetaMapper fileMetaMapper;
|
|
|
- // 10MiB
|
|
|
- private final int bufSize = 1024*1024*10;
|
|
|
+ // 1MiB
|
|
|
+ private final int bufSize = 1024*1024;
|
|
|
private final ObjectRepository objectRepository;
|
|
|
private final String domain;
|
|
|
|
|
|
@@ -72,15 +72,8 @@ public class GetObjectService {
|
|
|
long len = objectMeta.getSize();
|
|
|
String range = ServletUtil.getRequest().getHeader("range");
|
|
|
if (range != null) {
|
|
|
- String rangeStr = StringUtils.trimAllWhitespace(range);
|
|
|
- String[] arr = rangeStr.replace("bytes=", "").split("-");
|
|
|
- long start = Long.parseLong(arr[0]);
|
|
|
- if (arr.length == 1) {
|
|
|
- writeContentRange(objectMeta, start, len);
|
|
|
- } else {
|
|
|
- ContentRange contentRange = parseContentRange(range, len);
|
|
|
- writeContentRange(objectMeta, contentRange.getStart(), contentRange.getEnd());
|
|
|
- }
|
|
|
+ ContentRange contentRange = parseContentRange(range, len);
|
|
|
+ writeContentRange(objectMeta, contentRange);
|
|
|
} else {
|
|
|
if (host.contains(domain)) {
|
|
|
writeWholeContent(objectMeta);
|
|
|
@@ -90,44 +83,30 @@ public class GetObjectService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void writeAcceptRanges(String contentType, long len) throws IOException {
|
|
|
- HttpServletResponse response = ServletUtil.getResponse();
|
|
|
- response.setStatus(HttpServletResponse.SC_OK);
|
|
|
- response.setContentType(contentType);
|
|
|
- response.setHeader("Content-Length", ""+len);
|
|
|
- response.setHeader("Accept-Ranges", "bytes");
|
|
|
-
|
|
|
- OutputStream outputStream = response.getOutputStream();
|
|
|
- outputStream.flush();
|
|
|
- outputStream.close();
|
|
|
- }
|
|
|
-
|
|
|
private ContentRange parseContentRange(String range, long len) {
|
|
|
String rangeStr = StringUtils.trimAllWhitespace(range);
|
|
|
String[] arr = rangeStr.replace("bytes=", "").split("-");
|
|
|
long start = Long.parseLong(arr[0]);
|
|
|
- long end = 0;
|
|
|
+ long end;
|
|
|
if (arr.length == 2) {
|
|
|
end = Long.parseLong(arr[1]);
|
|
|
} else {
|
|
|
long l = len - start;
|
|
|
- if (l > 0) {
|
|
|
- end = Math.min(l, bufSize) + start;
|
|
|
- } else {
|
|
|
- System.out.println();
|
|
|
- }
|
|
|
+ end = Math.min(l, bufSize) + start;
|
|
|
}
|
|
|
|
|
|
return new ContentRange(start, end);
|
|
|
}
|
|
|
|
|
|
- private void writeContentRange(ObjectMeta objectMeta, long start, long end) throws IOException {
|
|
|
+ private void writeContentRange(ObjectMeta objectMeta, ContentRange contentRange) throws IOException {
|
|
|
+ long start = contentRange.getStart();
|
|
|
+ long end = contentRange.getEnd();
|
|
|
long contentLength = objectMeta.getSize();
|
|
|
HttpServletResponse response = ServletUtil.getResponse();
|
|
|
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
|
|
|
response.setContentType(objectMeta.getContentType());
|
|
|
- response.setHeader("Content-Length", ""+(end-start));
|
|
|
response.setHeader("Accept-Ranges", "bytes");
|
|
|
+ response.setHeader("Content-Length", ""+(end-start));
|
|
|
response.setHeader("Content-Range", "bytes "+start+"-"+(end-1)+"/"+contentLength);
|
|
|
|
|
|
String absolutePath = objectMeta.getAbsolutePath();
|