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:
ro 2024-07-23 17:46:45 -06:00
parent 4337a20fb8
commit 321feb9b92
No known key found for this signature in database
GPG key ID: 29B551CDBD4D3B50
5 changed files with 92 additions and 28 deletions

View file

@ -24,9 +24,9 @@ class FileUploadAPIController extends Controller
$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
switch ($request['source']) {
case 'avatar-upload':

View file

@ -3,38 +3,94 @@
namespace App\Http\Controllers\API;
use App\Interfaces\PageRepositoryInterface;
use App\Interfaces\MemberRepositoryInterface;
use App\Http\Controllers\Controller;
use App\Services\Assets\FileUploadService;
use Illuminate\Http\Request;
use function _\find;
class PageAPIController extends Controller
{
protected $pages;
protected $member;
protected $upload;
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());
dd($body);
//$result = $this->pages->update($body);
//return response()->json($result)->header('Content-Type', 'application/json');
$body = $request->collect();
$images = $request->file('images');
$apiKey = $request->header('fipamo-api-key');
$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)
{
$body = json_decode($request->getContent());
$result = $this->pages->create($body);
$body = $request->collect();
$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');
}
public function delete(Request $request)
{
$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);
session()->flush();
return response()->json($result)->header('Content-Type', 'application/json');
}
}

View file

@ -65,8 +65,7 @@ class RoutePostController extends Controller
}
break;
case 'upload':
$type = null;
$result = $result = $this->upload->handleFile($request, $type);
$result = $result = $this->upload->handleFile($request);
//update configs for specfic uploads
switch ($request['source']) {
case 'avatar-upload':

View file

@ -8,12 +8,17 @@ class FileUploadService
{
}
public static function handleFile($request, $type = null)
public static function handleFile($request, $fileMode = true)
{
//$options = $request->getParsedBody();
//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';
$filesPath = '';
$path = date('Y') . '/' . date('m');
@ -23,13 +28,17 @@ class FileUploadService
case 'jpg':
case 'png':
case 'gif':
if (isset($request["source"])) {
if ($request["source"] == "avatar-upload") {
$filesPath = '/assets/images/user/' . $path . '/';
//Member::updateData('avi', $filesPath . $file->getClientFileName());
if ($fileMode) {
if (isset($request["source"])) {
if ($request["source"] == "avatar-upload") {
$filesPath = '/assets/images/user/' . $path . '/';
//Member::updateData('avi', $filesPath . $file->getClientFileName());
} else {
$filesPath = '/assets/images/user/' . $path . '/';
// Settings::updateGlobalData('background', $filesPath . '/' . $file->getClientFileName());
}
} else {
$filesPath = '/assets/images/user/' . $path . '/';
// Settings::updateGlobalData('background', $filesPath . '/' . $file->getClientFileName());
$filesPath = '/assets/images/blog/' . $path . '/';
}
} else {
$filesPath = '/assets/images/blog/' . $path . '/';
@ -55,16 +64,16 @@ class FileUploadService
if (!is_dir($public_path . $filesPath)) {
mkdir($public_path . $filesPath, 0755, true);
}
$filename = urlencode($file[0]->getClientOriginalName());
$file[0]->move($public_path . $filesPath, $filename);
$filename = urlencode($file->getClientOriginalName());
$file->move($public_path . $filesPath, $filename);
} catch (RuntimeException $e) {
echo 'ERROR ' . $e->getMessage();
}
$response = [
'message' => "File Uploaded. Great!",
"filePath" => $filesPath . urlencode($file[0]->getClientOriginalName()),
"fileName" => urlencode($file[0]->getClientOriginalName()),
"filePath" => $filesPath . urlencode($file->getClientOriginalName()),
"fileName" => urlencode($file->getClientOriginalName()),
'type' => $type,
];

View file

@ -23,8 +23,8 @@ Route::post("/v1/init", [InitAPIController::class, 'setupFresh']);
Route::post("/v1/restore", [InitAPIController::class, 'setupRestore']);
//handle page editing actions
Route::group(['prefix' => '/v1/page', 'middleware' => 'validate.key'], function () {
Route::put("/write", [PageAPIController::class, 'write']);
Route::group(['prefix' => '/admin/page', 'middleware' => 'validate.key'], function () {
Route::post("/update", [PageAPIController::class, 'update']);
Route::post("/create", [PageAPIController::class, 'create']);
Route::delete("/delete", [PageAPIController::class, 'delete']);
});