150 lines
3.7 KiB
PHP
150 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Str;
|
|
|
|
class SapFileController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
// public function store(Request $request)
|
|
// {
|
|
// //
|
|
// }
|
|
|
|
public function getSapData(Request $request)
|
|
{
|
|
|
|
}
|
|
|
|
public function readFiles(Request $request)
|
|
{
|
|
$expectedUser = env('API_AUTH_USER');
|
|
$expectedPw = env('API_AUTH_PW');
|
|
$header_auth = $request->header('Authorization');
|
|
$expectedToken = $expectedUser . ':' . $expectedPw;
|
|
|
|
if ("Bearer " . $expectedToken != $header_auth)
|
|
{
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => 'Invalid authorization token!'
|
|
], 403);
|
|
}
|
|
|
|
$plantCode = $request->header('plant-code');
|
|
|
|
if ($plantCode == null || $plantCode == '')
|
|
{
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Plant code can't be empty!"
|
|
], 400);
|
|
}
|
|
else if (Str::length($plantCode) < 4 || !is_numeric($plantCode) || !preg_match('/^[1-9]\d{3,}$/', $plantCode))
|
|
{
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Invalid plant code found!"
|
|
], 400);
|
|
}
|
|
|
|
$path = "/LaserPRD/{$plantCode}";
|
|
|
|
// $isDir = is_dir($path);
|
|
// $isReadable = is_readable($path);
|
|
|
|
// Check if folder exists
|
|
if (!is_dir($path)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Folder for plant-code {$plantCode} not found!"
|
|
], 404);
|
|
}
|
|
|
|
if (!is_readable($path)) {
|
|
return response()->json([
|
|
'status_code' => 'ERROR',
|
|
'status_description' => "Folder for plant-code {$plantCode} exists but is not readable!"
|
|
], 500);
|
|
}
|
|
|
|
// Scan folder
|
|
$allFiles = scandir($path);
|
|
if ($allFiles === false) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'status_description' => 'Failed to scan directory',
|
|
// 'debug' => ['path_checked' => $path]
|
|
], 500);
|
|
}
|
|
|
|
$files = array_filter($allFiles, function($item) use ($path) {
|
|
$fullPath = $path . '/' . $item;
|
|
return @is_file($fullPath) && pathinfo($item, PATHINFO_EXTENSION) === 'txt';
|
|
});
|
|
|
|
if (empty($files)) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'status_description' => 'No text files found',
|
|
], 404);
|
|
}
|
|
|
|
$data = [];
|
|
foreach ($files as $file)
|
|
{
|
|
$filePath = $path . '/' . $file;
|
|
|
|
$content = file_get_contents($filePath);
|
|
|
|
$data[] = [
|
|
'filename' => $file,
|
|
'content' => $content,
|
|
];
|
|
}
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'files' => $data,
|
|
]);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
//
|
|
}
|
|
}
|