Skip to content

Commit

Permalink
fix: move api routes to v1/api
Browse files Browse the repository at this point in the history
feat: add ImageController to retrieve public files
  • Loading branch information
Tomut0 committed Dec 14, 2024
1 parent 4585e90 commit 36f6aa6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
40 changes: 40 additions & 0 deletions app/Http/Controllers/ImageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class ImageController extends Controller
{
public function index(Request $request): JsonResponse
{
$category = $request->input('category');
$basePath = public_path("images");

// If category is provided, validate it
if ($category && !preg_match('/^[a-zA-Z0-9_\-]+$/', $category)) {
return response()->json(['error' => 'Invalid category'], 400);
}

$path = $category ? "$basePath/$category" : $basePath;

// Check if the directory exists
if (!is_dir($path)) {
return response()->json(['error' => 'Category not found'], 404);
}

// Get all files recursively if category is null, otherwise limit to the specific category
$files = File::allFiles($path);

// Generate URLs for all found files
$imageUrls = array_map(function ($file) use ($category) {
// Generate the relative URL for each image
$relativePath = isset($category) ? "images/$category/{$file->getRelativePath()}" : "images/{$file->getRelativePath()}";
return url($relativePath . $file->getFilename());
}, $files);

return response()->json($imageUrls);
}
}
5 changes: 3 additions & 2 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public function boot(): void

$this->routes(function () {
Route::middleware('api')
->prefix('api')
->group(base_path('routes/api.php'));
->prefix('api/v1')
->as('v1:')
->group(base_path('routes/v1/api.php'));

Route::middleware('web')
->group(base_path('routes/web.php'));
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Components/Translator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let flags = ref(), sortedFlags = ref(new Proxy({}, {}));
// Lazy loading languages
watch(showPopup, async (value) => {
if (value && !flags.value) {
const localesPromise = await axios.get(route("locales.status"));
const localesPromise = await axios.get(route("v1:locales.status"));
flags.value = localesPromise.data;
sortedFlags.value = flags.value;
}
Expand Down
5 changes: 5 additions & 0 deletions routes/api.php → routes/v1/api.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Http\Controllers\ImageController;
use App\Http\Controllers\LeaderboardController;
use App\Http\Controllers\SearchController;
use App\Http\Controllers\TranslationController;
Expand All @@ -26,3 +27,7 @@
Route::get('/locales/status', [TranslationController::class, 'localizationMap'])->name('locales.status');

Route::get('/clans', [LeaderboardController::class, 'index']);

Route::prefix('images')->name('images.')->group(function () {
Route::get('/', [ImageController::class, 'index'])->name('index');
});

0 comments on commit 36f6aa6

Please sign in to comment.