Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@rien333
Copy link
Contributor

@rien333 rien333 commented Aug 2, 2025

This PR removes a broken attempt to handle extension mismatches.

Problem

The code currently tries to verify if a file's mimetype (as determined by Android's ContentResolver) matches the mimetype inferred from the file's extension. If there is a mismatch, that might indicate that the file has the wrong extension, and the correct extension is appended.

String mimeType = applicationContext.getContentResolver().getType(contentUri);
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
// this variable is misleadingly named, and should read 'mimetypeFromFilename'
String extensionFromFilename = mimeTypeMap.getMimeTypeFromExtension(
    MimeTypeMap.getFileExtensionFromUrl(fileName)
);

if (extensionFromFilename == null || !extensionFromFilename.equals(mimeType)) {
    fileName += "." + mimeTypeMap.getExtensionFromMimeType(mimeType);
}

However, MimeTypeMap.getFileExtensionFromUrl(fileName) will always returns null,
since getFileExtensionFromUrl takes URLs to always contain a /,
but *nix forbids / in filenames.

As a result, this check incorrectly appends an extra extension to
every filename it encounters (e.g. example.mp3.mp3).

Why remove the check

While the code is easily fixed, I think the check is better left out altogether, since in its current form, it is probably a truism. The value of mimeType is determined by applicationContext.getContentResolver().getType(contentUri), which internally calls mimeTypeMap.getMimeTypeFromExtension on the uri's extension.1 This mimeType variable is then compared to the output of the exact same function (mimeTypeMap.getMimeTypeFromExtension). Hard for that to be wrong.

Better leave handling misleading extensions to Kodi. In my tests, Kodi and standalone ffmeg are more than capable of handling files with incorrect extensions over http. I tested this by using Kore to play back a mpeg encoded file with an incorrect extension (.flac).

Footnotes

  1. Tracing the calls of getType(), I see that it ultimately just calls getTypeForName() in ExternalStorageProvider.java, which then calls getMimeTypeFromExtension.

The current implementation is broken, and probably always has
been. While easily fixed, the procedure in its current form is
basically a truism.

Better leave handling misleading extensions to Kodi. In my tests, Kodi
and standalone ffmeg are more than capable of handling files with
incorrect extension over http.
@rien333
Copy link
Contributor Author

rien333 commented Aug 2, 2025

I came across this when I was debugging some other stuff in Kore. Basically, my kodi log would always show files with two extensions.

@SyncedSynapse
Copy link
Member

Thank you for the PR. Unfortunately i'm not able to validate it in the next 2 weeks, but i will look into it afterwards. Best regards

@rien333
Copy link
Contributor Author

rien333 commented Aug 4, 2025

All right, let me know if something is unclear! And thanks for maintaining Kore, I've it used almost every day for years on end.

@rien333
Copy link
Contributor Author

rien333 commented Aug 15, 2025

I accidentally added some commits pertaining to an experiment here, will remove them in a bit! Sorry.

@SyncedSynapse SyncedSynapse merged commit b68b7e1 into xbmc:master Aug 22, 2025
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants