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

148 lines
5 KiB
PHP
Raw Normal View History

2021-01-15 14:47:59 +01:00
<?php
namespace App\Http\Controllers;
use App\Models\Directories;
use App\Models\Documents;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException;
use Pion\Laravel\ChunkUpload\Handler\AbstractHandler;
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory;
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
use Illuminate\Http\Request;
class DocumentsController extends Controller
{
public function __construct()
{
$this->middleware('auth', ['except' => ['download']]);
}
public function index(Request $request) {
return view('documents.index', ['documents' => Documents::where('lang', '=', 'fr')->get()]);
}
public function add(Request $request) {
$folders = Directories::all();
return view('documents.add', ['folders' => $folders, 'docsFR' => Documents::where('lang', '=', 'fr')->orderBy('title', 'asc')->get()]);
}
public function store(Request $request) {
$tmp = $request->document;
$doc = new Documents();
$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(Request $request) {
$requestId = $request->id;
$doc = Documents::findOrFail($requestId);
$folders = Directories::all();
return view('documents.edit', ['document' => $doc, 'folders' => $folders]);
}
public function update(Request $request) {
$tmp = $request->document;
$doc = Documents::findOrFail($tmp['id']);
$doc->uuid = $tmp['uuid'];
$doc->title = $tmp['title'];
$doc->type = $tmp['type'];
$doc->directory = $tmp['directory'];
$doc->path = $tmp['path'];
if ($tmp['published'] === true || $tmp['published'] === 1){
$doc->published = 1;
}else{
$doc->published = 0;
}
$doc->save();
if ($doc->lang === 'fr') {
foreach ($doc->others_lang as $item) {
$item->directory = $doc->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 = Documents::where('uuid', $uuid)->firstOrFail();
$pathToFile = public_path() . Storage::url('docs/' . $doc->uuid . '.' . $doc->type);
return response()->download($pathToFile, $doc->path);
}
2021-07-20 14:45:46 +02:00
public function delete(Request $request)
{
$doc = Documents::findOrFail($request->id);
if ($doc->lang === 'fr') {
foreach ($doc->others_lang as $item) {
$item->delete();
}
}
$doc->delete();
return ['status' => 'OK'];
}
2021-01-15 14:47:59 +01:00
}