mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-07-04 09:49:26 +02:00
Expose content type resolver api to plugins (#1752)
Expose an api which makes it easy to detect the content type of files. The api is based on the spotter api, but does not expose spotter classes. Co-authored-by: René Pfeuffer <rene.pfeuffer@cloudogu.com>
This commit is contained in:
@@ -24,7 +24,6 @@
|
||||
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.github.sdorra.spotter.ContentType;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
@@ -33,7 +32,8 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.NotFoundException;
|
||||
import sonia.scm.api.v2.ContentTypeResolver;
|
||||
import sonia.scm.io.ContentType;
|
||||
import sonia.scm.io.ContentTypeResolver;
|
||||
import sonia.scm.repository.NamespaceAndName;
|
||||
import sonia.scm.repository.api.RepositoryService;
|
||||
import sonia.scm.repository.api.RepositoryServiceFactory;
|
||||
@@ -61,10 +61,12 @@ public class ContentResource {
|
||||
private static final int HEAD_BUFFER_SIZE = 1024;
|
||||
|
||||
private final RepositoryServiceFactory serviceFactory;
|
||||
private final ContentTypeResolver contentTypeResolver;
|
||||
|
||||
@Inject
|
||||
public ContentResource(RepositoryServiceFactory serviceFactory) {
|
||||
public ContentResource(RepositoryServiceFactory serviceFactory, ContentTypeResolver contentTypeResolver) {
|
||||
this.serviceFactory = serviceFactory;
|
||||
this.contentTypeResolver = contentTypeResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,10 +206,10 @@ public class ContentResource {
|
||||
}
|
||||
|
||||
private void appendContentHeader(String path, byte[] head, Response.ResponseBuilder responseBuilder) {
|
||||
ContentType contentType = ContentTypeResolver.resolve(path, head);
|
||||
ContentType contentType = contentTypeResolver.resolve(path, head);
|
||||
responseBuilder.header("Content-Type", contentType.getRaw());
|
||||
contentType.getLanguage().ifPresent(
|
||||
language -> responseBuilder.header(ProgrammingLanguages.HEADER, ProgrammingLanguages.getValue(language))
|
||||
language -> responseBuilder.header(ProgrammingLanguages.HEADER, language)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,10 +24,9 @@
|
||||
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.github.sdorra.spotter.Language;
|
||||
import com.google.inject.Inject;
|
||||
import de.otto.edison.hal.Links;
|
||||
import sonia.scm.api.v2.ContentTypeResolver;
|
||||
import sonia.scm.io.ContentTypeResolver;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.api.DiffFile;
|
||||
import sonia.scm.repository.api.DiffLine;
|
||||
@@ -49,10 +48,12 @@ import static de.otto.edison.hal.Links.linkingTo;
|
||||
class DiffResultToDiffResultDtoMapper {
|
||||
|
||||
private final ResourceLinks resourceLinks;
|
||||
private final ContentTypeResolver contentTypeResolver;
|
||||
|
||||
@Inject
|
||||
DiffResultToDiffResultDtoMapper(ResourceLinks resourceLinks) {
|
||||
DiffResultToDiffResultDtoMapper(ResourceLinks resourceLinks, ContentTypeResolver contentTypeResolver) {
|
||||
this.resourceLinks = resourceLinks;
|
||||
this.contentTypeResolver = contentTypeResolver;
|
||||
}
|
||||
|
||||
public DiffResultDto mapForIncoming(Repository repository, DiffResult result, String source, String target) {
|
||||
@@ -154,8 +155,8 @@ class DiffResultToDiffResultDtoMapper {
|
||||
dto.setOldPath(oldPath);
|
||||
dto.setOldRevision(file.getOldRevision());
|
||||
|
||||
Optional<Language> language = ContentTypeResolver.resolve(path).getLanguage();
|
||||
language.ifPresent(value -> dto.setLanguage(ProgrammingLanguages.getValue(value)));
|
||||
Optional<String> language = contentTypeResolver.resolve(path).getLanguage();
|
||||
language.ifPresent(dto::setLanguage);
|
||||
|
||||
List<DiffResultDto.HunkDto> hunks = new ArrayList<>();
|
||||
for (Hunk hunk : file) {
|
||||
|
||||
@@ -21,28 +21,13 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.github.sdorra.spotter.Language;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
final class ProgrammingLanguages {
|
||||
|
||||
static final String HEADER = "X-Programming-Language";
|
||||
|
||||
private static final String DEFAULT = "text";
|
||||
|
||||
private ProgrammingLanguages() {
|
||||
}
|
||||
|
||||
static String getValue(Language language) {
|
||||
Optional<String> aceMode = language.getAceMode();
|
||||
if (!aceMode.isPresent()) {
|
||||
Optional<String> codemirrorMode = language.getCodemirrorMode();
|
||||
return codemirrorMode.orElse(DEFAULT);
|
||||
}
|
||||
return aceMode.get();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.io;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class DefaultContentType implements ContentType {
|
||||
|
||||
private static final String DEFAULT_LANG_MODE = "text";
|
||||
|
||||
private final com.github.sdorra.spotter.ContentType contentType;
|
||||
|
||||
DefaultContentType(com.github.sdorra.spotter.ContentType contentType) {
|
||||
this.contentType = contentType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrimary() {
|
||||
return contentType.getPrimary();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSecondary() {
|
||||
return contentType.getSecondary();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRaw() {
|
||||
return contentType.getRaw();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isText() {
|
||||
return contentType.isText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getLanguage() {
|
||||
return contentType.getLanguage().map(language -> {
|
||||
Optional<String> aceMode = language.getAceMode();
|
||||
return aceMode.orElseGet(() -> language.getCodemirrorMode().orElse(DEFAULT_LANG_MODE));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -22,13 +22,12 @@
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.api.v2;
|
||||
package sonia.scm.io;
|
||||
|
||||
import com.github.sdorra.spotter.ContentType;
|
||||
import com.github.sdorra.spotter.ContentTypeDetector;
|
||||
import com.github.sdorra.spotter.Language;
|
||||
|
||||
public final class ContentTypeResolver {
|
||||
public final class DefaultContentTypeResolver implements ContentTypeResolver {
|
||||
|
||||
private static final ContentTypeDetector PATH_BASED = ContentTypeDetector.builder()
|
||||
.defaultPathBased().boost(Language.MARKDOWN)
|
||||
@@ -38,14 +37,13 @@ public final class ContentTypeResolver {
|
||||
.defaultPathAndContentBased().boost(Language.MARKDOWN)
|
||||
.bestEffortMatch();
|
||||
|
||||
private ContentTypeResolver() {
|
||||
@Override
|
||||
public DefaultContentType resolve(String path) {
|
||||
return new DefaultContentType(PATH_BASED.detect(path));
|
||||
}
|
||||
|
||||
public static ContentType resolve(String path) {
|
||||
return PATH_BASED.detect(path);
|
||||
}
|
||||
|
||||
public static ContentType resolve(String path, byte[] contentPrefix) {
|
||||
return PATH_AND_CONTENT_BASED.detect(path, contentPrefix);
|
||||
@Override
|
||||
public DefaultContentType resolve(String path, byte[] contentPrefix) {
|
||||
return new DefaultContentType(PATH_AND_CONTENT_BASED.detect(path, contentPrefix));
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,8 @@ import sonia.scm.group.GroupManagerProvider;
|
||||
import sonia.scm.group.xml.XmlGroupDAO;
|
||||
import sonia.scm.initialization.DefaultInitializationFinisher;
|
||||
import sonia.scm.initialization.InitializationFinisher;
|
||||
import sonia.scm.io.ContentTypeResolver;
|
||||
import sonia.scm.io.DefaultContentTypeResolver;
|
||||
import sonia.scm.metrics.MeterRegistryProvider;
|
||||
import sonia.scm.migration.MigrationDAO;
|
||||
import sonia.scm.net.SSLContextProvider;
|
||||
@@ -290,6 +292,8 @@ class ScmServletModule extends ServletModule {
|
||||
bind(IndexQueue.class, DefaultIndexQueue.class);
|
||||
bind(SearchEngine.class, LuceneSearchEngine.class);
|
||||
bind(IndexLogStore.class, DefaultIndexLogStore.class);
|
||||
|
||||
bind(ContentTypeResolver.class).to(DefaultContentTypeResolver.class);
|
||||
}
|
||||
|
||||
private <T> void bind(Class<T> clazz, Class<? extends T> defaultImplementation) {
|
||||
|
||||
Reference in New Issue
Block a user