feat: player + tracks list

This commit is contained in:
2025-06-07 11:37:24 +03:00
parent 7cf08951eb
commit 752a4c1891
14 changed files with 512 additions and 48 deletions

View File

@@ -0,0 +1,104 @@
<?php
declare(strict_types=1);
// SPDX-FileCopyrightText: Chen Asraf <contact@casraf.dev>
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace OCA\Jukebox\Controller;
use OCA\Jukebox\Db\JukeboxMediaMapper;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\OCSController;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
class ApiController extends OCSController {
public function __construct(
string $appName,
IRequest $request,
private IAppConfig $config,
private IL10N $l,
private JukeboxMediaMapper $mediaMapper,
private IUserSession $userSession,
private IRootFolder $rootFolder,
private LoggerInterface $logger,
) {
parent::__construct($appName, $request);
}
/**
* List all tracks for the current user
*
* @return JSONResponse<Http::STATUS_OK, array{tracks: list<array<string, mixed>>}, array{}>
*
* 200: List of media tracks for current user
*/
#[ApiRoute(verb: 'GET', url: '/api/tracks')]
public function listTracks(): JSONResponse {
$user = $this->userSession->getUser();
if (!$user) {
return new JSONResponse(['message' => 'Unauthenticated'], Http::STATUS_UNAUTHORIZED);
}
$tracks = $this->mediaMapper->findByMediaType($user->getUID(), 'track');
return new JSONResponse(['tracks' => array_map(fn ($t) => $t->jsonSerialize(), $tracks)]);
}
/**
* Stream a track file for playback
*
* @param int $id Track ID
*
* @return FileDisplayResponse<Http::STATUS_OK, array{}>
* | JSONResponse<Http::STATUS_UNAUTHORIZED, array{message: string}, array{}>
* | JSONResponse<Http::STATUS_FORBIDDEN, array{message: string}, array{}>
* | JSONResponse<Http::STATUS_NOT_FOUND, array{message: string}, array{}>
*
* 200: File response returned successfully
* 401: User not authenticated
* 403: Track does not belong to current user
* 404: Track file or record not found
*/
#[ApiRoute(verb: 'GET', url: '/api/tracks/{id}/stream')]
#[NoCSRFRequired]
public function streamTrack(int $id): FileDisplayResponse|JSONResponse {
$this->logger->info('Received request to stream track with ID: ' . $id);
$user = $this->userSession->getUser();
if (!$user) {
return new JSONResponse(['message' => 'Unauthenticated'], Http::STATUS_UNAUTHORIZED);
}
$this->logger->info('Streaming track with ID: ' . $id, ['user' => $user->getUID()]);
try {
$media = $this->mediaMapper->find((string)$id);
if ($media->getUserId() !== $user->getUID()) {
return new JSONResponse(['message' => 'Forbidden'], Http::STATUS_FORBIDDEN);
}
$file = $this->rootFolder->get($media->getPath());
if (!($file instanceof File)) {
$this->logger->error('Track file not found: ' . $media->getPath());
throw new NotFoundException();
}
return new FileDisplayResponse($file);
} catch (NotFoundException $e) {
$this->logger->error('Track file not found for ID: ' . $id, ['exception' => $e]);
return new JSONResponse(['message' => 'Track not found'], Http::STATUS_NOT_FOUND);
}
}
}

View File

@@ -8,6 +8,7 @@ use OCA\Jukebox\AppInfo\Application;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\OCSController;
use OCP\IAppConfig;
use OCP\IRequest;
@@ -77,6 +78,6 @@ class SettingsController extends OCSController {
$result['music_folder_path'] = $musicPath;
}
return new DataResponse($result);
return new JSONResponse($result);
}
}

View File

@@ -95,7 +95,7 @@ class JukeboxMedia extends Entity implements JsonSerializable {
'album' => $this->album,
'albumArtist' => $this->albumArtist,
'duration' => $this->duration,
'albumArt' => $this->albumArt,
'albumArt' => $this->getAlbumArtBase64(),
'genre' => $this->genre,
'year' => $this->year,
'bitrate' => $this->bitrate,