You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.2KB

  1. <?php
  2. class Action
  3. {
  4. /**
  5. * Home action.
  6. *
  7. * @return string
  8. */
  9. public function home():string
  10. {
  11. return response()
  12. ->httpCode(200)
  13. ->json([]);
  14. }
  15. /**
  16. * Parser action.
  17. *
  18. * @param string $parser
  19. *
  20. * @return string
  21. */
  22. public function parse(string $parser):string
  23. {
  24. $url = input()->get('url')->value;
  25. if (!isValidUrl($url, getenv('API_HOSTNAME'))) {
  26. return response()
  27. ->httpCode(400)
  28. ->json(['error' => 'Invalid URL']);
  29. }
  30. $parsers = [
  31. 'graby' => 'graby',
  32. 'opengraph' => 'opengraph',
  33. ];
  34. if (!isset($parsers[$parser])) {
  35. return response()
  36. ->httpCode(400)
  37. ->json(['error' => 'Invalid parser']);
  38. }
  39. try {
  40. $result = call_user_func_array($parsers[$parser], [$url]);
  41. return response()
  42. ->httpCode(200)
  43. ->json($result);
  44. } catch (\Exception $e) {
  45. return response()
  46. ->httpCode(500)
  47. ->json(['error' => $e->getMessage()]);
  48. }
  49. }
  50. }