Skip to content

Commit

Permalink
Initial media upload mock
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Porritt committed Jan 15, 2023
1 parent 8017061 commit 444d234
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions application/src/Controller/MediaController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Service\ApiCheck;


/**
* API Controller to serve a mock of the Matrix API for media requests.
*
* @Route("/{serverID}/_matrix/media/r0")
*/
class MediaController extends DataController {

/**
* @Route("", name="endpoint")
*/
public function endpoint(): JsonResponse
{
return new JsonResponse((object) [
'errcode' => 'M_UNRECOGNIZED',
'error' => 'Unrecognized request'
],
404);
}

/**
* Create Matrix room.
*
* @Route("/upload", name="uploadMedia")
* @param string $serverID
* @param Request $request
* @return JsonResponse
*/
public function uploadMedia(string $serverID, Request $request): JsonResponse {
// Check call auth.
$authCheck = ApiCheck::checkAuth($request);
if (!$authCheck['status']) {
// Auth check failed, return error info.
return $authCheck['message'];
}

// Check HTTP method is accepted.
$method = $request->getMethod();
$methodCheck = ApiCheck::checkMethod(['POST'], $method);
if (!$methodCheck['status']) {
// Method check failed, return error info.
return $methodCheck['message'];
}

$filename = $request->query->get('filename');
$host = $request->getHost();

// We don't care about the payload and storing it.
// Just assume everything is fine and return a fake URI for it.
$contentURI = 'mxc://' . $host . '/' . substr(hash('sha256', ($serverID . $filename . $host)), 0, 24);

return new JsonResponse((object) [
'content_uri' => $contentURI,
],
200);
}
}

0 comments on commit 444d234

Please sign in to comment.