Files
c-cms-legacy/app/Http/Controllers/GoogleDriveController.php
Lagacé Mathieu 8189bbce9a Au cas ou
2020-03-05 08:59:10 -05:00

333 lines
9.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Psy\Util\Str;
use Symfony\Component\Console\Input\Input;
class GoogleDriveController extends Controller
{
/**
* Display a listing of the resource.
*
* @param string $dir
* @param bool $recursive
* @return \Illuminate\Http\Response
*/
public function index($folder = '')
{
return view('admin.files.Google Drive.index',['folder' => $folder]);
}
/**
* Show the form for creating a new file.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function createFile()
{
Storage::cloud()->put(\request('currentDir'.'/'.\request('name')), '');
return back()->with('success','Fichier créer avec succès');
}
public function createFolder()
{
Storage::cloud()->makeDirectory(\request('currentDir').'/'.\request('name'));
return back()->with('success','Dossier créer avec succès');
}
public function uploadFile()
{
Storage::cloud()->putFileAs(\request('currentDir'),\request()->file('fichier'),\request()->file('fichier')->getClientOriginalName());
return back()->with('success','Fichier téléversé avec succès');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
public function store(Request $request)
{
$path = Storage::cloud()->makeDirectory('Test Dir');
dd($path);
}
/**
* Display the specified resource.
*
* @param $filename
* @return \Illuminate\Http\Response
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function show()
{
$filename = \request('file');
$contents = collect(Storage::cloud()->listContents('/1nEe35-GvLX598RketTI-UoaOxIMNxfka', true));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($filename, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($filename, PATHINFO_EXTENSION))
->first(); // there can be duplicate file names!
//return $file; // array with file info
$rawData = Storage::cloud()->get($file['path']);
return response($rawData, 200)
->header('ContentType', $file['mimetype'])
->header('Content-Disposition', "attachment; filename='$filename'");
}
public function showMetadata($dir,$file)
{
if ($dir == 'root')
{
$dir = '/';
}
$contents = collect(Storage::cloud()->listContents($dir, true));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($file, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($file, PATHINFO_EXTENSION))
->first(); // there can be duplicate file names!
return $file;
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
public function getPathArray()
{
$contents = collect(Storage::cloud()->listContents('/', true));
return json_encode($contents->where('type', '=', 'dir'));
}
public function getPath($folder)
{
$contents = collect(Storage::cloud()->listContents('/', true));
$dir = collect($contents->where('type', '=', 'dir'));
foreach ($dir as $d)
{
if($d['basename'] == $folder)
{
return $d['dirname'];
}
}
}
public function getFile()
{
if(\request('f'))
{
$dir = '/';
if (\request('d') && urldecode(\request('d')) != '')
{
$dir = \request('d');
}
$filename = urldecode(\request('f'));
$recursive = false; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents($dir, $recursive));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($filename, PATHINFO_FILENAME))
->first(); // there can be duplicate file names!
$rawData = Storage::cloud()->get($file['path']);
$filename = urlencode($filename);
return response($rawData, 200)
->header('Content-Type', $file['mimetype'])
->header('Content-Disposition', "attachment; filename=$filename");
}
else
{
abort(404);
}
}
public function deleteFile()
{
if(\request('f'))
{
$dir = '/';
if (\request('d') && urldecode(\request('d')) != '')
{
$dir = \request('d');
}
$filename = urldecode(\request('f'));
$recursive = false; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents($dir, $recursive));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($filename, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($filename, PATHINFO_EXTENSION))
->first(); // there can be duplicate file names!
Storage::cloud()->delete($file['path']);
return back()->with('success','Fichier supprimé avec succès');
}
else
{
abort(404);
}
}
public function deleteDir()
{
$directoryID = \request('d');
// Now find that directory and use its ID (path) to delete it
$dir = '/';
$recursive = false; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents($dir, $recursive));
$directory = $contents
->where('type', '=', 'dir')
->where('basename', '=', $directoryID)
->first();
Storage::cloud()->deleteDirectory($directory['path']);
return back()->with('success','Dossier supprimé avec succès');
}
public function list($folder = 'root')
{
$recursive = false; // Get subdirectories also?
if ($folder == 'root')
{
$contents = collect(Storage::cloud()->listContents('/', $recursive));
}
else
{
$contents = collect(Storage::cloud()->listContents($folder, $recursive));
}
//dd($contents);
//$meta = collect(Storage::cloud()->listContents($folder, true));
//dd($meta);
return view('admin.files.Google Drive.explorer',['directories' => $contents->where('type', '=', 'dir')->sortByDesc('name'), 'files' => $contents->where('type', '=', 'file'), 'currentDir' => $folder]);
}
public function checkFileSystem()
{
$error = [];
if(\App\Config::getData('is_Google_Drive_enabled'))
{
$structure = $this->getFileStructure();
$this->checkStructure($structure,'/','/',$error);
}
return $error;
}
public function checkStructure($structure,$parent,$id,&$error)
{
$mydir = $this->listLockDirectory($id);
foreach ($structure as $key => $value)
{
$found = false;
$newDirID = null;
$p = null;
foreach ($mydir as $dir)
{
$p = $dir['basename'];
if ($dir['extension'] == $key)
{
$found = true;
$newDirID = $dir['basename'];
break;
}
}
if (!$found)
{
array_push($error,'Dossier 🔒.'.$key.' manquant... Le dossier a été créer');
\Storage::cloud()->createDir($parent.'/🔒.'.$key);
$tempdir = $this->listLockDirectory($parent);
$p = $tempdir->where('extension','=',$key)->first()['basename'];
}
if ($value != [])
{
$this->checkStructure($value,$p,$newDirID,$error);
}
}
}
public function getFileStructure()
{
return collect([
'Privé' => [
'Cadet' => [],
'ETAMAS' => [],
'Officier' => [],
'Staff' => [
'Guide' => []
]
],
'Publique' => [
'Fichier' => [],
'Image' => []
],
'Système' => [
'Fichier' => [],
'Image' => [
'Nouvelle' => [],
'Profil' => []
]
]
]);
}
public function listLockDirectory($d)
{
$contents = collect(\Storage::cloud()->listContents($d, false));
$dir = $contents->where('type', '=', 'dir');
$dir = $dir->where('filename','=','🔒');
return $dir;
}
}