use of org.eclipse.jgit.errors.LargeObjectException in project gitiles by GerritCodeReview.
the class PathServlet method doGetJson.
@Override
protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException {
GitilesView view = ViewFilter.getView(req);
Repository repo = ServletUtils.getRepository(req);
String longStr = req.getParameter("long");
boolean includeSizes = (longStr != null) && (longStr.isEmpty() || Boolean.TRUE.equals(StringUtils.toBooleanOrNull(longStr)));
String recursiveStr = req.getParameter("recursive");
boolean recursive = (recursiveStr != null) && (recursiveStr.isEmpty() || Boolean.TRUE.equals(StringUtils.toBooleanOrNull(recursiveStr)));
try (RevWalk rw = new RevWalk(repo);
WalkResult wr = WalkResult.forPath(rw, view, recursive)) {
if (wr == null) {
res.setStatus(SC_NOT_FOUND);
return;
}
switch(wr.type) {
case REGULAR_FILE:
renderJson(req, res, FileJsonData.toJsonData(wr.id, view.getRepositoryName(), view.getRevision().getName(), wr.path), FileJsonData.File.class);
break;
case TREE:
renderJson(req, res, TreeJsonData.toJsonData(wr.id, wr.tw, includeSizes, recursive), TreeJsonData.Tree.class);
break;
case EXECUTABLE_FILE:
case GITLINK:
case SYMLINK:
default:
res.setStatus(SC_NOT_FOUND);
break;
}
} catch (LargeObjectException e) {
res.setStatus(SC_INTERNAL_SERVER_ERROR);
}
}
use of org.eclipse.jgit.errors.LargeObjectException in project gerrit by GerritCodeReview.
the class FileContentUtil method downloadContent.
public BinaryResult downloadContent(ProjectState project, ObjectId revstr, String path, @Nullable Integer parent) throws ResourceNotFoundException, IOException {
try (Repository repo = openRepository(project);
RevWalk rw = new RevWalk(repo)) {
String suffix = "new";
RevCommit commit = rw.parseCommit(revstr);
if (parent != null && parent > 0) {
if (commit.getParentCount() == 1) {
suffix = "old";
} else {
suffix = "old" + parent;
}
commit = rw.parseCommit(commit.getParent(parent - 1));
}
ObjectReader reader = rw.getObjectReader();
TreeWalk tw = TreeWalk.forPath(reader, path, commit.getTree());
if (tw == null) {
throw new ResourceNotFoundException();
}
int mode = tw.getFileMode(0).getObjectType();
if (mode != Constants.OBJ_BLOB) {
throw new ResourceNotFoundException();
}
ObjectId id = tw.getObjectId(0);
ObjectLoader obj = repo.open(id, OBJ_BLOB);
byte[] raw;
try {
raw = obj.getCachedBytes(MAX_SIZE);
} catch (LargeObjectException e) {
raw = null;
}
MimeType contentType = registry.getMimeType(path, raw);
return registry.isSafeInline(contentType) ? wrapBlob(path, obj, raw, contentType, suffix) : zipBlob(path, obj, commit, suffix);
}
}
use of org.eclipse.jgit.errors.LargeObjectException in project gerrit by GerritCodeReview.
the class FileContentUtil method getContent.
public BinaryResult getContent(Repository repo, ProjectState project, ObjectId revstr, String path) throws IOException, ResourceNotFoundException {
try (RevWalk rw = new RevWalk(repo)) {
RevCommit commit = rw.parseCommit(revstr);
ObjectReader reader = rw.getObjectReader();
TreeWalk tw = TreeWalk.forPath(reader, path, commit.getTree());
if (tw == null) {
throw new ResourceNotFoundException();
}
org.eclipse.jgit.lib.FileMode mode = tw.getFileMode(0);
ObjectId id = tw.getObjectId(0);
if (mode == org.eclipse.jgit.lib.FileMode.GITLINK) {
return BinaryResult.create(id.name()).setContentType(X_GIT_GITLINK).base64();
}
ObjectLoader obj = repo.open(id, OBJ_BLOB);
byte[] raw;
try {
raw = obj.getCachedBytes(MAX_SIZE);
} catch (LargeObjectException e) {
raw = null;
}
String type;
if (mode == org.eclipse.jgit.lib.FileMode.SYMLINK) {
type = X_GIT_SYMLINK;
} else {
type = registry.getMimeType(path, raw).toString();
type = resolveContentType(project, path, FileMode.FILE, type);
}
return asBinaryResult(raw, obj).setContentType(type).base64();
}
}
Aggregations