Score:-2

Clicking links to uploaded files shows bytecode

hu flag

This issue just started occurring on our app. We allow users to upload files in our forms and we create links for those files like so;

$f = \Drupal::entityTypeManager()->getStorage('file')->load($file->value);
if ($f) {
  $fileLink = [
    'title' => $f->get('filename')->value,
    'link' => \Drupal::service('file_url_generator')->generateAbsoluteString($f->get('uri')->value),
  ];
  $fileLinks[] = $fileLink;
}

Nothing special.

Our default download method is "Public local files served by the webserver."

The issue is that as of last week all files, regardless of mime, display as bytecode and not the file.

The url to this file is direct with extension, so its not being served from Drupal, meaning no mime issues. I can open the file perfectly fine and if you right click and save as, the file downloads and opens fine.

Appreciate the help.

id flag
What changed in the codebase or in the platform last week?
hu flag
Hi @cilefen thats the thing, this was raised as an issue a long time after the previous deployment. There's a good chance this has been around for a while and went unnoticed which means it could have been caused by any number of releases.
id flag
What is happening is that the wrong content-type HTTP header is sent. If this is custom code you are not setting the header, or you are not calling the type detection library, or you are calling the library but the library broke, or you were depending on framework or platform magic which broke.
id flag
Very recently Symfony Framework broke and then fixed its mime detection in BinaryFileResponse. I haven’t yet verified if this could actually affect Drupal: https://github.com/symfony/symfony/pull/47746
hu flag
The issue goes away if I change the files to be temporary in the file_managed table. Nothing else changes and they load fine. I'm even using core code to generate the links now. Not sure why this was down voted.
Score:0
hu flag

The issue was due to an implementation of hook_file_download.

We were returning a 1 in this hook to allow certain users access, but the hook needs to return the headers of the file when a user has access.

Interestingly, this code has not changed since mid 2020 so something else has caused this to be an issue.

return file_get_content_headers($file);

All fixed.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.