Skip to content

Commit f804506

Browse files
committed
Can see counter on /api/download-counter #10837
1 parent 1658733 commit f804506

6 files changed

Lines changed: 88 additions & 23 deletions

File tree

config/autoload/dependencies.global.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
// class name.
1818
'invokables' => [
1919
Doctrine\ORM\Mapping\UnderscoreNamingStrategy::class => Doctrine\ORM\Mapping\UnderscoreNamingStrategy::class,
20+
Application\Handler\DownloadFile::class => Application\Handler\DownloadFile::class,
21+
Application\Handler\DownloadCounter::class => Application\Handler\DownloadCounter::class,
2022
],
2123
// Use 'factories' for services provided by callbacks/factory classes.
2224
'factories' => [

config/routes.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@
5757
\Application\Handler\DatatransHandler::class,
5858
], 'datatrans');
5959

60+
$app->get('/api/download-file', [
61+
\Application\Handler\DownloadFile::class,
62+
]);
63+
64+
$app->get('/api/download-counter', [
65+
\Application\Handler\DownloadCounter::class,
66+
]);
67+
6068
$app->get('/sitemap.xml', [
6169
SitemapHandler::class,
6270
], 'sitemap');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"ng": "ng",
77
"postinstall": "playwright install chromium",
88
"client-config": "`which php8.2 || which php` ./bin/generate-client-configuration.php",
9-
"prerequisite": "mkdir -p htdocs/ && cp -v server/index.php htdocs/ && cp -v server/download-file.php htdocs/ && yarn client-config && yarn codegen",
9+
"prerequisite": "mkdir -p htdocs/ && cp -v server/index.php htdocs/ && yarn client-config && yarn codegen",
1010
"dev": "yarn prerequisite && ng serve",
1111
"prod": "yarn prerequisite && ng build && find data/tmp/build/browser -type f -not -name '*.gz' -exec gzip --keep --best {} \\; && rm -rf htdocs/ && mv -v data/tmp/build/browser htdocs",
1212
"test": "yarn prerequisite && ng test",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Application\Handler;
6+
7+
use Ecodev\Felix\Handler\AbstractHandler;
8+
use Laminas\Diactoros\Response\TextResponse;
9+
use Psr\Http\Message\ResponseInterface;
10+
use Psr\Http\Message\ServerRequestInterface;
11+
12+
class DownloadCounter extends AbstractHandler
13+
{
14+
public function handle(ServerRequestInterface $request): ResponseInterface
15+
{
16+
$path = DownloadFile::COUNTER_PATH;
17+
if (!is_readable($path)) {
18+
return $this->createError('File not found on disk, or not readable');
19+
}
20+
21+
$response = new TextResponse(file_get_contents($path));
22+
23+
return $response;
24+
}
25+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Application\Handler;
6+
7+
use Ecodev\Felix\Handler\AbstractHandler;
8+
use Laminas\Diactoros\Response;
9+
use Psr\Http\Message\ResponseInterface;
10+
use Psr\Http\Message\ServerRequestInterface;
11+
12+
class DownloadFile extends AbstractHandler
13+
{
14+
public const COUNTER_PATH = 'data/download-file-counter/counter.txt';
15+
16+
public function handle(ServerRequestInterface $request): ResponseInterface
17+
{
18+
$path = 'data/download-file-counter/file.pdf';
19+
$cookie_name = 'artisans_pdf_download_1';
20+
21+
// Increment counter if no cookie = first visit
22+
if (!isset($_COOKIE[$cookie_name])) {
23+
$download_count = 0;
24+
if (file_exists(self::COUNTER_PATH)) {
25+
$download_count = (int) (file_get_contents(self::COUNTER_PATH));
26+
}
27+
++$download_count;
28+
file_put_contents(self::COUNTER_PATH, $download_count);
29+
}
30+
31+
// Flag cookie
32+
setcookie($cookie_name, 'true', time() + (86400 * 60), '/'); // 60 days
33+
34+
if (!is_readable($path)) {
35+
return $this->createError('File not found on disk, or not readable');
36+
}
37+
38+
$resource = fopen($path, 'rb');
39+
if ($resource === false) {
40+
return $this->createError('Cannot open file on disk');
41+
}
42+
43+
$size = filesize($path);
44+
$response = new Response($resource, 200, [
45+
'content-type' => 'application/pdf',
46+
'Content-Disposition' => 'attachment; filename="' . basename($path) . '"',
47+
'content-length' => $size,
48+
]);
49+
50+
return $response;
51+
}
52+
}

server/download-file.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
11
<?php
22

33
declare(strict_types=1);
4-
$pdf_file = '../data/download-file-counter/file.pdf';
5-
$counter_path = '../data/download-file-counter/counter.txt';
6-
$cookie_name = 'artisans_pdf_download_1';
7-
8-
// Increment counter if no cookie = first visit
9-
if (!isset($_COOKIE[$cookie_name])) {
10-
$download_count = 0;
11-
if (file_exists($counter_path)) {
12-
$download_count = (int) (file_get_contents($counter_path));
13-
}
14-
++$download_count;
15-
file_put_contents($counter_path, $download_count);
16-
}
17-
18-
// Flag cookie
19-
setcookie($cookie_name, 'true', time() + (86400 * 60), '/'); // 60 days
20-
21-
// Do it
22-
header('Content-Type: application/pdf');
23-
header('Content-Disposition: attachment; filename="' . basename($pdf_file) . '"');
24-
readfile($pdf_file);
25-
exit;

0 commit comments

Comments
 (0)