Adding api routes to list folders and move media

parent 245264eb
<?php
namespace Modules\Media\Http\Controllers\Api;
use Illuminate\Routing\Controller;
use Modules\Media\Repositories\FolderRepository;
class AllNestableFolderController extends Controller
{
/**
* @var FolderRepository
*/
private $folder;
public function __construct(FolderRepository $folder)
{
$this->folder = $folder;
}
public function __invoke()
{
$array = [];
$folders = $this->folder->allNested()->nest()->listsFlattened('filename', null, 0, $array, '--- ');
return response()->json($folders);
}
}
<?php
namespace Modules\Media\Http\Controllers\Api;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Media\Repositories\FileRepository;
use Modules\Media\Repositories\FolderRepository;
class MoveMediaController extends Controller
{
/**
* @var FileRepository
*/
private $file;
/**
* @var FolderRepository
*/
private $folder;
public function __construct(FileRepository $file, FolderRepository $folder)
{
$this->file = $file;
$this->folder = $folder;
}
public function __invoke(Request $request)
{
foreach ($request->get('files') as $file) {
$file = $this->file->find($file['id']);
if ($file->is_folder === false) {
$destination = $this->folder->findFolder($request->get('destinationFolder'));
$this->file->move($file, $destination);
}
}
return response()->json([
'errors' => false,
'message' => 'Files moved successfully',
]);
}
}
......@@ -4,6 +4,11 @@ use Illuminate\Routing\Router;
/** @var Router $router */
$router->group(['middleware' => 'api.token'], function (Router $router) {
$router->get('folder', [
'uses' => 'AllNestableFolderController',
'as' => 'api.media.folders.all-nestable',
'middleware' => 'token-can:media.folders.index',
]);
$router->post('folder', [
'uses' => 'FolderController@store',
'as' => 'api.media.folders.store',
......@@ -42,6 +47,10 @@ $router->group(['middleware' => 'api.token'], function (Router $router) {
'uses' => 'MediaController@unlinkMedia',
'as' => 'api.media.unlink',
]);
$router->post('media/move', [
'uses' => 'MoveMediaController',
'as' => 'api.media.media.move',
]);
$router->get('media/all', [
'uses' => 'MediaController@all',
'as' => 'api.media.all',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment