-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPdfGalleryControl.php
154 lines (135 loc) · 4.29 KB
/
PdfGalleryControl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
declare(strict_types=1);
namespace App\Components\PdfGallery;
use Fykosak\Utils\Components\DIComponent;
use Nette\Caching\Cache;
use Nette\Caching\Storage;
use Nette\DI\Container;
use Nette\Utils\Finder;
use Nette\Utils\UnknownImageFileException;
class PdfGalleryControl extends DIComponent
{
private readonly string $wwwDir;
private readonly Cache $cache;
public function __construct(Container $container)
{
parent::__construct($container);
$this->wwwDir = $container->getParameters()['wwwDir'];
}
public function injectStorage(Storage $storage): void
{
$this->cache = new Cache($storage, __NAMESPACE__);
}
public static function getPdfs(string $path, string $wwwDir, bool $fromMetadata = false): array
{
$pdfs = [];
try {
$iterator = Finder::findFiles('*.pdf')->in($wwwDir . $path)->getIterator();
} catch (\Exception $e) {
return [];
}
foreach ($iterator as $file) {
if ($fromMetadata) {
try {
$info = static::getPdfProp($file->getRealPath());
$name = $info["Title"] ?? ($info["title"] ?? '');
} catch (\Exception $e) {
$name = '';
}
} else {
$name = null;
}
$wwwPath = substr($file->getPathname(), strlen($wwwDir));
$pdfs[] = [
'src' => $wwwPath,
'name' => $name ?: $file->getBasename('.pdf'),
];
}
//sort alphabetically
usort($pdfs, function ($a, $b) {
return $a['name'] <=> $b['name'];
});
foreach ($pdfs as $index => &$pdfFile) {
$pdfFile['index'] = $index;
}
return $pdfs;
}
/**
* @throws UnknownImageFileException
* @throws \Throwable
*/
public function render(string $path, bool $fromMetadata = false): void
{
$this->template->pdfs = $this->cache->load(
[$path, $this->wwwDir],
fn() => self::getPdfs($path, $this->wwwDir, $fromMetadata)
);
$this->template->render(__DIR__ . DIRECTORY_SEPARATOR . 'pdfGalleryList.latte');
}
public function renderButtons(string $path, bool $fromMetadata = false): void
{
$this->template->pdfs = $this->cache->load(
[$path, $this->wwwDir],
fn() => self::getPdfs($path, $this->wwwDir, $fromMetadata)
);
$this->template->render(__DIR__ . DIRECTORY_SEPARATOR . 'pdfGalleryButtons.latte');
}
/**
* @throws UnknownImageFileException
* @throws \Throwable
*/
public function hasFiles(string $path): bool
{
return count($this->cache->load(
[$path, $this->wwwDir],
fn() => self::getPdfs($path, $this->wwwDir)
)) > 0;
}
/**
* magic from http://www.fpdf.org/en/script/script59.php
* @param string $file filename
* @return array whose keys are the names of the properties found in the file
*/
public static function getPdfProp($file): array
{
$f = fopen($file, 'rb');
if (!$f) {
return [];
}
//Read the last 16KB
fseek($f, -16384, SEEK_END);
$s = fread($f, 16384);
//Extract cross-reference table and trailer
if (!preg_match("/xref[\r\n]+(.*)trailer(.*)startxref/s", $s, $a)) {
return [];
}
$xref = $a[1];
$trailer = $a[2];
//Extract Info object number
if (!preg_match('/Info ([0-9]+) /', $trailer, $a)) {
return [];
}
$object_no = (int) $a[1];
//Extract Info object offset
$lines = preg_split("/[\r\n]+/", $xref);
$line = $lines[1 + $object_no];
$offset = (int) $line;
if ($offset == 0) {
return [];
}
//Read Info object
fseek($f, $offset, SEEK_SET);
$s = fread($f, 1024);
fclose($f);
//Extract properties
if (!preg_match('/<<(.*)>>/Us', $s, $a)) {
return [];
}
$n = preg_match_all('|/([a-z]+) ?\((.*)\)|Ui', $a[1], $a);
$prop = array();
for ($i = 0; $i < $n; $i++) {
$prop[$a[1][$i]] = $a[2][$i];
}
return $prop;
}
}