150 lines
5.4 KiB
PHP
Executable File
150 lines
5.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\M3\Http\Controllers\API;
|
|
|
|
use Cache;
|
|
use Config;
|
|
use File;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Modules\M3\Helpers\Helper;
|
|
use Modules\M3\Http\Controllers\Controller;
|
|
use Str;
|
|
|
|
class PdfStorage extends Controller
|
|
{
|
|
private const ZERO_PAD = '000000';
|
|
|
|
public function PdfStorageAPI(Request $request): JsonResponse
|
|
{
|
|
$this->configureTimeouts();
|
|
$context = $this->requestContext($request);
|
|
|
|
if ($context === null) {
|
|
$result = $this->errorResponse('No URL Parameters Sent', self::ZERO_PAD, '', '', '');
|
|
} else {
|
|
try {
|
|
$this->logRequest($context);
|
|
$cacheKey = $this->buildCacheKey($context);
|
|
$pdfFileName = (string) Config::get('arrays.PdfPath') . $context['FILENAME'] . '.pdf';
|
|
$error = $this->validateStorageRequest($pdfFileName, $cacheKey);
|
|
|
|
if ($error !== null) {
|
|
$result = $this->errorResponse($error, $context['DELIVERYNO'], $context['DOCNUM'], $context['COMPANY'], $context['WHS']);
|
|
} else {
|
|
Cache::forever($cacheKey, base64_encode(File::get($pdfFileName)));
|
|
$this->logResult($context, 'PDF File converted to Base64 and Saved to Cache.Key: ' . $cacheKey);
|
|
$result = response()->json(['code' => '200', 'status' => 'Success', 'cacheKey' => $cacheKey]);
|
|
}
|
|
} catch (\Throwable $exception) {
|
|
$message = 'An Error Occured - ' . substr($exception->getMessage(), 0, 300);
|
|
$this->safeLog($context, $message, true);
|
|
$result = response()->json([
|
|
'code' => '405',
|
|
'status' => 'error',
|
|
'message' => 'Catched Error: ' . $exception->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
private function configureTimeouts(): void
|
|
{
|
|
$timeout = (string) Config::get('arrays.PHPTimeOut');
|
|
ini_set('max_execution_time', $timeout);
|
|
ini_set('max_input_time', $timeout);
|
|
ini_set('default_socket_timeout', $timeout);
|
|
}
|
|
|
|
private function requestContext(Request $request): ?array
|
|
{
|
|
if ($request->all() === []) {
|
|
return null;
|
|
}
|
|
|
|
$context = [];
|
|
foreach (['COMPANY', 'WHS', 'DOCTYPE', 'FILENAME', 'DOCNUM', 'A4MACPKR'] as $key) {
|
|
$context[$key] = trim((string) $request->input($key, ''));
|
|
}
|
|
|
|
$context['DELIVERYNO'] = $context['A4MACPKR'] !== '' ? $context['A4MACPKR'] : self::ZERO_PAD;
|
|
$context['SHUSER'] = m3User() !== '' ? Str::upper(m3User()) : 'WebUser';
|
|
$context['SHSOURCE'] = 'PdfStorageAPI';
|
|
|
|
return $context;
|
|
}
|
|
|
|
private function buildCacheKey(array $context): string
|
|
{
|
|
foreach (['A4MACPKR', 'DOCTYPE', 'DOCNUM', 'COMPANY', 'WHS'] as $required) {
|
|
if ($context[$required] === '') {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
return $context['A4MACPKR'] . $context['DOCTYPE'] . $context['DOCNUM'] . $context['COMPANY'] . $context['WHS'];
|
|
}
|
|
|
|
private function validateStorageRequest(string $pdfFileName, string $cacheKey): ?string
|
|
{
|
|
$messages = [];
|
|
if (! File::exists($pdfFileName)) {
|
|
$messages[] = 'PDF File Not Found-';
|
|
}
|
|
if ($cacheKey === '') {
|
|
$messages[] = 'Cache Key is missing Params';
|
|
}
|
|
|
|
return $messages === [] ? null : implode('', $messages);
|
|
}
|
|
|
|
private function logRequest(array $context): void
|
|
{
|
|
$params = array_intersect_key($context, array_flip(['COMPANY', 'WHS', 'DOCTYPE', 'FILENAME', 'A4MACPKR', 'DOCNUM']));
|
|
$this->safeLog($context, json_encode($params, JSON_THROW_ON_ERROR), false, 'M3-Req-out');
|
|
}
|
|
|
|
private function logResult(array $context, string $message): void
|
|
{
|
|
$this->safeLog($context, $message);
|
|
}
|
|
|
|
private function safeLog(array $context, string $message, bool $error = false, string $type = 'M3-Res-out'): void
|
|
{
|
|
try {
|
|
Helper::logToICM699M([
|
|
'SHMACPKR' => $context['DELIVERYNO'] ?? self::ZERO_PAD,
|
|
'SHDT' => date('Y-m-d'),
|
|
'SHTIME' => date('H:i:s'),
|
|
'SHERROR' => $error ? 'X' : '',
|
|
'SHINTBOX' => $context['DOCNUM'] ?? '',
|
|
'SHTYPE' => $type,
|
|
'SHDATA' => $message,
|
|
'SHUSER' => $context['SHUSER'] ?? (m3User() !== '' ? Str::upper(m3User()) : 'WebUser'),
|
|
'SHSOURCE' => $context['SHSOURCE'] ?? 'PdfStorageAPI',
|
|
'SHCO' => $context['COMPANY'] ?? '',
|
|
'SHWHS' => $context['WHS'] ?? '',
|
|
]);
|
|
} catch (\Throwable) {
|
|
// Logging must never turn an API response into a 500 error.
|
|
}
|
|
}
|
|
|
|
private function errorResponse(string $message, string $deliveryNo, string $docNum, string $company, string $warehouse): JsonResponse
|
|
{
|
|
$context = [
|
|
'DELIVERYNO' => $deliveryNo,
|
|
'DOCNUM' => $docNum,
|
|
'COMPANY' => $company,
|
|
'WHS' => $warehouse,
|
|
'SHUSER' => m3User() !== '' ? Str::upper(m3User()) : 'WebUser',
|
|
'SHSOURCE' => 'PdfStorageAPI',
|
|
];
|
|
$this->safeLog($context, 'An Error Occured -' . $message, true);
|
|
|
|
return response()->json(['code' => '405', 'status' => 'Error', 'message' => $message]);
|
|
}
|
|
}
|