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:
@@ -1433,6 +1433,29 @@ specified file was deleted in the change edit "`204 No Content`" is returned.
|
||||
RnJvbSA3ZGFkY2MxNTNmZGVhMTdhYTg0ZmYzMmE2ZTI0NWRiYjY...
|
||||
----
|
||||
|
||||
[[get-edit-file-mime-type]]
|
||||
=== Retrieve file content MIME type from Change Edit
|
||||
--
|
||||
'GET /changes/link:#change-id[\{change-id\}]/edit/path%2fto%2ffile/type
|
||||
--
|
||||
|
||||
Retrieves content MIME type of a file from a change edit.
|
||||
|
||||
.Request
|
||||
----
|
||||
GET /changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/edit/foo%2fbar%2fbaz%2fqux.txt/type HTTP/1.0
|
||||
----
|
||||
|
||||
.Response
|
||||
----
|
||||
HTTP/1.1 200 OK
|
||||
Content-Disposition: attachment
|
||||
Content-Type: application/json;charset=UTF-8
|
||||
|
||||
)]}'
|
||||
"text/plain"
|
||||
----
|
||||
|
||||
[[get-edit-message]]
|
||||
=== Retrieve commit message from Change Edit or current patch set of the change
|
||||
--
|
||||
@@ -2790,6 +2813,31 @@ The content is returned as base64 encoded string.
|
||||
Ly8gQ29weXJpZ2h0IChDKSAyMDEwIFRoZSBBbmRyb2lkIE9wZW4gU291cmNlIFByb2plY...
|
||||
----
|
||||
|
||||
[[get-content-type]]
|
||||
=== Get Content MIME Type
|
||||
--
|
||||
'GET /changes/link:#change-id[\{change-id\}]/revisions/link:#revision-id[\{revision-id\}]/files/link:#file-id[\{file-id\}]/type'
|
||||
--
|
||||
|
||||
Gets the content MIME type of a file from a certain revision.
|
||||
|
||||
.Request
|
||||
----
|
||||
GET /changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/revisions/674ac754f91e64a0efb8087e59a176484bd534d1/files/readme.txt/type HTTP/1.0
|
||||
----
|
||||
|
||||
The content MIME type is returned as string.
|
||||
|
||||
.Response
|
||||
----
|
||||
HTTP/1.1 200 OK
|
||||
Content-Disposition: attachment
|
||||
Content-Type: application/json;charset=UTF-8
|
||||
|
||||
)]}'
|
||||
"text/plain"
|
||||
----
|
||||
|
||||
[[get-diff]]
|
||||
=== Get Diff
|
||||
--
|
||||
|
||||
@@ -484,6 +484,18 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
.isEqualTo(StringUtils.newStringUtf8(CONTENT_NEW2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFileContentTypeRest() throws Exception {
|
||||
Put.Input in = new Put.Input();
|
||||
in.content = RestSession.newRawInput(CONTENT_NEW);
|
||||
assertThat(adminSession.putRaw(urlEditFile(), in.content).getStatusCode())
|
||||
.isEqualTo(SC_NO_CONTENT);
|
||||
RestResponse r = adminSession.get(urlEditFileContentType());
|
||||
assertThat(r.getStatusCode()).isEqualTo(SC_OK);
|
||||
String res = newGson().fromJson(r.getReader(), String.class);
|
||||
assertThat(res).isEqualTo("application/octet-stream");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFileNotFoundRest() throws Exception {
|
||||
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
|
||||
@@ -606,6 +618,13 @@ public class ChangeEditIT extends AbstractDaemonTest {
|
||||
+ FILE_NAME;
|
||||
}
|
||||
|
||||
private String urlEditFileContentType() {
|
||||
return urlEdit()
|
||||
+ "/"
|
||||
+ FILE_NAME
|
||||
+ "/type";
|
||||
}
|
||||
|
||||
private String urlGetFiles() {
|
||||
return urlEdit()
|
||||
+ "?list";
|
||||
|
||||
@@ -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