2FAuth/app/Api/v1/Controllers/IconController.php

66 lines
1.7 KiB
PHP
Raw Normal View History

2020-01-05 23:23:48 +01:00
<?php
namespace App\Api\v1\Controllers;
2020-01-05 23:23:48 +01:00
2023-06-30 14:50:57 +02:00
use App\Api\v1\Requests\IconFetchRequest;
use App\Http\Controllers\Controller;
use App\Models\TwoFAccount;
use App\Services\LogoService;
2022-11-22 15:15:52 +01:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
2020-01-05 23:23:48 +01:00
class IconController extends Controller
{
/**
2020-01-08 15:24:34 +01:00
* Handle uploaded icon image
2020-01-05 23:23:48 +01:00
*
2021-11-26 11:18:58 +01:00
* @return \Illuminate\Http\JsonResponse
2020-01-05 23:23:48 +01:00
*/
public function upload(Request $request)
{
$this->validate($request, [
'icon' => 'required|image',
]);
$icon = $request->file('icon');
$path = $icon instanceof \Illuminate\Http\UploadedFile ? $icon->store('', 'icons') : false;
return $path
? response()->json(['filename' => pathinfo($path)['basename']], 201)
: response()->json(['message' => __('errors.file_upload_failed')], 500);
2020-01-05 23:23:48 +01:00
}
/**
* Fetch a logo
*
* @return \Illuminate\Http\JsonResponse
*/
2023-06-30 14:50:57 +02:00
public function fetch(IconFetchRequest $request, LogoService $logoService)
{
2023-06-30 14:50:57 +02:00
$validated = $request->validated();
2022-11-22 15:15:52 +01:00
2023-06-30 14:50:57 +02:00
$icon = $logoService->getIcon($validated['service']);
return $icon
? response()->json(['filename' => $icon], 201)
: response()->json(null, 204);
}
2020-01-08 15:24:34 +01:00
/**
2020-01-08 15:24:34 +01:00
* delete an icon
*
2021-11-26 11:18:58 +01:00
* @return \Illuminate\Http\JsonResponse
2020-01-08 15:24:34 +01:00
*/
public function delete(string $icon, Request $request)
2020-01-08 15:24:34 +01:00
{
// An icon affected to someone else's twofaccount cannot be deleted
if ($icon && TwoFAccount::where('icon', $icon)->where('user_id', '<>', $request->user()->id)->count() > 0) {
abort(403, 'unauthorized');
}
2022-11-22 15:15:52 +01:00
Storage::disk('icons')->delete($icon);
2020-01-08 15:24:34 +01:00
return response()->json(null, 204);
}
2022-11-22 15:15:52 +01:00
}