mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-04 20:45:52 +01: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:
71
scm-core/src/main/java/sonia/scm/io/ContentType.java
Normal file
71
scm-core/src/main/java/sonia/scm/io/ContentType.java
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Detected type of content.
|
||||
*
|
||||
* @since 2.23.0
|
||||
*/
|
||||
public interface ContentType {
|
||||
|
||||
/**
|
||||
* Returns the primary part of the content type (e.g.: text of text/plain).
|
||||
*
|
||||
* @return primary content type part
|
||||
*/
|
||||
String getPrimary();
|
||||
|
||||
/**
|
||||
* Returns the secondary part of the content type (e.g.: plain of text/plain).
|
||||
*
|
||||
* @return secondary content type part
|
||||
*/
|
||||
String getSecondary();
|
||||
|
||||
/**
|
||||
* Returns the raw presentation of the content type (e.g.: text/plain).
|
||||
*
|
||||
* @return raw presentation
|
||||
*/
|
||||
String getRaw();
|
||||
|
||||
/**
|
||||
* Returns {@code true} if the content type is text based.
|
||||
*
|
||||
* @return {@code true} for text content
|
||||
*/
|
||||
boolean isText();
|
||||
|
||||
/**
|
||||
* Returns an optional with the programming language
|
||||
* or empty if the content is not programming language.
|
||||
*
|
||||
* @return programming language or empty
|
||||
*/
|
||||
Optional<String> getLanguage();
|
||||
}
|
||||
52
scm-core/src/main/java/sonia/scm/io/ContentTypeResolver.java
Normal file
52
scm-core/src/main/java/sonia/scm/io/ContentTypeResolver.java
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* ContentTypeResolver is able to detect the {@link ContentType} of files based on their path and (optinally) a few starting bytes. These files do not have to be real files on the file system, but can be hypothetical constructs ("What content type is most probable for a file named like this").
|
||||
*
|
||||
* @since 2.23.0
|
||||
*/
|
||||
public interface ContentTypeResolver {
|
||||
|
||||
/**
|
||||
* Detects the {@link ContentType} of the given path, by only using path based strategies.
|
||||
*
|
||||
* @param path path of the file
|
||||
*
|
||||
* @return {@link ContentType} of path
|
||||
*/
|
||||
ContentType resolve(String path);
|
||||
|
||||
/**
|
||||
* Detects the {@link ContentType} of the given path, by using path and content based strategies.
|
||||
*
|
||||
* @param path path of the file
|
||||
* @param contentPrefix first few bytes of the content
|
||||
*
|
||||
* @return {@link ContentType} of path and content prefix
|
||||
*/
|
||||
ContentType resolve(String path, byte[] contentPrefix);
|
||||
}
|
||||
Reference in New Issue
Block a user