ldc-com-app-bo/app/Http/Controllers/DocumentsController.php

142 lines
4.8 KiB
PHP
Raw Normal View History

2021-01-15 14:47:59 +01:00
<?php
namespace App\Http\Controllers;
use App\Models\Directories;
2022-05-11 12:36:27 +02:00
use App\Models\Document;
2021-01-15 14:47:59 +01:00
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException;
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory;
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
use Illuminate\Http\Request;
class DocumentsController extends Controller
{
2022-05-11 12:36:27 +02:00
public function index()
2021-01-15 14:47:59 +01:00
{
2022-05-11 12:36:27 +02:00
return view('document.index', ['documents' => Document::where('lang', '=', 'fr')->get()]);
2021-01-15 14:47:59 +01:00
}
2022-05-11 12:36:27 +02:00
public function create()
{
return view('document.create', ['folders' => Directories::all(), 'docsFR' => Document::where('lang', '=', 'fr')->orderBy('title', 'asc')->get()]);
2021-01-15 14:47:59 +01:00
}
2022-09-29 10:09:43 +02:00
public function store(Request $request)
{
$tmp = $request->document;
$doc = new Document();
$doc->uuid = $tmp['uuid'];
$doc->title = $tmp['title'];
$doc->type = $tmp['type'];
2021-01-15 14:47:59 +01:00
$doc->directory = $tmp['directory'];
2022-09-29 10:09:43 +02:00
$doc->path = $tmp['path'];
$doc->lang = $tmp['lang'];
$doc->parent = $tmp['parent'];
if ($tmp['published'] === true || $tmp['published'] === 1) {
2021-01-15 14:47:59 +01:00
$doc->published = 1;
2022-09-29 10:09:43 +02:00
} else {
2021-01-15 14:47:59 +01:00
$doc->published = 0;
}
2022-09-29 10:09:43 +02:00
if ($doc->save()) {
2021-01-15 14:47:59 +01:00
return $doc->id;
}
return 'NOK';
}
2022-05-11 12:36:27 +02:00
public function edit(Document $document)
{
return view('document.edit', ['document' => $document, 'folders' => Directories::all()]);
2021-01-15 14:47:59 +01:00
}
2022-05-11 12:36:27 +02:00
public function update(Request $request, Document $document)
{
2022-09-29 10:09:43 +02:00
$tmp = $request->document;
$document->uuid = $tmp['uuid'];
$document->title = $tmp['title'];
$document->type = $tmp['type'];
2022-05-11 12:36:27 +02:00
$document->directory = $tmp['directory'];
2022-09-29 10:09:43 +02:00
$document->path = $tmp['path'];
if ($tmp['published'] === true || $tmp['published'] === 1) {
2022-05-11 12:36:27 +02:00
$document->published = 1;
2022-09-29 10:09:43 +02:00
} else {
2022-05-11 12:36:27 +02:00
$document->published = 0;
2021-01-15 14:47:59 +01:00
}
2022-05-11 12:36:27 +02:00
$document->save();
2021-01-15 14:47:59 +01:00
2022-05-11 12:36:27 +02:00
if ($document->lang === 'fr') {
foreach ($document->others_lang as $item) {
$item->directory = $document->directory;
2021-01-15 14:47:59 +01:00
$item->save();
}
}
return "OK";
}
2022-09-29 10:09:43 +02:00
public function upload(Request $request)
{
2021-01-15 14:47:59 +01:00
if (isset($request->uuid)) {
$uuid = $request->uuid;
2022-09-29 10:09:43 +02:00
} else {
2021-01-15 14:47:59 +01:00
$uuid = false;
}
// create the file receiver
$receiver = new FileReceiver("file", $request, HandlerFactory::classFromRequest($request));
// check if the upload is success, throw exception or return response you need
if ($receiver->isUploaded() === false) {
throw new UploadMissingFileException();
}
// receive the file
$save = $receiver->receive();
// check if the upload has finished (in chunk mode it will send smaller files)
if ($save->isFinished()) {
// save the file and return any response you need, current example uses `move` function. If you are
// not using move, you need to manually delete the file by unlink($save->getFile()->getPathname())
return $this->saveFile($save->getFile(), $uuid);
}
// we are in chunk mode, lets send the current progress
/** @var AbstractHandler $handler */
$handler = $save->handler();
return response()->json([
"done" => $handler->getPercentageDone(),
'status' => true
]);
}
protected function saveFile(UploadedFile $file, $uuid)
{
2022-09-29 10:09:43 +02:00
if ($uuid === false) {
2021-01-15 14:47:59 +01:00
$uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
2022-09-29 10:09:43 +02:00
}
2021-01-15 14:47:59 +01:00
$finalPath = storage_path('app/public/docs/');
// move the file name
$file->move($finalPath, $uuid . '.' . $file->getClientOriginalExtension());
return response()->json([
'uid' => $uuid,
]);
}
/** Download the document */
public function download(Request $request, $uuid)
{
2022-09-29 10:09:43 +02:00
$doc = Document::where('uuid', $uuid)->firstOrFail();
2021-01-15 14:47:59 +01:00
$pathToFile = public_path() . Storage::url('docs/' . $doc->uuid . '.' . $doc->type);
return response()->download($pathToFile, $doc->path);
}
2021-07-20 14:45:46 +02:00
2022-05-11 12:36:27 +02:00
public function destroy(Request $request, Document $document)
2021-07-20 14:45:46 +02:00
{
2022-05-11 12:36:27 +02:00
if ($document->lang === 'fr') {
foreach ($document->others_lang as $item) {
2021-07-20 14:45:46 +02:00
$item->delete();
}
}
2022-05-11 12:36:27 +02:00
$document->delete();
2021-07-20 14:45:46 +02:00
return ['status' => 'OK'];
}
2021-01-15 14:47:59 +01:00
}