FOSSology  4.5.1
Open Source License Compliance by Open Source Software
DownloadUtil.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2024 Siemens AG
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 namespace Fossology\Lib\Util;
8 
9 use Symfony\Component\HttpFoundation\Response;
10 
12 {
20  public static function getDownloadConfirmationResponse($downloadUrl, $fileName, $referer)
21  {
22  if (empty($referer)) {
23  $referer = "?mod=browse";
24  }
25 
26  $script = '<script>
27  if (confirm("Do you want to download the file ' . $fileName . '?")) {
28  fetch("' . $downloadUrl . '")
29  .then(response => response.blob())
30  .then(blob => {
31  const url = window.URL.createObjectURL(blob);
32  const a = document.createElement("a");
33  a.href = url;
34  a.download = "' . $fileName . '";
35  document.body.appendChild(a);
36  a.click();
37  window.URL.revokeObjectURL(url);
38  document.body.removeChild(a);
39  window.location.href = "' . $referer . '";
40  });
41  } else {
42  window.location.href = "' . $referer . '";
43  }
44  </script>';
45 
46  return new Response($script, Response::HTTP_OK, ['Content-Type' => 'text/html']);
47  }
48 
56  public static function getDownloadResponse($content, $fileName, $contentType = 'text/csv')
57  {
58  $headers = array(
59  'Content-type' => $contentType . ', charset=UTF-8',
60  'Content-Disposition' => 'attachment; filename=' . $fileName,
61  'Pragma' => 'no-cache',
62  'Cache-Control' => 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0',
63  'Expires' => 'Expires: Thu, 19 Nov 1981 08:52:00 GMT'
64  );
65 
66  return new Response($content, Response::HTTP_OK, $headers);
67  }
68 }
static getDownloadConfirmationResponse($downloadUrl, $fileName, $referer)
static getDownloadResponse($content, $fileName, $contentType='text/csv')