summaryrefslogtreecommitdiff
path: root/parser/feed/src/main/java/de/danoeh/antennapod/parser/feed/util/SyndTypeUtils.java
blob: dfd7801a58cba0f7420faf3292db3d3ed4780025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package de.danoeh.antennapod.parser.feed.util;

import android.webkit.MimeTypeMap;
import androidx.annotation.Nullable;
import org.apache.commons.io.FilenameUtils;

/**
 * Utility class for handling MIME-Types of enclosures.
 * */
public class SyndTypeUtils {
    public static final String OCTET_STREAM = "application/octet-stream";

    private SyndTypeUtils() {

    }

    @Nullable
    public static String getMimeType(@Nullable String type, @Nullable String filename) {
        if (isMediaFile(type) && !OCTET_STREAM.equals(type)) {
            return type;
        }
        String filenameType = SyndTypeUtils.getMimeTypeFromUrl(filename);
        if (isMediaFile(filenameType)) {
            return filenameType;
        }
        return type;
    }

    public static boolean isMediaFile(String type) {
        if (type == null) {
            return false;
        } else {
            return type.startsWith("audio/")
                    || type.startsWith("video/")
                    || type.equals("application/ogg")
                    || type.equals("application/octet-stream");
        }
    }

    public static boolean isImageFile(String type) {
        if (type == null) {
            return false;
        } else {
            return type.startsWith("image/");
        }
    }

    /**
     * Should be used if mime-type of enclosure tag is not supported. This
     * method will return the mime-type of the file extension.
     */
    private static String getMimeTypeFromUrl(String url) {
        if (url == null) {
            return null;
        }
        String extension = FilenameUtils.getExtension(url);
        return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
}