Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MIME type for .bib should be 'text/plain'? #144

Open
brian-mit opened this issue Apr 8, 2019 · 3 comments
Open

MIME type for .bib should be 'text/plain'? #144

brian-mit opened this issue Apr 8, 2019 · 3 comments
Labels

Comments

@brian-mit
Copy link

I'm using the Media Library to store my bibtex files and after upgrading Wordpress core from 4.9.9 to 5.1.1 and the papercite plugin from 0.5.15 to 0.5.18, I'm having the following issues:

  1. I can no longer upload .bib files and get the error message 'This file type is not permitted for security reasons.' I was able to get around that using the upload_mimes filter, but I had to use 'text/plain' as the mime type for .bib files as 'application/x-bibtex' didn't work. This probably isn't your problem, I'm just including it as an FYI.
  2. After uploading my bibtex file to the Media Library, it doesn't seem to be found and processed correctly. I think it has to do with its mime type. As a test, I changed line 418 in papercite.php from
    $bibFile = $this->getDataFile("$biburi", "bib", "bib", "application/x-bibtex", $options);
    to
    $bibFile = $this->getDataFile("$biburi", "bib", "bib", "text/plain", $options);
    and things seem to work.

I'm not sure, but it looks like line 2558 in wp-includes/functions.php might be where the mime type of a file is found:

$finfo     = finfo_open( FILEINFO_MIME_TYPE );
$real_mime = finfo_file( $finfo, $file );

I check my bibtex file using the following command:

$ php -r '$finfo = finfo_open(FILEINFO_MIME_TYPE); echo finfo_file($finfo, "myBibtexFile.bib");'

and got the response 'text/plain'.

If I create the directory wp-content/papercite-data/bib and put my bibtex files in there, things seem to work, so I can do that as a workaround. Being able to use the Media Library is more convenient though.

@digfish
Copy link
Collaborator

digfish commented Apr 9, 2019

Thanks for letting us know about this issue. It appears the "problem" is around from Wordpress 5.0.1 onwards, and was propagated to 4.9.9 also. according to the Wordpress tracker. I found a github gist wirth a workaround here and adapted it for the bibtex case and added at the papercite-wp-plugin.php (I'm using version 0.5.20, which was pushed by me, @digfish). The code is below

/**
 * by digfish (09 Apr 2019)
 * Restore .bib upload functionality in Media Library for WordPress 4.9.9 and up
 * adapted from https://gist.github.com/rmpel/e1e2452ca06ab621fe061e0fde7ae150
 */
add_filter('wp_check_filetype_and_ext', function($values, $file, $filename, $mimes) {
    if ( extension_loaded( 'fileinfo' ) ) {
        // with the php-extension, a bib file is issues type text/plain so we fix that back to
        // application/x-bibtex by trusting the file extension.
        $finfo     = finfo_open( FILEINFO_MIME_TYPE );
        $real_mime = finfo_file( $finfo, $file );
        finfo_close( $finfo );
        if ( $real_mime === 'text/plain' && preg_match( '/\.(bib)$/i', $filename ) ) {
            $values['ext']  = 'bib';
            $values['type'] = 'application/x-bibtex';
        }
    } else {
        // without the php- extension, we probably don't have the issue at all, but just to be sure...
        if ( preg_match( '/\.(bib)$/i', $filename ) ) {
            $values['ext']  = 'bib';
            $values['type'] = 'application/x-bibtex';
        }
    }
    return $values;
}, PHP_INT_MAX, 4);

You can add it to your theme's functions.php until a new commit with the fix is pushed.

Other threads that I stumbled upon regarding this issue in case anyone comes to this topic by Google results:

@digfish digfish added the bug label Apr 9, 2019
digfish added a commit that referenced this issue Apr 9, 2019
@digfish
Copy link
Collaborator

digfish commented Apr 9, 2019

Pushed the last commit with the fix (v. 0.5.22)

digfish added a commit to digfish/papercite that referenced this issue Apr 9, 2019
@brian-mit
Copy link
Author

Thanks, that seems to work for me. One note in case anyone else finds this: I needed to re-upload my bibtex files. After adding the above code to functions.php, the bibtex files that were currently in my media library still did not seem to be found. After re-uploading them though, they were.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants