Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 430c6e6e authored by Arthur Hung's avatar Arthur Hung
Browse files

Get mime type from getDocumentType instead of from file name

DownloadProvider and FileSystemProvide will decide mime type
in different way, so if some function like getDocumentMetadata
will try to get mime type and do some loading. It may reurn unsupported

Bug: 80454526
Test: Download jpg file, remove .jpg extension, get info
Change-Id: Ia8d237181eba51005981ba46a1be3a43af0f249c
parent 35f12ed1
Loading
Loading
Loading
Loading
+15 −24
Original line number Diff line number Diff line
@@ -136,7 +136,7 @@ public abstract class FileSystemProvider extends DocumentsProvider {
            return null;
        }

        String mimeType = getTypeForFile(file);
        String mimeType = getDocumentType(documentId);
        if (!MetadataReader.isSupportedMimeType(mimeType)) {
            return null;
        }
@@ -418,7 +418,19 @@ public abstract class FileSystemProvider extends DocumentsProvider {
    @Override
    public String getDocumentType(String documentId) throws FileNotFoundException {
        final File file = getFileForDocId(documentId);
        return getTypeForFile(file);
        if (file.isDirectory()) {
            return Document.MIME_TYPE_DIR;
        } else {
            final int lastDot = documentId.lastIndexOf('.');
            if (lastDot >= 0) {
                final String extension = documentId.substring(lastDot + 1).toLowerCase();
                final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
                if (mime != null) {
                    return mime;
                }
            }
            return MIMETYPE_OCTET_STREAM;
        }
    }

    @Override
@@ -483,7 +495,7 @@ public abstract class FileSystemProvider extends DocumentsProvider {
            }
        }

        final String mimeType = getTypeForFile(file);
        final String mimeType = getDocumentType(docId);
        final String displayName = file.getName();
        if (mimeType.startsWith("image/")) {
            flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
@@ -510,31 +522,10 @@ public abstract class FileSystemProvider extends DocumentsProvider {
        return row;
    }

    private static String getTypeForFile(File file) {
        if (file.isDirectory()) {
            return Document.MIME_TYPE_DIR;
        } else {
            return getTypeForName(file.getName());
        }
    }

    protected boolean typeSupportsMetadata(String mimeType) {
        return MetadataReader.isSupportedMimeType(mimeType);
    }

    private static String getTypeForName(String name) {
        final int lastDot = name.lastIndexOf('.');
        if (lastDot >= 0) {
            final String extension = name.substring(lastDot + 1).toLowerCase();
            final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
            if (mime != null) {
                return mime;
            }
        }

        return MIMETYPE_OCTET_STREAM;
    }

    protected final File getFileForDocId(String docId) throws FileNotFoundException {
        return getFileForDocId(docId, false);
    }