in src/main/java/com/googlesource/gerrit/plugins/imagare/ImageServlet.java [97:208]
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException {
if (!"GET".equals(req.getMethod()) && !"HEAD".equals(req.getMethod())) {
CacheHeaders.setNotCacheable(res);
res.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
return;
}
ResourceKey key = ResourceKey.fromPath(getEncodedPath(req));
ProjectState state = projectCache.get(key.project);
if (state == null || key.file == null) {
notFound(res);
return;
}
MimeType mimeType = fileTypeRegistry.getMimeType(key.file, (byte[]) null);
if (!("image".equals(mimeType.getMediaType()) && fileTypeRegistry.isSafeInline(mimeType))) {
notFound(res);
return;
}
try {
ProjectState projectState = projectCache.get(key.project);
String rev = key.revision;
if (rev == null || Constants.HEAD.equals(rev)) {
rev = getHead.get().apply(new ProjectResource(projectState, self.get()));
} else {
if (!ObjectId.isId(rev)) {
if (!rev.startsWith(Constants.R_REFS)) {
rev = Constants.R_HEADS + rev;
}
PermissionBackend.ForProject perm = permissionBackend.currentUser().project(key.project);
try {
perm.ref(rev).check(RefPermission.READ);
} catch (AuthException e) {
notFound(res);
return;
}
}
}
try (Repository repo = repoManager.openRepository(key.project)) {
ObjectId revId = repo.resolve(rev != null ? rev : Constants.HEAD);
if (revId == null) {
notFound(res);
return;
}
if (ObjectId.isId(rev)) {
try (RevWalk rw = new RevWalk(repo)) {
RevCommit commit = rw.parseCommit(repo.resolve(rev));
if (!commits.canRead(state, repo, commit)) {
notFound(res);
return;
}
}
}
String eTag = null;
String receivedETag = req.getHeader(HttpHeaders.IF_NONE_MATCH);
if (receivedETag != null) {
eTag = computeETag(key.project, revId, key.file);
if (eTag.equals(receivedETag)) {
res.sendError(SC_NOT_MODIFIED);
return;
}
}
if (!"image".equals(mimeType.getMediaType())) {
notFound(res);
return;
}
try (RevWalk rw = new RevWalk(repo)) {
RevCommit commit = rw.parseCommit(revId);
RevTree tree = commit.getTree();
try (TreeWalk tw = new TreeWalk(repo)) {
tw.addTree(tree);
tw.setRecursive(true);
tw.setFilter(PathFilter.create(key.file));
if (!tw.next()) {
notFound(res);
return;
}
ObjectId objectId = tw.getObjectId(0);
ObjectLoader loader = repo.open(objectId);
byte[] content = loader.getBytes(Integer.MAX_VALUE);
mimeType = fileTypeRegistry.getMimeType(key.file, content);
if (!"image".equals(mimeType.getMediaType())
|| !fileTypeRegistry.isSafeInline(mimeType)) {
notFound(res);
return;
}
res.setHeader(
HttpHeaders.ETAG, eTag != null ? eTag : computeETag(key.project, revId, key.file));
CacheHeaders.setCacheablePrivate(res, 7, TimeUnit.DAYS, false);
send(req, res, content, mimeType.toString(), commit.getCommitTime());
return;
}
} catch (IOException e) {
notFound(res);
return;
}
}
} catch (RepositoryNotFoundException
| ResourceNotFoundException
| AuthException
| RevisionSyntaxException
| PermissionBackendException e) {
notFound(res);
return;
}
}