bug5127: fixed adding Youtube and Vimeo media to an event.

- extended MimeType test for new centralized functionality of extracting MimeTypes from URL.
This commit is contained in:
Udo Wessels
2023-01-23 18:55:15 +01:00
parent 232e991fa0
commit 43e6743119
4 changed files with 73 additions and 65 deletions
@@ -16,6 +16,9 @@ public enum MimeType {
mp4panorama(MediaType.video, MediaSubType.mp4, "mp4"),
mp4panoramaflip(MediaType.video, MediaSubType.mp4, "mp4"),
mov(MediaType.video, MediaSubType.mp4, "mov|quicktime");
private final static String YOUTUBE_REGEX = "((http(s)?:\\/\\/)?)(www\\.)?((youtube\\.com\\/)|(youtu.be\\/))[\\S]+";
public final static String VIMEO_REGEX = "(?:http|https)?:?\\/?\\/?(?:www\\.)?(?:player\\.)?vimeo\\.com\\/(?:channels\\/(?:\\w+\\/)?|groups\\/(?:[^\\/]*)\\/videos\\/|video\\/|)(\\d+)(?:|\\/\\?)";
public final MediaType mediaType;
public final MediaSubType mediaSubType;
@@ -81,27 +84,45 @@ public enum MimeType {
*/
public static MimeType byExtension(String fileName) {
final MimeType result;
int dotPos = fileName.lastIndexOf('.');
if (dotPos >= 0) {
String fileEnding = fileName.substring(dotPos + 1).toLowerCase();
MimeType bestMatch = unknown;
if (fileEnding != null) {
for (MimeType mimeType : MimeType.values()) {
if (!mimeType.getEndingPattern().isEmpty()) {
boolean matchFound = fileEnding.toLowerCase().matches(mimeType.endingPattern);
if (matchFound) {
bestMatch = mimeType;
break;
if (fileName == null) {
result = unknown;
} else {
int dotPos = fileName.trim().lastIndexOf('.');
if (dotPos >= 0) {
String fileEnding = fileName.trim().substring(dotPos + 1).toLowerCase();
MimeType bestMatch = unknown;
if (fileEnding != null) {
for (MimeType mimeType : MimeType.values()) {
if (!mimeType.getEndingPattern().isEmpty()) {
boolean matchFound = fileEnding.toLowerCase().matches(mimeType.endingPattern);
if (matchFound) {
bestMatch = mimeType;
break;
}
}
}
}
result = bestMatch;
} else {
result = unknown;
}
result = bestMatch;
} else {
result = unknown;
}
return result;
}
public static MimeType extractFromUrl(String url) {
final MimeType mimeType;
if (url == null) {
mimeType = MimeType.unknown;
} else if (url.trim().matches(YOUTUBE_REGEX)) {
mimeType = MimeType.youtube;
} else if (url.trim().matches(VIMEO_REGEX)) {
mimeType = MimeType.vimeo;
} else {
mimeType = byExtension(url);
}
return mimeType;
}
/**
* Gets a mime type based on content type, e.g. image/jpeg, video/mp4, ...