pixelfed/app/Console/Commands/TransformImports.php

158 lines
4.5 KiB
PHP
Raw Permalink Normal View History

2023-06-12 11:46:56 +00:00
<?php
namespace App\Console\Commands;
use App\Media;
use App\Models\ImportPost;
2023-06-12 11:46:56 +00:00
use App\Profile;
use App\Services\AccountService;
use App\Services\ImportService;
2023-06-12 11:46:56 +00:00
use App\Services\MediaPathService;
use App\Status;
use Illuminate\Console\Command;
2023-06-12 11:46:56 +00:00
use Illuminate\Support\Str;
use Storage;
2023-06-12 11:46:56 +00:00
class TransformImports extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:transform-imports';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Transform imports into statuses';
/**
* Execute the console command.
*/
public function handle()
{
if (! config('import.instagram.enabled')) {
2023-06-12 11:46:56 +00:00
return;
}
$ips = ImportPost::whereNull('status_id')->where('skip_missing_media', '!=', true)->take(500)->get();
2023-06-12 11:46:56 +00:00
if (! $ips->count()) {
2023-06-12 11:46:56 +00:00
return;
}
foreach ($ips as $ip) {
2023-06-12 11:46:56 +00:00
$id = $ip->user_id;
$pid = $ip->profile_id;
$profile = Profile::find($pid);
if (! $profile) {
$ip->skip_missing_media = true;
$ip->save();
continue;
}
$exists = ImportPost::whereUserId($id)
->whereNotNull('status_id')
->where('filename', $ip->filename)
->where('creation_year', $ip->creation_year)
->where('creation_month', $ip->creation_month)
->where('creation_day', $ip->creation_day)
->exists();
if ($exists == true) {
$ip->skip_missing_media = true;
$ip->save();
continue;
}
2023-06-12 11:46:56 +00:00
$idk = ImportService::getId($ip->user_id, $ip->creation_year, $ip->creation_month, $ip->creation_day);
if (! $idk) {
$ip->skip_missing_media = true;
$ip->save();
continue;
}
if (Storage::exists('imports/'.$id.'/'.$ip->filename) === false) {
2023-06-12 11:46:56 +00:00
ImportService::clearAttempts($profile->id);
ImportService::getPostCount($profile->id, true);
$ip->skip_missing_media = true;
$ip->save();
2023-06-12 11:46:56 +00:00
continue;
}
$missingMedia = false;
foreach ($ip->media as $ipm) {
2023-06-12 11:46:56 +00:00
$fileName = last(explode('/', $ipm['uri']));
$og = 'imports/'.$id.'/'.$fileName;
if (! Storage::exists($og)) {
2023-06-12 11:46:56 +00:00
$missingMedia = true;
}
}
if ($missingMedia === true) {
2023-06-12 11:46:56 +00:00
$ip->skip_missing_media = true;
$ip->save();
2023-06-12 11:46:56 +00:00
continue;
}
$caption = $ip->caption;
$status = new Status;
$status->profile_id = $pid;
$status->caption = $caption;
$status->type = $ip->post_type;
$status->scope = 'unlisted';
$status->visibility = 'unlisted';
$status->id = $idk['id'];
$status->created_at = now()->parse($ip->creation_date);
$status->save();
foreach ($ip->media as $ipm) {
2023-06-12 11:46:56 +00:00
$fileName = last(explode('/', $ipm['uri']));
$ext = last(explode('.', $fileName));
$basePath = MediaPathService::get($profile);
$og = 'imports/'.$id.'/'.$fileName;
if (! Storage::exists($og)) {
2023-06-12 11:46:56 +00:00
$ip->skip_missing_media = true;
$ip->save();
2023-06-12 11:46:56 +00:00
continue;
}
$size = Storage::size($og);
$mime = Storage::mimeType($og);
$newFile = Str::random(40).'.'.$ext;
$np = $basePath.'/'.$newFile;
2023-06-12 11:46:56 +00:00
Storage::move($og, $np);
$media = new Media;
$media->profile_id = $pid;
$media->user_id = $id;
$media->status_id = $status->id;
$media->media_path = $np;
$media->mime = $mime;
$media->size = $size;
$media->save();
}
$ip->status_id = $status->id;
$ip->creation_id = $idk['incr'];
$ip->save();
$profile->status_count = $profile->status_count + 1;
$profile->save();
AccountService::del($profile->id);
2023-06-12 11:46:56 +00:00
ImportService::clearAttempts($profile->id);
ImportService::getPostCount($profile->id, true);
}
}
}