TransformImports.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\ImportPost;
  5. use App\Services\ImportService;
  6. use App\Media;
  7. use App\Profile;
  8. use App\Status;
  9. use Storage;
  10. use App\Services\AccountService;
  11. use App\Services\MediaPathService;
  12. use Illuminate\Support\Str;
  13. use App\Util\Lexer\Autolink;
  14. class TransformImports extends Command
  15. {
  16. /**
  17. * The name and signature of the console command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'app:transform-imports';
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description = 'Transform imports into statuses';
  28. /**
  29. * Execute the console command.
  30. */
  31. public function handle()
  32. {
  33. if(!config('import.instagram.enabled')) {
  34. return;
  35. }
  36. $ips = ImportPost::whereNull('status_id')->where('skip_missing_media', '!=', true)->take(500)->get();
  37. if(!$ips->count()) {
  38. return;
  39. }
  40. foreach($ips as $ip) {
  41. $id = $ip->user_id;
  42. $pid = $ip->profile_id;
  43. $profile = Profile::find($pid);
  44. if(!$profile) {
  45. $ip->skip_missing_media = true;
  46. $ip->save();
  47. continue;
  48. }
  49. $exists = ImportPost::whereUserId($id)
  50. ->whereNotNull('status_id')
  51. ->where('filename', $ip->filename)
  52. ->where('creation_year', $ip->creation_year)
  53. ->where('creation_month', $ip->creation_month)
  54. ->where('creation_day', $ip->creation_day)
  55. ->exists();
  56. if($exists == true) {
  57. $ip->skip_missing_media = true;
  58. $ip->save();
  59. continue;
  60. }
  61. $idk = ImportService::getId($ip->user_id, $ip->creation_year, $ip->creation_month, $ip->creation_day);
  62. if(Storage::exists('imports/' . $id . '/' . $ip->filename) === false) {
  63. ImportService::clearAttempts($profile->id);
  64. ImportService::getPostCount($profile->id, true);
  65. $ip->skip_missing_media = true;
  66. $ip->save();
  67. continue;
  68. }
  69. $missingMedia = false;
  70. foreach($ip->media as $ipm) {
  71. $fileName = last(explode('/', $ipm['uri']));
  72. $og = 'imports/' . $id . '/' . $fileName;
  73. if(!Storage::exists($og)) {
  74. $missingMedia = true;
  75. }
  76. }
  77. if($missingMedia === true) {
  78. $ip->skip_missing_media = true;
  79. $ip->save();
  80. continue;
  81. }
  82. $caption = $ip->caption;
  83. $status = new Status;
  84. $status->profile_id = $pid;
  85. $status->caption = $caption;
  86. $status->rendered = strlen(trim($caption)) ? Autolink::create()->autolink($ip->caption) : null;
  87. $status->type = $ip->post_type;
  88. $status->scope = 'unlisted';
  89. $status->visibility = 'unlisted';
  90. $status->id = $idk['id'];
  91. $status->created_at = now()->parse($ip->creation_date);
  92. $status->save();
  93. foreach($ip->media as $ipm) {
  94. $fileName = last(explode('/', $ipm['uri']));
  95. $ext = last(explode('.', $fileName));
  96. $basePath = MediaPathService::get($profile);
  97. $og = 'imports/' . $id . '/' . $fileName;
  98. if(!Storage::exists($og)) {
  99. $ip->skip_missing_media = true;
  100. $ip->save();
  101. continue;
  102. }
  103. $size = Storage::size($og);
  104. $mime = Storage::mimeType($og);
  105. $newFile = Str::random(40) . '.' . $ext;
  106. $np = $basePath . '/' . $newFile;
  107. Storage::move($og, $np);
  108. $media = new Media;
  109. $media->profile_id = $pid;
  110. $media->user_id = $id;
  111. $media->status_id = $status->id;
  112. $media->media_path = $np;
  113. $media->mime = $mime;
  114. $media->size = $size;
  115. $media->save();
  116. }
  117. $ip->status_id = $status->id;
  118. $ip->creation_id = $idk['incr'];
  119. $ip->save();
  120. $profile->status_count = $profile->status_count + 1;
  121. $profile->save();
  122. AccountService::del($profile->id);
  123. ImportService::clearAttempts($profile->id);
  124. ImportService::getPostCount($profile->id, true);
  125. }
  126. }
  127. }