From c480f388487d6b58f3972a72e25bbdffca1eb02a Mon Sep 17 00:00:00 2001 From: Alex Puiu Date: Mon, 28 Feb 2022 09:26:51 +0200 Subject: [PATCH] Fix pdf conversion on download. --- app/Http/Controllers/FileController.php | 15 ++++++++++----- app/SearchDisplace/SearchAndDisplaceXML.php | 6 ++++-- .../js/components/ProcessFile/ProcessFile.ts | 2 +- resources/js/interfaces/FileData.ts | 1 + resources/js/services/ApiService.ts | 7 +++++-- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/FileController.php b/app/Http/Controllers/FileController.php index e31445e..e1133cd 100644 --- a/app/Http/Controllers/FileController.php +++ b/app/Http/Controllers/FileController.php @@ -12,6 +12,11 @@ use Symfony\Component\Process\Process; class FileController extends Controller { + public function __construct() + { + $this->directoryPath = storage_path("app/contracts/"); + } + /** * * @return JsonResponse @@ -45,7 +50,7 @@ class FileController extends Controller Storage::disk('local')->put("contracts/{$originalFile}", file_get_contents($file)); // keep the original file - $process = new Process(['soffice', '--convert-to', 'xml', storage_path('app/contracts/' . $originalFile), '--outdir', storage_path('app/contracts/') . $fileId]); + $process = new Process(['soffice', '--convert-to', 'xml', $this->directoryPath . $originalFile, '--outdir', $this->directoryPath . $fileId]); $process->run(); Storage::delete("contracts/{$originalFile}"); @@ -71,15 +76,15 @@ class FileController extends Controller */ public function convert(): JsonResponse { - $fileId = request()->input('file_id'); + $file = (object) request()->input('file'); $searchers = request()->input('searchers'); - $xml = storage_path() . "/app/contracts/{$fileId}/{$fileId}.xml"; + $xml = $this->directoryPath . "{$file->id}/{$file->id}.xml"; - (new SearchAndDisplaceXML($xml, $searchers))->execute(); + (new SearchAndDisplaceXML($xml, $searchers, $file->type))->execute(); return response()->json([ - 'path' => 'tmp/' . $fileId . '.odt' + 'path' => "tmp/{$file->id}.{$file->type}" ]); } diff --git a/app/SearchDisplace/SearchAndDisplaceXML.php b/app/SearchDisplace/SearchAndDisplaceXML.php index b03a664..bcaa9ce 100644 --- a/app/SearchDisplace/SearchAndDisplaceXML.php +++ b/app/SearchDisplace/SearchAndDisplaceXML.php @@ -8,11 +8,13 @@ class SearchAndDisplaceXML { protected $file; protected $searchers; + protected $type; - public function __construct($file, $searchers) + public function __construct($file, $searchers, $type = 'odt') { $this->file = $file; $this->searchers = $searchers; + $this->type = $type; } public function execute() @@ -24,7 +26,7 @@ class SearchAndDisplaceXML protected function convertToOdt() { - (new Process(['soffice', '--convert-to', 'odt', $this->file, '--outdir', storage_path('app/tmp/')]))->run(); + (new Process(['soffice', '--convert-to', $this->type, $this->file, '--outdir', storage_path('app/tmp/')]))->run(); } protected function applySD() diff --git a/resources/js/components/ProcessFile/ProcessFile.ts b/resources/js/components/ProcessFile/ProcessFile.ts index 410101b..ecb8132 100644 --- a/resources/js/components/ProcessFile/ProcessFile.ts +++ b/resources/js/components/ProcessFile/ProcessFile.ts @@ -428,7 +428,7 @@ export default class ProcessFile extends Vue { }); }); - let response = await this.$api.convertFile(this.processedFileContent, this.file.id, searchers); + let response = await this.$api.convertFile(this.processedFileContent, {id: this.file.id, name: this.file.file_name}, searchers); window.open(`${window.location.origin}/file/download/` + response.path); } diff --git a/resources/js/interfaces/FileData.ts b/resources/js/interfaces/FileData.ts index d428fb2..e2e816b 100644 --- a/resources/js/interfaces/FileData.ts +++ b/resources/js/interfaces/FileData.ts @@ -3,4 +3,5 @@ export interface FileData file: string; id: string; path: string; + file_name?: string; } \ No newline at end of file diff --git a/resources/js/services/ApiService.ts b/resources/js/services/ApiService.ts index 07ea8be..2d6dac9 100644 --- a/resources/js/services/ApiService.ts +++ b/resources/js/services/ApiService.ts @@ -145,12 +145,15 @@ export default class ApiService { * * @returns */ - public async convertFile(content: string, fileId: string, searchers: Array<{ key: string; type: string; value: string; }>) { + public async convertFile(content: string, file: object, searchers: Array<{ key: string; type: string; value: string; }>) { try { let response = await axios.post( this.apiRoutes.fileDownload, { - 'file_id': fileId, + 'file': { + 'id': file.id, + 'type': (file.name.substring(file.name.lastIndexOf('.') + 1, file.name.length) !== 'pdf') ? 'odt' : 'pdf' + }, 'content': content, 'searchers': searchers }