|
|
<?php
namespace App\Http\Controllers;
use App\SearchDisplace\Ingest\SendDocument; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Storage; use Illuminate\Http\UploadedFile; use GuzzleHttp\Exception\BadResponseException; use Illuminate\Support\Str; use Symfony\Component\Process\Process;
class FileController extends Controller { /** * * @return JsonResponse */ public function create(): JsonResponse { request()->validate([ 'file' => [ 'required', 'file', 'max:10000', 'mimes:doc,dot,docx,dotx,docm,dotm,odt,rtf,pdf,txt', ], ]);
try { /** @var UploadedFile $file */ $file = request()->file('file'); $time = time(); $fileId = $time . '_' . pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$sendDocument = new SendDocument();
$sendDocument->execute($fileId, [ 'path' => $file->getRealPath(), 'type' => $file->getMimeType(), 'name' => $file->getClientOriginalName() ]);
return response()->json([ 'id' => $fileId, 'file_name' => $file->getClientOriginalName(), ]); } catch (BadResponseException $e) { return response()->json([ 'message' => $e->getMessage(), 'response' => $e->getResponse() ], 400); } catch (\Exception $e) { return response()->json([ 'message' => $e->getMessage(), ], 400); } }
/** * */ public function convert(): JsonResponse { $mdFileContent = request()->input('content'); $fileId = request()->input('file_id');
Storage::disk('local')->put('tmp/' . $fileId . '.md', $mdFileContent); $tmpMdFilePath = Storage::path('tmp/' . $fileId . '.md');
$pandoc = new Process([ 'pandoc', '-f', 'markdown', '-t', 'odt', $tmpMdFilePath ]);
$pandoc->start();
while ($pandoc->isRunning()) { //
}
$odtFileContent = $pandoc->getOutput(); Storage::disk('local')->put('tmp/' . $fileId . '.odt', $odtFileContent);
return response()->json([ 'path' => 'tmp/' . $fileId . '.odt' ]); }
/** * * @param string $path * @return mixed */ public function download(string $path) { return Storage::download($path); }
/** * Delete a file currently in progress * * @param string $id * @return JsonResponse */ public function delete(string $id): JsonResponse { $success = Storage::delete([ "tmp/{$id}.md", "tmp/{$id}.odt", "contracts/{$id}.md", ]); return response()->json(['success' => $success]); } }
|