organic/pdf: Add pdf upload feature

This commit is contained in:
2025-07-13 22:04:06 +02:00
parent fffc450e87
commit f108f026b9

View File

@@ -1,9 +1,9 @@
<?php <?php
if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'HEAD') { if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'POST' && $_SERVER['REQUEST_METHOD'] !== 'HEAD') {
header('Status: 405'); header('Status: 405');
header('Content-Length: 0'); header('Content-Length: 0');
header('Allow: GET, HEAD'); header('Allow: GET, POST, HEAD');
exit; exit;
} }
@@ -14,7 +14,7 @@ if ($info !== '') {
exit; exit;
} }
$url = str_replace(' ', '+', $_GET['url']); $url = isset($_GET['url']) ? str_replace(' ', '+', $_GET['url']) : null;
$format = $_GET['format'] ?? 'json'; $format = $_GET['format'] ?? 'json';
function get_address($array, $from, $to): array { function get_address($array, $from, $to): array {
@@ -42,7 +42,31 @@ function jenc($data): string {
if ($format === 'text') { if ($format === 'text') {
header('Content-Type: text/plain; charset=UTF-8'); header('Content-Type: text/plain; charset=UTF-8');
passthru("curl -s '" . escapeshellarg($url) . "' | pdftotext -raw - -");
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fd_spec = [
0 => ["pipe", "r"], // stdin
1 => ["pipe", "w"], // stdout
2 => ["pipe", "w"], // stderr
];
$process = proc_open(['pdftotext', '-raw', '-', '-'], $fd_spec, $pipes);
$input = fopen("php://input", "rb");
while (!feof($input)) {
if (($buffer = fread($input, 8192)) === false)
break;
fwrite($pipes[0], $buffer);
}
fclose($input);
fclose($pipes[0]);
fpassthru($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$return_value = proc_close($process);
} else {
passthru("curl -s '" . escapeshellarg($url) . "' | pdftotext -raw - -");
}
} else if ($format === 'json') { } else if ($format === 'json') {
header('Content-Type: application/json; charset=UTF-8'); header('Content-Type: application/json; charset=UTF-8');
@@ -51,13 +75,25 @@ if ($format === 'text') {
1 => ["pipe", "w"], // stdout 1 => ["pipe", "w"], // stdout
2 => ["pipe", "w"], // stderr 2 => ["pipe", "w"], // stderr
]; ];
$process = proc_open( if ($_SERVER['REQUEST_METHOD'] === 'POST') {
['bash', '-c', $process = proc_open(['pdftotext', '-raw', '-', '-'], $fd_spec, $pipes);
"curl -s " . escapeshellarg($url) . " | " . $input = fopen("php://input", "rb");
"pdftotext -raw - -"], while (!feof($input)) {
$fd_spec, if (($buffer = fread($input, 8192)) === false)
$pipes break;
); fwrite($pipes[0], $buffer);
}
fclose($input);
} else {
$process = proc_open(
['bash', '-c',
"curl -s " . escapeshellarg($url) . " | " .
"pdftotext -raw - -"],
$fd_spec,
$pipes
);
}
fclose($pipes[0]); fclose($pipes[0]);
$text = stream_get_contents($pipes[1]); $text = stream_get_contents($pipes[1]);
fclose($pipes[1]); fclose($pipes[1]);