TransformImports.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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(!$idk) {
  63. $ip->skip_missing_media = true;
  64. $ip->save();
  65. continue;
  66. }
  67. if(Storage::exists('imports/' . $id . '/' . $ip->filename) === false) {
  68. ImportService::clearAttempts($profile->id);
  69. ImportService::getPostCount($profile->id, true);
  70. $ip->skip_missing_media = true;
  71. $ip->save();
  72. continue;
  73. }
  74. $missingMedia = false;
  75. foreach($ip->media as $ipm) {
  76. $fileName = last(explode('/', $ipm['uri']));
  77. $og = 'imports/' . $id . '/' . $fileName;
  78. if(!Storage::exists($og)) {
  79. $missingMedia = true;
  80. }
  81. }
  82. if($missingMedia === true) {
  83. $ip->skip_missing_media = true;
  84. $ip->save();
  85. continue;
  86. }
  87. $caption = $ip->caption;
  88. $status = new Status;
  89. $status->profile_id = $pid;
  90. $status->caption = $caption;
  91. $status->rendered = strlen(trim($caption)) ? Autolink::create()->autolink($ip->caption) : null;
  92. $status->type = $ip->post_type;
  93. $status->scope = 'unlisted';
  94. $status->visibility = 'unlisted';
  95. $status->id = $idk['id'];
  96. $status->created_at = now()->parse($ip->creation_date);
  97. $status->save();
  98. foreach($ip->media as $ipm) {
  99. $fileName = last(explode('/', $ipm['uri']));
  100. $ext = last(explode('.', $fileName));
  101. $basePath = MediaPathService::get($profile);
  102. $og = 'imports/' . $id . '/' . $fileName;
  103. if(!Storage::exists($og)) {
  104. $ip->skip_missing_media = true;
  105. $ip->save();
  106. continue;
  107. }
  108. $size = Storage::size($og);
  109. $mime = Storage::mimeType($og);
  110. $newFile = Str::random(40) . '.' . $ext;
  111. $np = $basePath . '/' . $newFile;
  112. Storage::move($og, $np);
  113. $media = new Media;
  114. $media->profile_id = $pid;
  115. $media->user_id = $id;
  116. $media->status_id = $status->id;
  117. $media->media_path = $np;
  118. $media->mime = $mime;
  119. $media->size = $size;
  120. $media->save();
  121. }
  122. $ip->status_id = $status->id;
  123. $ip->creation_id = $idk['incr'];
  124. $ip->save();
  125. $profile->status_count = $profile->status_count + 1;
  126. $profile->save();
  127. AccountService::del($profile->id);
  128. ImportService::clearAttempts($profile->id);
  129. ImportService::getPostCount($profile->id, true);
  130. }
  131. }
  132. }