54 lines
1.7 KiB
PHP
54 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Exceptions\ProParserNotImplementedException;
|
|
use App\Models\Song;
|
|
use App\Services\ProImportService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ProFileController extends Controller
|
|
{
|
|
public function importPro(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'file' => ['required', 'file', 'max:51200'],
|
|
]);
|
|
|
|
$file = $request->file('file');
|
|
$extension = strtolower($file->getClientOriginalExtension());
|
|
|
|
if (! in_array($extension, ['pro', 'zip'])) {
|
|
return response()->json([
|
|
'message' => 'Nur .pro und .zip Dateien sind erlaubt.',
|
|
], 422);
|
|
}
|
|
|
|
try {
|
|
$service = new ProImportService();
|
|
$songs = $service->import($file);
|
|
|
|
return response()->json([
|
|
'message' => count($songs) === 1
|
|
? "Song \"{$songs[0]->title}\" erfolgreich importiert."
|
|
: count($songs) . ' Songs erfolgreich importiert.',
|
|
'songs' => collect($songs)->map(fn (Song $song) => [
|
|
'id' => $song->id,
|
|
'title' => $song->title,
|
|
'ccli_id' => $song->ccli_id,
|
|
]),
|
|
]);
|
|
} catch (\InvalidArgumentException $e) {
|
|
return response()->json(['message' => $e->getMessage()], 422);
|
|
} catch (\RuntimeException $e) {
|
|
return response()->json(['message' => $e->getMessage()], 422);
|
|
}
|
|
}
|
|
|
|
public function downloadPro(Song $song): JsonResponse
|
|
{
|
|
throw new ProParserNotImplementedException();
|
|
}
|
|
}
|