Commit 149fa00e authored by Artem Hrybeniuk's avatar Artem Hrybeniuk
Browse files

Thumbnails: Using RepositoryService for getting thumbnails

parent f891d892
......@@ -25,15 +25,17 @@ import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.security.SecurityContextUtil;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.service.BytesStorageService;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.exception.NotFoundElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
......@@ -55,16 +57,15 @@ public class RepositoryDownloadController {
@Autowired
private RepositoryService repositoryService;
@Autowired
private BytesStorageService byteStorageService;
private void downloadFile(final Path path, final String name, final String ext, final HttpServletResponse response, HttpServletRequest request) throws IOException, NotFoundElement, NoSuchRepositoryFileException {
byte[] data;
boolean noCache = "no-cache".equalsIgnoreCase(request.getHeader(HttpHeaders.CACHE_CONTROL))
|| "no-cache".equalsIgnoreCase(request.getHeader(HttpHeaders.PRAGMA));
if (path.startsWith(RepositoryService.THUMB_PATH) && ext.equals(RepositoryService.THUMB_EXT)) {
String extension = StringUtils.removeStartIgnoreCase(ext, ".");
if (path.startsWith(RepositoryService.THUMB_PATH) && (extension.equals(RepositoryService.THUMB_EXT_JPG) || extension.equals(RepositoryService.THUMB_EXT_WEBP))) {
final String filename = name + ext;
if (LOG.isDebugEnabled()) {
LOG.debug("_thumb path={} filename={}", path, filename);
......@@ -79,19 +80,25 @@ public class RepositoryDownloadController {
return;
}
data = this.byteStorageService.get(path.resolve(filename));
if (data == null) {
throw new NotFoundElement("No thumbnail at " + path.resolve(filename));
try {
data = repositoryService.getThumbnail(path, name, extension, repositoryFile);
} catch (Exception e) {
throw new InvalidApiUsageException("Thumbnail cannot be fetched", e);
}
response.setDateHeader(HttpHeaders.LAST_MODIFIED, repositoryFile.getLastModifiedDate().getTime());
response.setHeader(HttpHeaders.ETAG, repositoryFile.getSha1Sum());
response.setContentType(RepositoryService.THUMB_CONTENT_TYPE);
if (extension.equals(RepositoryService.THUMB_EXT_JPG)) {
response.setContentType(RepositoryService.THUMB_CONTENT_TYPE_JPG);
} else {
response.setContentType(RepositoryService.THUMB_CONTENT_TYPE_WEBP);
}
// Cache longer for public files
if (SecurityContextUtil.anyoneHasPermission(repositoryFile, "READ")) {
// Cache for 30days
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=2592000, s-maxage=2592000, public, no-transform");
} else {
// Cache for 24hrs
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=86400, s-maxage=86400, private, no-transform");
}
......@@ -118,10 +125,11 @@ public class RepositoryDownloadController {
throw new NotFoundElement("No such thing");
}
// Cache for 24hrs
if (SecurityContextUtil.anyoneHasPermission(repositoryFile, "READ")) {
// Cache for 30days
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=2592000, s-maxage=2592000, public, no-transform");
} else {
// Cache for 24hrs
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=86400, s-maxage=86400, private, no-transform");
}
response.setHeader(HttpHeaders.PRAGMA, "");
......@@ -139,9 +147,14 @@ public class RepositoryDownloadController {
}
}
if (data != null && data.length > 0) {
response.setContentLength(data.length);
response.getOutputStream().write(data);
response.flushBuffer();
} else {
throw new NotFoundElement("No such thing. Sorry");
}
}
private boolean clientCacheValid(RepositoryFile repositoryFile, HttpServletRequest request, HttpServletResponse response) throws IOException {
......
......@@ -25,11 +25,12 @@ import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.security.SecurityContextUtil;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.service.BytesStorageService;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.exception.NotFoundElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -51,16 +52,15 @@ public class RepositoryDownloadController extends BaseController {
@Autowired
private RepositoryService repositoryService;
@Autowired
private BytesStorageService byteStorageService;
private void downloadFile(final Path path, final String name, final String ext, final HttpServletResponse response, HttpServletRequest request) throws IOException {
byte[] data;
boolean noCache = "no-cache".equalsIgnoreCase(request.getHeader(HttpHeaders.CACHE_CONTROL))
|| "no-cache".equalsIgnoreCase(request.getHeader(HttpHeaders.PRAGMA));
if (path.startsWith(RepositoryService.THUMB_PATH) && ext.equals(RepositoryService.THUMB_EXT)) {
String extension = StringUtils.removeStartIgnoreCase(ext, ".");
if (path.startsWith(RepositoryService.THUMB_PATH) && (extension.equals(RepositoryService.THUMB_EXT_JPG) || extension.equals(RepositoryService.THUMB_EXT_WEBP))) {
final String filename = name + ext;
if (LOG.isDebugEnabled()) {
LOG.debug("_thumb path={} filename={}", path, filename);
......@@ -75,14 +75,25 @@ public class RepositoryDownloadController extends BaseController {
return;
}
try {
data = repositoryService.getThumbnail(path, name, extension, repositoryFile);
} catch (Exception e) {
throw new InvalidApiUsageException("Thumbnail cannot be fetched", e);
}
response.setDateHeader(HttpHeaders.LAST_MODIFIED, repositoryFile.getLastModifiedDate().getTime());
response.setHeader(HttpHeaders.ETAG, repositoryFile.getSha1Sum());
response.setContentType("image/png");
if (extension.equals(RepositoryService.THUMB_EXT_JPG)) {
response.setContentType(RepositoryService.THUMB_CONTENT_TYPE_JPG);
} else {
response.setContentType(RepositoryService.THUMB_CONTENT_TYPE_WEBP);
}
// Cache for 24hrs
if (SecurityContextUtil.anyoneHasPermission(repositoryFile, "READ")) {
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=86400, s-maxage=86400, public, no-transform");
// Cache for 30days
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=2592000, s-maxage=2592000, public, no-transform");
} else {
// Cache for 24hrs
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=86400, s-maxage=86400, private, no-transform");
}
......@@ -90,11 +101,6 @@ public class RepositoryDownloadController extends BaseController {
throw new NotFoundElement("No file for thumb " + name);
}
data = this.byteStorageService.get(path.resolve(filename));
if (data == null) {
throw new NotFoundElement("No thumbnail at " + path.resolve(filename));
}
} else {
// Regular repository file
try {
......@@ -109,11 +115,15 @@ public class RepositoryDownloadController extends BaseController {
}
data = this.repositoryService.getFileBytes(repositoryFile);
if (data == null) {
throw new NotFoundElement("No such thing");
}
// Cache for 24hrs
if (SecurityContextUtil.anyoneHasPermission(repositoryFile, "READ")) {
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=86400, s-maxage=86400, public, no-transform");
// Cache for 30days
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=2592000, s-maxage=2592000, public, no-transform");
} else {
// Cache for 24hrs
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=86400, s-maxage=86400, private, no-transform");
}
response.setHeader(HttpHeaders.PRAGMA, "");
......
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