From c742c88182271fd58636dc80c652aa958ced82f0 Mon Sep 17 00:00:00 2001
From: catch <6915-catch@users.noreply.drupalcode.org>
Date: Wed, 5 Mar 2025 12:33:24 +0000
Subject: [PATCH] Issue #3494126 by ramprassad, karimb, nicxvan, kim.pepper,
 smustgrave, berdir: Move file_get_content_headers() to a method on the file
 entity

---
 core/modules/editor/src/Hook/EditorHooks.php          |  2 +-
 core/modules/file/file.module                         | 10 +++++-----
 core/modules/file/src/Entity/File.php                 | 11 +++++++++++
 core/modules/file/src/FileInterface.php               |  9 +++++++++
 core/modules/file/src/Hook/FileDownloadHook.php       |  2 +-
 .../file/tests/file_test/src/Hook/FileTestHooks.php   |  2 +-
 6 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/core/modules/editor/src/Hook/EditorHooks.php b/core/modules/editor/src/Hook/EditorHooks.php
index 952dbff1db5e..e58f922d2ae7 100644
--- a/core/modules/editor/src/Hook/EditorHooks.php
+++ b/core/modules/editor/src/Hook/EditorHooks.php
@@ -319,7 +319,7 @@ public function fileDownload($uri): array|int|null {
       }
     }
     // Access is granted.
-    $headers = file_get_content_headers($file);
+    $headers = $file->getDownloadHeaders();
     return $headers;
   }
 
diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index 9c10140fa4ba..120d553c5c6d 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -35,13 +35,13 @@
  * @return array
  *   An associative array of headers, as expected by
  *   \Symfony\Component\HttpFoundation\StreamedResponse.
+ *
+ * @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use Drupal\file\Entity\FileInterface::getDownloadHeaders() instead.
+ * @see https://www.drupal.org/node/3494172
  */
 function file_get_content_headers(FileInterface $file) {
-  return [
-    'Content-Type' => $file->getMimeType(),
-    'Content-Length' => $file->getSize(),
-    'Cache-Control' => 'private',
-  ];
+  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0.  Use Drupal\file\Entity\FileInterface::getDownloadHeaders() instead. See https://www.drupal.org/node/3494172', E_USER_DEPRECATED);
+  return $file->getDownloadHeaders();
 }
 
 /**
diff --git a/core/modules/file/src/Entity/File.php b/core/modules/file/src/Entity/File.php
index 0022fec6add1..bc310eeb7f22 100644
--- a/core/modules/file/src/Entity/File.php
+++ b/core/modules/file/src/Entity/File.php
@@ -300,4 +300,15 @@ protected function invalidateTagsOnSave($update) {
     Cache::invalidateTags($tags);
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function getDownloadHeaders(): array {
+    return [
+      'Content-Type' => $this->getMimeType(),
+      'Content-Length' => $this->getSize(),
+      'Cache-Control' => 'private',
+    ];
+  }
+
 }
diff --git a/core/modules/file/src/FileInterface.php b/core/modules/file/src/FileInterface.php
index 588009cb475b..efa6627790fe 100644
--- a/core/modules/file/src/FileInterface.php
+++ b/core/modules/file/src/FileInterface.php
@@ -142,4 +142,13 @@ public function setTemporary();
    */
   public function getCreatedTime();
 
+  /**
+   * Examines a file entity and returns content headers for download.
+   *
+   * @return array
+   *   An associative array of headers, as expected by
+   *   \Symfony\Component\HttpFoundation\StreamedResponse.
+   */
+  public function getDownloadHeaders(): array;
+
 }
diff --git a/core/modules/file/src/Hook/FileDownloadHook.php b/core/modules/file/src/Hook/FileDownloadHook.php
index 21086c5eb114..1dee2310d57f 100644
--- a/core/modules/file/src/Hook/FileDownloadHook.php
+++ b/core/modules/file/src/Hook/FileDownloadHook.php
@@ -58,7 +58,7 @@ public function __invoke($uri): array|int|null {
       return -1;
     }
     // Access is granted.
-    return file_get_content_headers($file);
+    return $file->getDownloadHeaders();
   }
 
 }
diff --git a/core/modules/file/tests/file_test/src/Hook/FileTestHooks.php b/core/modules/file/tests/file_test/src/Hook/FileTestHooks.php
index 1f274d9ed0ab..267e2ada834c 100644
--- a/core/modules/file/tests/file_test/src/Hook/FileTestHooks.php
+++ b/core/modules/file/tests/file_test/src/Hook/FileTestHooks.php
@@ -38,7 +38,7 @@ public function fileDownload($uri): array|int|null {
     if (\Drupal::state()->get('file_test.allow_all', FALSE)) {
       $files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri]);
       $file = reset($files);
-      return file_get_content_headers($file);
+      return $file->getDownloadHeaders();
     }
     FileTestHelper::logCall('download', [$uri]);
     return $this->getReturn('download');
-- 
GitLab