ImportEmojis.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\CustomEmoji;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\Storage;
  7. class ImportEmojis extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'import:emojis
  15. {path : Path to a tar.gz archive with the emojis}
  16. {--prefix : Define a prefix for the emjoi shortcode}
  17. {--suffix : Define a suffix for the emjoi shortcode}
  18. {--overwrite : Overwrite existing emojis}
  19. {--disabled : Import all emojis as disabled}';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = 'Import emojis to the database';
  26. /**
  27. * Execute the console command.
  28. *
  29. * @return int
  30. */
  31. public function handle()
  32. {
  33. $path = $this->argument('path');
  34. if (!file_exists($path) || !mime_content_type($path) == 'application/x-tar') {
  35. $this->error('Path does not exist or is not a tarfile');
  36. return Command::FAILURE;
  37. }
  38. $imported = 0;
  39. $skipped = 0;
  40. $failed = 0;
  41. $tar = new \PharData($path);
  42. $tar->decompress();
  43. foreach (new \RecursiveIteratorIterator($tar) as $entry) {
  44. $this->line("Processing {$entry->getFilename()}");
  45. if (!$entry->isFile() || !$this->isImage($entry) || !$this->isEmoji($entry->getPathname())) {
  46. $failed++;
  47. continue;
  48. }
  49. $filename = pathinfo($entry->getFilename(), PATHINFO_FILENAME);
  50. $extension = pathinfo($entry->getFilename(), PATHINFO_EXTENSION);
  51. // Skip macOS shadow files
  52. if (str_starts_with($filename, '._')) {
  53. continue;
  54. }
  55. $shortcode = implode('', [
  56. $this->option('prefix'),
  57. $filename,
  58. $this->option('suffix'),
  59. ]);
  60. $customEmoji = CustomEmoji::whereShortcode($shortcode)->first();
  61. if ($customEmoji && !$this->option('overwrite')) {
  62. $skipped++;
  63. continue;
  64. }
  65. $emoji = $customEmoji ?? new CustomEmoji();
  66. $emoji->shortcode = $shortcode;
  67. $emoji->domain = config('pixelfed.domain.app');
  68. $emoji->disabled = $this->option('disabled');
  69. $emoji->save();
  70. $fileName = $emoji->id . '.' . $extension;
  71. Storage::putFileAs('public/emoji', $entry->getPathname(), $fileName);
  72. $emoji->media_path = 'emoji/' . $fileName;
  73. $emoji->save();
  74. $imported++;
  75. Cache::forget('pf:custom_emoji');
  76. }
  77. $this->line("Imported: {$imported}");
  78. $this->line("Skipped: {$skipped}");
  79. $this->line("Failed: {$failed}");
  80. //delete file
  81. unlink(str_replace('.tar.gz', '.tar', $path));
  82. return Command::SUCCESS;
  83. }
  84. private function isImage($file)
  85. {
  86. $image = getimagesize($file->getPathname());
  87. return $image !== false;
  88. }
  89. private function isEmoji($filename)
  90. {
  91. $allowedMimeTypes = ['image/png', 'image/jpeg', 'image/webp'];
  92. $mimeType = mime_content_type($filename);
  93. return in_array($mimeType, $allowedMimeTypes);
  94. }
  95. }