InstanceService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Services;
  3. use Cache;
  4. use App\Instance;
  5. use App\Util\Blurhash\Blurhash;
  6. use App\Services\ConfigCacheService;
  7. class InstanceService
  8. {
  9. const CACHE_KEY_BY_DOMAIN = 'pf:services:instance:by_domain:';
  10. const CACHE_KEY_BANNED_DOMAINS = 'instances:banned:domains';
  11. const CACHE_KEY_UNLISTED_DOMAINS = 'instances:unlisted:domains';
  12. const CACHE_KEY_NSFW_DOMAINS = 'instances:auto_cw:domains';
  13. const CACHE_KEY_STATS = 'pf:services:instances:stats';
  14. const CACHE_KEY_BANNER_BLURHASH = 'pf:services:instance:header-blurhash:v1';
  15. public function __construct()
  16. {
  17. ini_set('memory_limit', config('pixelfed.memory_limit', '1024M'));
  18. }
  19. public static function getByDomain($domain)
  20. {
  21. return Cache::remember(self::CACHE_KEY_BY_DOMAIN.$domain, 3600, function() use($domain) {
  22. return Instance::whereDomain($domain)->first();
  23. });
  24. }
  25. public static function getBannedDomains()
  26. {
  27. return Cache::remember(self::CACHE_KEY_BANNED_DOMAINS, 1209600, function() {
  28. return Instance::whereBanned(true)->pluck('domain')->toArray();
  29. });
  30. }
  31. public static function getUnlistedDomains()
  32. {
  33. return Cache::remember(self::CACHE_KEY_UNLISTED_DOMAINS, 1209600, function() {
  34. return Instance::whereUnlisted(true)->pluck('domain')->toArray();
  35. });
  36. }
  37. public static function getNsfwDomains()
  38. {
  39. return Cache::remember(self::CACHE_KEY_NSFW_DOMAINS, 1209600, function() {
  40. return Instance::whereAutoCw(true)->pluck('domain')->toArray();
  41. });
  42. }
  43. public static function software($domain)
  44. {
  45. $key = 'instances:software:' . strtolower($domain);
  46. return Cache::remember($key, 86400, function() use($domain) {
  47. $instance = Instance::whereDomain($domain)->first();
  48. if(!$instance) {
  49. return;
  50. }
  51. return $instance->software;
  52. });
  53. }
  54. public static function stats()
  55. {
  56. return Cache::remember(self::CACHE_KEY_STATS, 86400, function() {
  57. return [
  58. 'total_count' => Instance::count(),
  59. 'new_count' => Instance::where('created_at', '>', now()->subDays(14))->count(),
  60. 'banned_count' => Instance::whereBanned(true)->count(),
  61. 'nsfw_count' => Instance::whereAutoCw(true)->count()
  62. ];
  63. });
  64. }
  65. public static function refresh()
  66. {
  67. Cache::forget(self::CACHE_KEY_BANNED_DOMAINS);
  68. Cache::forget(self::CACHE_KEY_UNLISTED_DOMAINS);
  69. Cache::forget(self::CACHE_KEY_NSFW_DOMAINS);
  70. Cache::forget(self::CACHE_KEY_STATS);
  71. self::getBannedDomains();
  72. self::getUnlistedDomains();
  73. self::getNsfwDomains();
  74. return true;
  75. }
  76. public static function headerBlurhash()
  77. {
  78. return Cache::rememberForever(self::CACHE_KEY_BANNER_BLURHASH, function() {
  79. if(str_ends_with(config_cache('app.banner_image'), 'headers/default.jpg')) {
  80. return 'UzJR]l{wHZRjM}R%XRkCH?X9xaWEjZj]kAjt';
  81. }
  82. $cached = config_cache('instance.banner.blurhash');
  83. if($cached) {
  84. return $cached;
  85. }
  86. $file = config_cache('app.banner_image') ?? url(Storage::url('public/headers/default.jpg'));
  87. $image = imagecreatefromstring(file_get_contents($file));
  88. if(!$image) {
  89. return 'UzJR]l{wHZRjM}R%XRkCH?X9xaWEjZj]kAjt';
  90. }
  91. $width = imagesx($image);
  92. $height = imagesy($image);
  93. $pixels = [];
  94. for ($y = 0; $y < $height; ++$y) {
  95. $row = [];
  96. for ($x = 0; $x < $width; ++$x) {
  97. $index = imagecolorat($image, $x, $y);
  98. $colors = imagecolorsforindex($image, $index);
  99. $row[] = [$colors['red'], $colors['green'], $colors['blue']];
  100. }
  101. $pixels[] = $row;
  102. }
  103. // Free the allocated GdImage object from memory:
  104. imagedestroy($image);
  105. $components_x = 4;
  106. $components_y = 4;
  107. $blurhash = Blurhash::encode($pixels, $components_x, $components_y);
  108. if(strlen($blurhash) > 191) {
  109. return 'UzJR]l{wHZRjM}R%XRkCH?X9xaWEjZj]kAjt';
  110. }
  111. ConfigCacheService::put('instance.banner.blurhash', $blurhash);
  112. return $blurhash;
  113. });
  114. }
  115. }