Incorrect Content-Type for PDF files

12,112 views
Skip to first unread message

samue...@gmail.com

unread,
Jun 25, 2016, 3:40:26 PM6/25/16
to back{4}app
Hello back{4}app,

First; back4app is great, thank you for this amazing alternative.

Unfortunately PDF files get served with Content-Type=text/plain e.g.
which results in plain text blobs that get shown to the user instead of pdf files (and error messages e.g. under Chrome "Failed to load PDF document").

Upload is fine, I guess you're using S3 so it should normally detect it based on the extension, but to be sure I also
tried new Parse.File(filename, file, "application/pdf");  as a test without luck.

Images work fine and after downloading the blob I'm able to open the pdf file correctly, so I don't think it is my setup.

Kind regards

samue...@gmail.com

unread,
Jun 25, 2016, 9:13:50 PM6/25/16
to back{4}app
As a temporary solution I'm using a quick-and-dirty PHP script to get the files with the correct mime type, maybe it helps somebody:

<?php
// call it like the following
// https://example.com/get.php?path=e4YQYTfDTeW9xmA2sGEd9WL7PWomHoGgC4xrNKqI/be1da227fa380c8d057edbcf19cc8e14_pdf.pdf
$url
= isset($_GET['path'])?$_GET['path']:null;
if (!$url) exit;


$content
= file_get_contents("https://parsefiles.back4app.com/$url");


// from
// http://php.net/manual/de/function.mime-content-type.php
$mime_types
= array(


   
'txt' => 'text/plain',
   
'htm' => 'text/html',
   
'html' => 'text/html',
   
'php' => 'text/html',
   
'css' => 'text/css',
   
'js' => 'application/javascript',
   
'json' => 'application/json',
   
'xml' => 'application/xml',
   
'swf' => 'application/x-shockwave-flash',
   
'flv' => 'video/x-flv',


   
// images
   
'png' => 'image/png',
   
'jpe' => 'image/jpeg',
   
'jpeg' => 'image/jpeg',
   
'jpg' => 'image/jpeg',
   
'gif' => 'image/gif',
   
'bmp' => 'image/bmp',
   
'ico' => 'image/vnd.microsoft.icon',
   
'tiff' => 'image/tiff',
   
'tif' => 'image/tiff',
   
'svg' => 'image/svg+xml',
   
'svgz' => 'image/svg+xml',


   
// archives
   
'zip' => 'application/zip',
   
'rar' => 'application/x-rar-compressed',
   
'exe' => 'application/x-msdownload',
   
'msi' => 'application/x-msdownload',
   
'cab' => 'application/vnd.ms-cab-compressed',


   
// audio/video
   
'mp3' => 'audio/mpeg',
   
'qt' => 'video/quicktime',
   
'mov' => 'video/quicktime',


   
// adobe
   
'pdf' => 'application/pdf',
   
'psd' => 'image/vnd.adobe.photoshop',
   
'ai' => 'application/postscript',
   
'eps' => 'application/postscript',
   
'ps' => 'application/postscript',


   
// ms office
   
'doc' => 'application/msword',
   
'rtf' => 'application/rtf',
   
'xls' => 'application/vnd.ms-excel',
   
'ppt' => 'application/vnd.ms-powerpoint',


   
// open office
   
'odt' => 'application/vnd.oasis.opendocument.text',
   
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);


$ext
= strtolower(array_pop(explode('.', basename($url))));
if (array_key_exists($ext, $mime_types)) {
    $mime
= $mime_types[$ext];
} else {
    $mime
= 'application/octet-stream';
}


header
("Content-Type: $mime");


if (isset($_GET['download'])) {
   
if ($_GET['download']) {
        $download
= $_GET['download'];
   
} else {
        $download
= $url;
   
}
    header
('Content-Disposition: attachment; filename="'. basename($download) .'"');
}
if ($content) {
   
die($content);
}



samue...@gmail.com

unread,
Jun 25, 2016, 9:25:28 PM6/25/16
to back{4}app
Please don't use this script for production, XSS attacks get easier with this and the mime type is only detected using the extension (instead of analyzing the file's content).
I hope there is a way to get the correct Content-Type in the future.

Davi Macêdo

unread,
Jun 28, 2016, 2:22:29 PM6/28/16
to back{4}app
Hi,

This is an issue of Parse Server and Parse Dashboard Open Source project, but it is already solved in the latest version. We are updating all apps now and then you can try again.

Best!

samue...@gmail.com

unread,
Jul 4, 2016, 7:40:22 PM7/4/16
to back{4}app
Hello,

thanks for the fast reply!
Hopefully the deployment runs smoothly. :)

looks to me like https://github.com/ParsePlatform/parse-server/issues/1900 for anyone who's interested

Kind regards!

Davi Macêdo

unread,
Jul 6, 2016, 11:40:02 PM7/6/16
to back{4}app
Hi.

Did you try again? Is it fixed for you?

Best!

samue...@gmail.com

unread,
Jul 7, 2016, 4:39:45 PM7/7/16
to back{4}app
Hello,

I've updated the Javascript Parse SDK and uploaded a pdf file, but
unfortunately it still gives me the error "Failed to load PDF document",
although I'm able to open the file after downloading it (due to the Content-Type).

Do others have those problems too or am I the only one who uploads something "unusual" like a pdf file?

// pseudo-code
var data = {
    filename
: 'test.pdf',
    file
: File // File object
}
console
.log(data.file.type); // === 'application/pdf'
new Parse.File(data.filename, data.file, data.file.type)

Is there something inherently wrong with the code? I'm using Parse v1.8.5

Kind regards


rabbiga...@gmail.com

unread,
Jul 8, 2016, 8:56:35 AM7/8/16
to back{4}app
No, I'm having this problem l, too. If I understand correctly, the problem is not with back4app but with Parse Server itself. We are waiting for that correction.

Davi Macêdo

unread,
Jul 11, 2016, 8:23:36 PM7/11/16
to back{4}app
Hi, Guys.

Yes. It is a problem regarding Parse Server and Parse Server Dashboard. But it seems that in Parse Server it is already fixed. Waiting only the solution in dashboard.

By this way, if you upload the file though Dashboard you will still experience the problem, but if you upload through cloud code, it should be fine.

In the case of @samuelant37, you have to specify the content-type. You can dot it by changing this:

new Parse.File(data.filename, data.file, data.file.type)


To this:

new Parse.File(data.filename, data.file, 'text/pdf')


Please, let me know.


Best!

samue...@gmail.com

unread,
Jul 14, 2016, 6:34:12 AM7/14/16
to back{4}app
Hello,

Maybe my pseudo code confused you, data.file.type is application/pdf (and text/pdf isn't the correct mime type for PDFs).

var file_obj = new Parse.File(
data
.filename, data.file, 'application/pdf'
);




samue...@gmail.com

unread,
Jul 14, 2016, 8:20:45 AM7/14/16
to back{4}app
Hello,

Ok, I've solved the problem for me.
The problem is in the Parse SDK for Javascript, this explains why the Parse Server update didn't help.

I've debugged Parse SDK and found the root of the issue, maybe someone is interested in the solution I've come up with:

Kind regards

Davi Macêdo

unread,
Jul 19, 2016, 9:07:36 AM7/19/16
to back{4}app
Good to know it and thank you for the contribution :)
Reply all
Reply to author
Forward
0 new messages