2020-01-05 23:23:48 +01:00
|
|
|
<?php
|
|
|
|
|
2021-11-07 21:57:22 +01:00
|
|
|
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;
|
2024-10-18 14:28:45 +02:00
|
|
|
use App\Facades\IconStore;
|
|
|
|
use App\Helpers\Helpers;
|
2021-11-07 21:57:22 +01:00
|
|
|
use App\Http\Controllers\Controller;
|
2023-02-23 16:40:53 +01:00
|
|
|
use App\Models\TwoFAccount;
|
2022-07-20 13:32:31 +02:00
|
|
|
use App\Services\LogoService;
|
2024-10-18 14:28:45 +02:00
|
|
|
use Exception;
|
2022-11-22 15:15:52 +01:00
|
|
|
use Illuminate\Http\Request;
|
2024-10-18 14:28:45 +02:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2020-01-05 23:23:48 +01:00
|
|
|
|
|
|
|
class IconController extends Controller
|
|
|
|
{
|
2022-07-20 13:32:31 +02:00
|
|
|
/**
|
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)
|
|
|
|
{
|
2020-01-19 23:02:20 +01:00
|
|
|
$this->validate($request, [
|
2020-01-09 16:33:32 +01:00
|
|
|
'icon' => 'required|image',
|
2020-01-12 19:55:17 +01:00
|
|
|
]);
|
2020-01-09 16:33:32 +01:00
|
|
|
|
2024-10-18 14:28:45 +02:00
|
|
|
$icon = $request->file('icon');
|
|
|
|
$isStored = $name = false;
|
2022-09-02 14:28:57 +02:00
|
|
|
|
2024-10-18 14:28:45 +02:00
|
|
|
if ($icon instanceof UploadedFile) {
|
|
|
|
try {
|
|
|
|
if ($content = $icon->get()) {
|
|
|
|
$name = Helpers::getRandomFilename($icon->extension());
|
|
|
|
$isStored = IconStore::store($name, $content);
|
|
|
|
}
|
2024-11-09 10:18:45 +01:00
|
|
|
} catch (Exception) {
|
2024-10-18 14:28:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $isStored
|
|
|
|
? response()->json(['filename' => $name], 201)
|
2022-09-02 14:28:57 +02:00
|
|
|
: response()->json(['message' => __('errors.file_upload_failed')], 500);
|
2020-01-05 23:23:48 +01:00
|
|
|
}
|
2022-07-20 13:32:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch a logo
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2023-06-30 14:50:57 +02:00
|
|
|
public function fetch(IconFetchRequest $request, LogoService $logoService)
|
2022-07-20 13:32:31 +02:00
|
|
|
{
|
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']);
|
2022-07-20 13:32:31 +02:00
|
|
|
|
|
|
|
return $icon
|
|
|
|
? response()->json(['filename' => $icon], 201)
|
|
|
|
: response()->json(null, 204);
|
|
|
|
}
|
2020-01-08 15:24:34 +01:00
|
|
|
|
2022-07-20 13:32:31 +02: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
|
|
|
*/
|
2023-02-23 16:40:53 +01:00
|
|
|
public function delete(string $icon, Request $request)
|
2020-01-08 15:24:34 +01:00
|
|
|
{
|
2023-02-23 16:40:53 +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');
|
|
|
|
}
|
|
|
|
|
2024-10-18 14:28:45 +02:00
|
|
|
IconStore::delete($icon);
|
2020-01-08 15:24:34 +01:00
|
|
|
|
|
|
|
return response()->json(null, 204);
|
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|