forked from projects/fipamo
Page Admin API, part 1
API accessible tasks (create, update, delete) have been updated to the new format. The controller needs to be cleaned up because it's a bit heavy and the new API flow still needs to be properly documented, but it's a good start
This commit is contained in:
parent
4337a20fb8
commit
321feb9b92
5 changed files with 92 additions and 28 deletions
|
@ -24,9 +24,9 @@ class FileUploadAPIController extends Controller
|
||||||
$this->settings = $settingsService;
|
$this->settings = $settingsService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function upload(Request $request, $type = null)
|
public function upload(Request $request)
|
||||||
{
|
{
|
||||||
$result = $this->upload->handleFile($request, $type);
|
$result = $this->upload->handleFile($request);
|
||||||
//update configs for specfic uploads
|
//update configs for specfic uploads
|
||||||
switch ($request['source']) {
|
switch ($request['source']) {
|
||||||
case 'avatar-upload':
|
case 'avatar-upload':
|
||||||
|
|
|
@ -3,38 +3,94 @@
|
||||||
namespace App\Http\Controllers\API;
|
namespace App\Http\Controllers\API;
|
||||||
|
|
||||||
use App\Interfaces\PageRepositoryInterface;
|
use App\Interfaces\PageRepositoryInterface;
|
||||||
|
use App\Interfaces\MemberRepositoryInterface;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Services\Assets\FileUploadService;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
use function _\find;
|
||||||
|
|
||||||
class PageAPIController extends Controller
|
class PageAPIController extends Controller
|
||||||
{
|
{
|
||||||
protected $pages;
|
protected $pages;
|
||||||
|
protected $member;
|
||||||
|
protected $upload;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
PageRepositoryInterface $pageRepository
|
PageRepositoryInterface $pageRepository,
|
||||||
|
MemberRepositoryInterface $memberRepo,
|
||||||
|
FileUploadService $fileUploadService,
|
||||||
) {
|
) {
|
||||||
$this->pages = $pageRepository;
|
$this->pages = $pageRepository;
|
||||||
|
$this->upload = $fileUploadService;
|
||||||
|
$this->member = $memberRepo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function write(Request $request)
|
public function update(Request $request)
|
||||||
{
|
{
|
||||||
$body = json_decode($request->getContent());
|
$body = $request->collect();
|
||||||
dd($body);
|
$images = $request->file('images');
|
||||||
//$result = $this->pages->update($body);
|
$apiKey = $request->header('fipamo-api-key');
|
||||||
//return response()->json($result)->header('Content-Type', 'application/json');
|
$imageList = '';
|
||||||
|
$fileList = '';
|
||||||
|
$body = $body->all();
|
||||||
|
//if new images are present, replace existing
|
||||||
|
//if not, keep current
|
||||||
|
if ($request->hasfile('images')) {
|
||||||
|
foreach ($images as $key => $image) {
|
||||||
|
$response = $this->upload->handlefile($image, false);
|
||||||
|
$imageList = $imageList . $response['filePath'] . ',';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$imageList = $body['imageList'];
|
||||||
|
}
|
||||||
|
$folks = $this->member->getAll();
|
||||||
|
$member = find($folks, ['key' => $apiKey]);
|
||||||
|
$request->session()->put('member', $member);
|
||||||
|
$body['slug'] = strtolower(str_replace(' ', '-', $body['title']));
|
||||||
|
$body['imageList'] = $imageList;
|
||||||
|
$body['fileList'] = $fileList;
|
||||||
|
$body = (object) $body;
|
||||||
|
$result = $this->pages->update($body);
|
||||||
|
session()->flush();
|
||||||
|
return response()->json($result)->header('Content-Type', 'application/json');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create(Request $request)
|
public function create(Request $request)
|
||||||
{
|
{
|
||||||
$body = json_decode($request->getContent());
|
$body = $request->collect();
|
||||||
$result = $this->pages->create($body);
|
$images = $request->file('images');
|
||||||
|
$apiKey = $request->header('fipamo-api-key');
|
||||||
|
$imageList = '';
|
||||||
|
$fileList = '';
|
||||||
|
if ($request->hasfile('images')) {
|
||||||
|
foreach ($images as $key => $image) {
|
||||||
|
$response = $this->upload->handlefile($image, false);
|
||||||
|
$imageList = $imageList . $response['filePath'] . ',';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$folks = $this->member->getAll();
|
||||||
|
$member = find($folks, ['key' => $apiKey]);
|
||||||
|
$request->session()->put('member', $member);
|
||||||
|
$body = $body->all();
|
||||||
|
$body['slug'] = strtolower(str_replace(' ', '-', $body['title']));
|
||||||
|
$body['imageList'] = $imageList;
|
||||||
|
$body['fileList'] = $fileList;
|
||||||
|
$body = (object) $body;
|
||||||
|
$result = $this->pages->create($body);
|
||||||
|
session()->flush();
|
||||||
return response()->json($result)->header('Content-Type', 'application/json');
|
return response()->json($result)->header('Content-Type', 'application/json');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete(Request $request)
|
public function delete(Request $request)
|
||||||
{
|
{
|
||||||
$body = json_decode($request->getContent());
|
$body = json_decode($request->getContent());
|
||||||
|
$apiKey = $request->header('fipamo-api-key');
|
||||||
|
$folks = $this->member->getAll();
|
||||||
|
$member = find($folks, ['key' => $apiKey]);
|
||||||
|
$request->session()->put('member', $member);
|
||||||
$result = $this->pages->delete($body);
|
$result = $this->pages->delete($body);
|
||||||
|
session()->flush();
|
||||||
return response()->json($result)->header('Content-Type', 'application/json');
|
return response()->json($result)->header('Content-Type', 'application/json');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,8 +65,7 @@ class RoutePostController extends Controller
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'upload':
|
case 'upload':
|
||||||
$type = null;
|
$result = $result = $this->upload->handleFile($request);
|
||||||
$result = $result = $this->upload->handleFile($request, $type);
|
|
||||||
//update configs for specfic uploads
|
//update configs for specfic uploads
|
||||||
switch ($request['source']) {
|
switch ($request['source']) {
|
||||||
case 'avatar-upload':
|
case 'avatar-upload':
|
||||||
|
|
|
@ -8,12 +8,17 @@ class FileUploadService
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function handleFile($request, $type = null)
|
public static function handleFile($request, $fileMode = true)
|
||||||
{
|
{
|
||||||
//$options = $request->getParsedBody();
|
//$options = $request->getParsedBody();
|
||||||
//front end sends one by one for progress tracking, so grab first
|
//front end sends one by one for progress tracking, so grab first
|
||||||
$file = $request->file('upload_files');
|
|
||||||
$type = $file[0]->extension();
|
if ($fileMode) {
|
||||||
|
$file = $request->file('upload_files')[0];
|
||||||
|
} else {
|
||||||
|
$file = $request;
|
||||||
|
}
|
||||||
|
$type = $file->extension();
|
||||||
$public_path = '../public';
|
$public_path = '../public';
|
||||||
$filesPath = '';
|
$filesPath = '';
|
||||||
$path = date('Y') . '/' . date('m');
|
$path = date('Y') . '/' . date('m');
|
||||||
|
@ -23,13 +28,17 @@ class FileUploadService
|
||||||
case 'jpg':
|
case 'jpg':
|
||||||
case 'png':
|
case 'png':
|
||||||
case 'gif':
|
case 'gif':
|
||||||
if (isset($request["source"])) {
|
if ($fileMode) {
|
||||||
if ($request["source"] == "avatar-upload") {
|
if (isset($request["source"])) {
|
||||||
$filesPath = '/assets/images/user/' . $path . '/';
|
if ($request["source"] == "avatar-upload") {
|
||||||
//Member::updateData('avi', $filesPath . $file->getClientFileName());
|
$filesPath = '/assets/images/user/' . $path . '/';
|
||||||
|
//Member::updateData('avi', $filesPath . $file->getClientFileName());
|
||||||
|
} else {
|
||||||
|
$filesPath = '/assets/images/user/' . $path . '/';
|
||||||
|
// Settings::updateGlobalData('background', $filesPath . '/' . $file->getClientFileName());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$filesPath = '/assets/images/user/' . $path . '/';
|
$filesPath = '/assets/images/blog/' . $path . '/';
|
||||||
// Settings::updateGlobalData('background', $filesPath . '/' . $file->getClientFileName());
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$filesPath = '/assets/images/blog/' . $path . '/';
|
$filesPath = '/assets/images/blog/' . $path . '/';
|
||||||
|
@ -55,16 +64,16 @@ class FileUploadService
|
||||||
if (!is_dir($public_path . $filesPath)) {
|
if (!is_dir($public_path . $filesPath)) {
|
||||||
mkdir($public_path . $filesPath, 0755, true);
|
mkdir($public_path . $filesPath, 0755, true);
|
||||||
}
|
}
|
||||||
$filename = urlencode($file[0]->getClientOriginalName());
|
$filename = urlencode($file->getClientOriginalName());
|
||||||
$file[0]->move($public_path . $filesPath, $filename);
|
$file->move($public_path . $filesPath, $filename);
|
||||||
} catch (RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
echo 'ERROR ' . $e->getMessage();
|
echo 'ERROR ' . $e->getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = [
|
$response = [
|
||||||
'message' => "File Uploaded. Great!",
|
'message' => "File Uploaded. Great!",
|
||||||
"filePath" => $filesPath . urlencode($file[0]->getClientOriginalName()),
|
"filePath" => $filesPath . urlencode($file->getClientOriginalName()),
|
||||||
"fileName" => urlencode($file[0]->getClientOriginalName()),
|
"fileName" => urlencode($file->getClientOriginalName()),
|
||||||
'type' => $type,
|
'type' => $type,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -23,8 +23,8 @@ Route::post("/v1/init", [InitAPIController::class, 'setupFresh']);
|
||||||
Route::post("/v1/restore", [InitAPIController::class, 'setupRestore']);
|
Route::post("/v1/restore", [InitAPIController::class, 'setupRestore']);
|
||||||
|
|
||||||
//handle page editing actions
|
//handle page editing actions
|
||||||
Route::group(['prefix' => '/v1/page', 'middleware' => 'validate.key'], function () {
|
Route::group(['prefix' => '/admin/page', 'middleware' => 'validate.key'], function () {
|
||||||
Route::put("/write", [PageAPIController::class, 'write']);
|
Route::post("/update", [PageAPIController::class, 'update']);
|
||||||
Route::post("/create", [PageAPIController::class, 'create']);
|
Route::post("/create", [PageAPIController::class, 'create']);
|
||||||
Route::delete("/delete", [PageAPIController::class, 'delete']);
|
Route::delete("/delete", [PageAPIController::class, 'delete']);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue