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.
135 lines
3.9 KiB
135 lines
3.9 KiB
<?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 App\SearchDisplace\SearchAndDisplace;
|
|
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()
|
|
]);
|
|
|
|
$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', storage_path('app/contracts/' . $originalFile), '--outdir', storage_path('app/contracts/') . $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
|
|
{
|
|
$fileId = request()->input('file_id');
|
|
$searchers = request()->input('searchers');
|
|
|
|
$xml = storage_path() . "/app/contracts/{$fileId}/{$fileId}.xml";
|
|
$dom = new \DOMDocument();
|
|
$dom->load($xml);
|
|
foreach($dom->getElementsByTagName('p') as $key => $text) {
|
|
$is_image = false;
|
|
if($text->childNodes) {
|
|
foreach($text->childNodes as $child) {
|
|
if(isset($child->tagName) && $child->tagName == 'draw:frame') {
|
|
$is_image = true;
|
|
}
|
|
}
|
|
}
|
|
if(!$is_image) {
|
|
$searchAndDisplace = new SearchAndDisplace(
|
|
stripslashes($text->textContent),
|
|
[
|
|
'searchers' => $searchers,
|
|
]
|
|
);
|
|
|
|
$response = $searchAndDisplace->execute();
|
|
$text->textContent = $response['content'];
|
|
}
|
|
}
|
|
|
|
$dom->save($xml);
|
|
|
|
$process = new Process(['soffice', '--convert-to', 'odt', $xml, '--outdir', storage_path('app/tmp/')]);
|
|
$process->run();
|
|
|
|
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}.odt"
|
|
]);
|
|
return response()->json(['success' => $success]);
|
|
}
|
|
}
|