return ok(responseBody);
}
}
public static Result getFile(Long id) throws IOException {
Attachment attachment = Attachment.find.byId(id);
String action = HttpUtil.getFirstValueFromQuery(request().queryString(), "action");
String dispositionType = StringUtils.equals(action, "download") ? "attachment" : "inline";
if (attachment == null) {
return notFound("The file does not exist.");
}
String eTag = "\"" + attachment.hash + "-" + dispositionType + "\"";
if (!AccessControl.isAllowed(UserApp.currentUser(), attachment.asResource(), Operation.READ)) {
return forbidden("You have no permission to get the file.");
}
response().setHeader("Cache-Control", "private, max-age=3600");
String ifNoneMatchValue = request().getHeader("If-None-Match");
if(ifNoneMatchValue != null && ifNoneMatchValue.equals(eTag)) {
response().setHeader("ETag", eTag);
return status(NOT_MODIFIED);
}
File file = attachment.getFile();
if(file != null && !file.exists()){
Logger.error("Attachment ID:" + id + " (" + file.getAbsolutePath() + ") does not exist on storage");
return internalServerError("The file does not exist");
}