141 lines
4.8 KiB
PHP
141 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Directories;
|
|
use App\Models\Document;
|
|
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
|
|
{
|
|
public function index()
|
|
{
|
|
return view('document.index', ['documents' => Document::where('lang', '=', 'fr')->get()]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('document.create', ['folders' => Directories::all(), 'docsFR' => Document::where('lang', '=', 'fr')->orderBy('title', 'asc')->get()]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$tmp = $request->document;
|
|
$doc = new Document();
|
|
$doc->uuid = $tmp['uuid'];
|
|
$doc->title = $tmp['title'];
|
|
$doc->type = $tmp['type'];
|
|
$doc->directory = $tmp['directory'];
|
|
$doc->path = $tmp['path'];
|
|
$doc->lang = $tmp['lang'];
|
|
$doc->parent = $tmp['parent'];
|
|
if ($tmp['published'] === true || $tmp['published'] === 1) {
|
|
$doc->published = 1;
|
|
} else {
|
|
$doc->published = 0;
|
|
}
|
|
if ($doc->save()) {
|
|
return $doc->id;
|
|
}
|
|
return 'NOK';
|
|
}
|
|
|
|
public function edit(Document $document)
|
|
{
|
|
return view('document.edit', ['document' => $document, 'folders' => Directories::all()]);
|
|
}
|
|
|
|
public function update(Request $request, Document $document)
|
|
{
|
|
$tmp = $request->document;
|
|
$document->uuid = $tmp['uuid'];
|
|
$document->title = $tmp['title'];
|
|
$document->type = $tmp['type'];
|
|
$document->directory = $tmp['directory'];
|
|
$document->path = $tmp['path'];
|
|
if ($tmp['published'] === true || $tmp['published'] === 1) {
|
|
$document->published = 1;
|
|
} else {
|
|
$document->published = 0;
|
|
}
|
|
$document->save();
|
|
|
|
if ($document->lang === 'fr') {
|
|
foreach ($document->others_lang as $item) {
|
|
$item->directory = $document->directory;
|
|
$item->save();
|
|
}
|
|
}
|
|
|
|
return "OK";
|
|
}
|
|
|
|
public function upload(Request $request)
|
|
{
|
|
if (isset($request->uuid)) {
|
|
$uuid = $request->uuid;
|
|
} else {
|
|
$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)
|
|
{
|
|
if ($uuid === false) {
|
|
$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));
|
|
}
|
|
$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)
|
|
{
|
|
$doc = Document::where('uuid', $uuid)->firstOrFail();
|
|
$pathToFile = public_path() . Storage::url('docs/' . $doc->uuid . '.' . $doc->type);
|
|
return response()->download($pathToFile, $doc->path);
|
|
}
|
|
|
|
public function destroy(Request $request, Document $document)
|
|
{
|
|
if ($document->lang === 'fr') {
|
|
foreach ($document->others_lang as $item) {
|
|
$item->delete();
|
|
}
|
|
}
|
|
|
|
$document->delete();
|
|
|
|
return ['status' => 'OK'];
|
|
}
|
|
}
|