organic/pdf: Add pdf upload feature
This commit is contained in:
@@ -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');
|
||||||
|
|
||||||
|
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 - -");
|
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,6 +75,16 @@ if ($format === 'text') {
|
|||||||
1 => ["pipe", "w"], // stdout
|
1 => ["pipe", "w"], // stdout
|
||||||
2 => ["pipe", "w"], // stderr
|
2 => ["pipe", "w"], // stderr
|
||||||
];
|
];
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
|
$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);
|
||||||
|
} else {
|
||||||
$process = proc_open(
|
$process = proc_open(
|
||||||
['bash', '-c',
|
['bash', '-c',
|
||||||
"curl -s " . escapeshellarg($url) . " | " .
|
"curl -s " . escapeshellarg($url) . " | " .
|
||||||
@@ -58,6 +92,8 @@ if ($format === 'text') {
|
|||||||
$fd_spec,
|
$fd_spec,
|
||||||
$pipes
|
$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]);
|
||||||
|
Reference in New Issue
Block a user