Skip to content

Double "Content-Type" headers in ApiController::getThumbnail #12712

Closed

Description

@oparoz

This is the code which returns a thumbnail in the Files' ApiController

$preview = new Preview('', 'files', urldecode($file), $x, $y, true);
echo($preview->showPreview('image/png'));
return new DownloadResponse(urldecode($file).'.png', 'image/png');

showPreview() already returns headers, so the client never gets the second header
I'm not sure what echo() around that void function is supposed to do?

Solutions

  • Remove the DownloadResponse and fix the headers of OC_Image::show() if better ones are needed.
  • Use OC_Preview::getPreview(), add headers locally via OCP\Response and header() and push that to the client

Next steps

  • Get issue confirmation
  • Get fix in
  • Get fix validated

Activity

  1. oparoz commented on Dec 8, 2014

    @oparoz
    ContributorAuthor

    @georgehrke - You might know what's best to do in this situation.

  2. enoch85 commented on Feb 24, 2015

    @enoch85

    any update on this?

  3. rullzer commented on Mar 16, 2015

    @rullzer
    Contributor

    I ran into a similar situation with the avatars. I'll prepare a PR that fixes this.

  4. self-assigned this
    on Mar 16, 2015
  5. added this to the milestone on Mar 16, 2015
  6. oparoz commented on Mar 22, 2015

    @oparoz
    ContributorAuthor

    @enoch85 - On stable8, you could try simply replacing this

    echo($preview->showPreview('image/png'));
    return new DownloadResponse(urldecode($file).'.png', 'image/png');
    

    with this

    $preview->showPreview('image/png');
    
  7. enoch85 commented on Mar 22, 2015

    @enoch85

    So now the code looks like this:

    $preview = new Preview('', 'files', urldecode($file), $x, $y, true);
                $preview->showPreview('image/png');
            } catch (\Exception $e) {
                return new JSONResponse('File not found.', Http::STATUS_NOT_FOUND);
    

    And I get this:

    Error   PHP Cannot modify header information - headers already sent by (output started at /var/www/owncloud/lib/private/image.php:249) at /var/www/owncloud/lib/private/appframework/app.php#116    2015-03-22T20:17:16+00:00
    Error   PHP Cannot modify header information - headers already sent by (output started at /var/www/owncloud/lib/private/image.php:249) at /var/www/owncloud/lib/private/appframework/app.php#104    2015-03-22T20:17:16+00:00
    Error   PHP Cannot modify header information - headers already sent by (output started at /var/www/owncloud/lib/private/image.php:249) at /var/www/owncloud/lib/private/appframework/app.php#104    2015-03-22T20:17:16+00:00
    
  8. enoch85 commented on Mar 22, 2015

    @enoch85

    @oparoz Any other suggestion?

  9. oparoz commented on Mar 22, 2015

    @oparoz
    ContributorAuthor

    @enoch85 - Ah, yes, my bad... It's in a controller, so you we can't do it this way because it's the one sending the headers.
    Let's try again, by replacing the original

    $preview = new Preview('', 'files', urldecode($file), $x, $y, true);
    echo($preview->showPreview('image/png'));
    return new DownloadResponse(urldecode($file).'.png', 'image/png');
    

    with this

    $preview = new Preview('', 'files', urldecode($file), $x, $y, true);
    $previewData->getPreview();
    $response = new JSONResponse($previewData->data());
    $response->setHeaders(
        [
            'Cache-Control' => 'no-cache, must-revalidate',
            'Content-type'  => 'image/png; charset=utf-8'
        ]
    );
    \OCP\Response::setContentDispositionHeader(basename(urldecode($file).'.png'), 'attachment');
    
    return $response;
    

    I'm not sure if more headers are required and this might be fragile, in case bad previews are sent to the class, but if it works, it wouldn't be worst than the original solution.

  10. oparoz commented on Mar 22, 2015

    @oparoz
    ContributorAuthor

    Just added another header

  11. enoch85 commented on Mar 22, 2015

    @enoch85
    try {
                $preview = new Preview('', 'files', urldecode($file), $x, $y, true);
                $response = new JSONResponse($preview->data());
                $response->setHeaders(
                        [
                        'Cache-Control' => 'no-cache, must-revalidate',
                            'Content-type'  => 'image/png; charset=utf-8'
                        ]
    );
    

    Gives

    Error   PHP Call to undefined method OC\Preview::data() at /var/www/owncloud/apps/files/controller/apicontroller.php#53 2015-03-22T20:54:59+00:00
    Error   PHP Call to undefined method OC\Preview::data() at /var/www/owncloud/apps/files/controller/apicontroller.php#53 2015-03-22T20:54:59+00:00
    Error   PHP Call to undefined method OC\Preview::data() at /var/www/owncloud/apps/files/controller/apicontroller.php#53
    
  12. enoch85 commented on Mar 22, 2015

    @enoch85

    @oparoz we are getting there. ;) Next move?

  13. 7 remaining items

  14. enoch85 commented on Mar 22, 2015

    @enoch85

    @oparoz Yes, it was Githubs fault.

    Get this now:

    Error   PHP Call to a member function getPreview() on a non-object at /var/www/owncloud/apps/files/controller/apicontroller.php#53  2015-03-22T21:16:41+00:00
    Error   PHP Undefined variable: previewData at /var/www/owncloud/apps/files/controller/apicontroller.php#53 2015-03-22T21:16:41+00:00
    Error   PHP Call to a member function getPreview() on a non-object at /var/www/owncloud/apps/files/controller/apicontroller.php#53  2015-03-22T21:16:40+00:00
    Error   PHP Undefined variable: previewData at /var/www/owncloud/apps/files/controller/apicontroller.php#53 2015-03-22T21:16:40+00:00
    

    Any suggestion?

  15. oparoz commented on Mar 22, 2015

    @oparoz
    ContributorAuthor

    Argh... I'm not using the IDE and it shows...

    $preview = new Preview('', 'files', urldecode($file), $x, $y, true);
    $previewData = $preview->getPreview();
    $response = new JSONResponse($previewData->data());
    $response->setHeaders(
        [
            'Cache-Control' => 'no-cache, must-revalidate',
            'Content-type'  => 'image/png; charset=utf-8'
        ]
    );
    \OCP\Response::setContentDispositionHeader(basename(urldecode($file).'.png'), 'attachment');
    
    return $response;
    
  16. enoch85 commented on Mar 22, 2015

    @enoch85

    Haha, yes now the error is gone, but it doesn't show any previews. :/

    What to do?

  17. oparoz commented on Mar 22, 2015

    @oparoz
    ContributorAuthor

    Check the network tab in your browser's debugger (F12). If you're getting 412 errors, it means some headers are missing.

  18. enoch85 commented on Mar 22, 2015

    @enoch85

    Browsers debugger? I´m testing with the Android app, not Chrome.

  19. oparoz commented on Mar 22, 2015

    @oparoz
    ContributorAuthor

    In case it's the headers, try this

    $preview = new Preview('', 'files', urldecode($file), $x, $y, true);
    $previewData = $preview->getPreview();
    $response = new JSONResponse($previewData->data());
    $response->addHeader('Content-type', 'image/png; charset=utf-8');
    \OCP\Response::setContentDispositionHeader(basename(urldecode($file).'.png'), 'attachment');
    
    return $response;
    

    The Response class is smart enough to be able to replace a header.

    Even when using Android, you can type the thumbnail URL in a browser. You get the URL from the owncloud log

  20. enoch85 commented on Mar 22, 2015

    @enoch85

    Ok, so I can see the previews in the browser with both of your solutions - with no errors, but not with the Android app. @tobiasKaminsky

  21. oparoz commented on Mar 22, 2015

    @oparoz
    ContributorAuthor

    OK, I won't be able to help you there then... Hopefully someone else will be able to offer a fix.

  22. enoch85 commented on Mar 22, 2015

    @enoch85

    OK, so this is a working patch for the browser:

    $preview = new Preview('', 'files', urldecode($file), $x, $y, true);
                $previewData = $preview->getPreview();
                $response = new JSONResponse($previewData->data());
                $response->setHeaders(
                    [
                        'Cache-Control' => 'no-cache, must-revalidate',
                        'Content-type'  => 'image/png; charset=utf-8'
                    ]
    );
    \OCP\Response::setContentDispositionHeader(basename(urldecode($file).'.png'), 'attachment');
    
    return $response;
    

    Thanks for helping me debugging!

  23. oparoz commented on Mar 22, 2015

    @oparoz
    ContributorAuthor

    Thanks for testing 36 versions of the patch ;)

  24. MorrisJobke commented on Mar 24, 2015

    @MorrisJobke
    Contributor

    @rullzer is this fixed with #14919 ?

  25. rullzer commented on Mar 24, 2015

    @rullzer
    Contributor

    It should be. But @oparoz please confirm.

  26. rullzer commented on Apr 13, 2015

    @rullzer
    Contributor

    So I assume the PR fixed this. Please reopen if not.

  27. locked as resolved and limited conversation to collaborators on Aug 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions