Merge changes from topic 'inline-3'
* changes:
Add REST API to retrieve file's content type from revision
InlineEdit: Add GET /changes/{id}/edit/{path}/type endpoint
This commit is contained in:
@@ -516,4 +516,23 @@ public class ChangeEdits implements
|
||||
return BinaryResult.create(m).base64();
|
||||
}
|
||||
}
|
||||
|
||||
@Singleton
|
||||
public static class GetType implements RestReadView<ChangeEditResource> {
|
||||
private final FileContentUtil fileContentUtil;
|
||||
|
||||
@Inject
|
||||
GetType(FileContentUtil fileContentUtil) {
|
||||
this.fileContentUtil = fileContentUtil;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apply(ChangeEditResource rsrc)
|
||||
throws ResourceNotFoundException, IOException {
|
||||
return fileContentUtil.getContentType(
|
||||
rsrc.getChangeEdit().getChange().getProject(),
|
||||
rsrc.getChangeEdit().getRevision().get(),
|
||||
rsrc.getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,14 +14,18 @@
|
||||
|
||||
package com.google.gerrit.server.change;
|
||||
|
||||
import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
|
||||
|
||||
import com.google.gerrit.extensions.restapi.BinaryResult;
|
||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||
import com.google.gerrit.reviewdb.client.Project;
|
||||
import com.google.gerrit.server.FileTypeRegistry;
|
||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import org.eclipse.jgit.lib.ObjectLoader;
|
||||
import org.eclipse.jgit.lib.ObjectReader;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
@@ -33,10 +37,13 @@ import java.io.OutputStream;
|
||||
@Singleton
|
||||
public class FileContentUtil {
|
||||
private final GitRepositoryManager repoManager;
|
||||
private final FileTypeRegistry registry;
|
||||
|
||||
@Inject
|
||||
FileContentUtil(GitRepositoryManager repoManager) {
|
||||
FileContentUtil(GitRepositoryManager repoManager,
|
||||
FileTypeRegistry ftr) {
|
||||
this.repoManager = repoManager;
|
||||
this.registry = ftr;
|
||||
}
|
||||
|
||||
public BinaryResult getContent(Project.NameKey project, String revstr,
|
||||
@@ -68,4 +75,32 @@ public class FileContentUtil {
|
||||
repo.close();
|
||||
}
|
||||
}
|
||||
|
||||
public String getContentType(Project.NameKey project, String revstr,
|
||||
String path) throws ResourceNotFoundException, IOException {
|
||||
Repository repo = repoManager.openRepository(project);
|
||||
try {
|
||||
RevWalk rw = new RevWalk(repo);
|
||||
ObjectReader reader = repo.newObjectReader();
|
||||
try {
|
||||
RevCommit commit = rw.parseCommit(repo.resolve(revstr));
|
||||
TreeWalk tw =
|
||||
TreeWalk.forPath(rw.getObjectReader(), path,
|
||||
commit.getTree().getId());
|
||||
if (tw == null) {
|
||||
throw new ResourceNotFoundException();
|
||||
}
|
||||
ObjectLoader blobLoader = reader.open(tw.getObjectId(0), OBJ_BLOB);
|
||||
byte[] raw = blobLoader.isLarge()
|
||||
? null
|
||||
: blobLoader.getCachedBytes();
|
||||
return registry.getMimeType(path, raw).toString();
|
||||
} finally {
|
||||
reader.release();
|
||||
rw.release();
|
||||
}
|
||||
} finally {
|
||||
repo.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
// Copyright (C) 2014 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server.change;
|
||||
|
||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||
import com.google.gerrit.extensions.restapi.RestReadView;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Singleton
|
||||
public class GetContentType implements RestReadView<FileResource> {
|
||||
private final FileContentUtil fileContentUtil;
|
||||
|
||||
@Inject
|
||||
GetContentType(FileContentUtil fileContentUtil) {
|
||||
this.fileContentUtil = fileContentUtil;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apply(FileResource rsrc)
|
||||
throws ResourceNotFoundException, IOException {
|
||||
return fileContentUtil.getContentType(
|
||||
rsrc.getRevision().getControl().getProject().getNameKey(),
|
||||
rsrc.getRevision().getPatchSet().getRevision().get(),
|
||||
rsrc.getPatchKey().get());
|
||||
}
|
||||
}
|
||||
@@ -103,6 +103,7 @@ public class Module extends RestApiModule {
|
||||
put(FILE_KIND, "reviewed").to(PutReviewed.class);
|
||||
delete(FILE_KIND, "reviewed").to(DeleteReviewed.class);
|
||||
get(FILE_KIND, "content").to(GetContent.class);
|
||||
get(FILE_KIND, "type").to(GetContentType.class);
|
||||
get(FILE_KIND, "diff").to(GetDiff.class);
|
||||
|
||||
child(CHANGE_KIND, "edit").to(ChangeEdits.class);
|
||||
@@ -114,6 +115,7 @@ public class Module extends RestApiModule {
|
||||
put(CHANGE_EDIT_KIND, "/").to(ChangeEdits.Put.class);
|
||||
delete(CHANGE_EDIT_KIND).to(ChangeEdits.DeleteContent.class);
|
||||
get(CHANGE_EDIT_KIND, "/").to(ChangeEdits.Get.class);
|
||||
get(CHANGE_EDIT_KIND, "type").to(ChangeEdits.GetType.class);
|
||||
|
||||
install(new FactoryModule() {
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user