directoryPath = storage_path("app/contracts/"); } /** * * @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() ]); $originalFile = $fileId . "/{$fileId}." . $file->extension(); Storage::disk('local')->put("contracts/{$originalFile}", file_get_contents($file)); // keep the original file $process = new Process(['soffice', '--convert-to', 'xml', $this->directoryPath . $originalFile, '--outdir', $this->directoryPath . $fileId]); $process->run(); Storage::delete("contracts/{$originalFile}"); 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 { $file = (object) request()->input('file'); $searchers = request()->input('searchers'); $xml = $this->directoryPath . "{$file->id}/{$file->id}.xml"; (new SearchAndDisplaceXML($xml, $searchers, $file->type))->execute(); return response()->json([ 'path' => "tmp/{$file->id}.{$file->type}" ]); } /** * * @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 { Storage::deleteDirectory("contracts/${id}"); $tmpFiles = Storage::allFiles("tmp"); $success = Storage::delete($tmpFiles); return response()->json(['success' => $success]); } }