Repo for the search and displace core module including the interface to select files and search and displace operations to run on them. https://searchanddisplace.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

60 lines
1.8 KiB

<?php
namespace App\Http\Controllers;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\UploadedFile;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
class FileController extends Controller
{
/**
*
* @return JsonResponse
*
* @throws BindingResolutionException
*/
public function create(): JsonResponse
{
try {
/** @var UploadedFile $file */
$file = request()->file('file');
$time = time();
$fileName = $time . '-' . $file->getClientOriginalName();
$fileId = $time . pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$path = Storage::putFileAs('files/', $file, $fileName);
$guzzle = new Client();
/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $guzzle->request('POST', 'http://ingest.sandd/ingest', [
'multipart' => [
[
'name' => 'id',
'contents' => $fileId,
'headers' => [ 'Content-Type' => 'application/json' ]
],
[
'name' => 'document',
'contents' => fopen($file->getPathname(), 'r')
]
]
]);
return response()->json([
'file' => $file->getClientOriginalName(),
'id' => $fileId,
'path' => $path,
]);
} catch (BadResponseException $e) {
return response()->json([
'message' => $e->getMessage(),
'response' => $e->getResponse()
], 400);
}
}
}