forked from moodlehq/matrixsynapse_mock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matt Porritt
committed
Jan 15, 2023
1 parent
8017061
commit 444d234
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |