Browse Source

Fix pdf conversion on download.

master
Alex Puiu 2 years ago
parent
commit
c480f38848
  1. 15
      app/Http/Controllers/FileController.php
  2. 6
      app/SearchDisplace/SearchAndDisplaceXML.php
  3. 2
      resources/js/components/ProcessFile/ProcessFile.ts
  4. 1
      resources/js/interfaces/FileData.ts
  5. 7
      resources/js/services/ApiService.ts

15
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}"
]);
}

6
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()

2
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);
}

1
resources/js/interfaces/FileData.ts

@ -3,4 +3,5 @@ export interface FileData
file: string;
id: string;
path: string;
file_name?: string;
}

7
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
}

Loading…
Cancel
Save