Update import job

This commit is contained in:
Daniel Supernault 2021-04-29 23:52:59 -06:00
parent f25452ea1a
commit 40f9aa6055
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -12,120 +12,120 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use App\Jobs\ImageOptimizePipeline\ImageOptimize; use App\Jobs\ImageOptimizePipeline\ImageOptimize;
use App\{ use App\{
ImportJob, ImportJob,
ImportData, ImportData,
Media, Media,
Profile, Profile,
Status, Status,
}; };
class ImportInstagram implements ShouldQueue class ImportInstagram implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $import; protected $import;
/** /**
* Delete the job if its models no longer exist. * Delete the job if its models no longer exist.
* *
* @var bool * @var bool
*/ */
public $deleteWhenMissingModels = true; public $deleteWhenMissingModels = true;
/** /**
* Create a new job instance. * Create a new job instance.
* *
* @return void * @return void
*/ */
public function __construct(ImportJob $import) public function __construct(ImportJob $import)
{ {
$this->import = $import; $this->import = $import;
} }
/** /**
* Execute the job. * Execute the job.
* *
* @return void * @return void
*/ */
public function handle() public function handle()
{ {
if(config('pixelfed.import.instagram.enabled') != true) { if(config('pixelfed.import.instagram.enabled') != true) {
return; return;
} }
$job = ImportJob::findOrFail($this->import->id); $job = ImportJob::findOrFail($this->import->id);
$profile = Profile::findOrFail($job->profile_id); $profile = Profile::findOrFail($job->profile_id);
$user = $profile->user; $user = $profile->user;
$json = $job->mediaJson(); $json = $job->mediaJson();
$collection = array_reverse($json['photos']); $collection = array_reverse($json['photos']);
$files = $job->files; $files = $job->files;
$monthHash = hash('sha1', date('Y').date('m')); $monthHash = hash('sha1', date('Y').date('m'));
$userHash = hash('sha1', $user->id . (string) $user->created_at); $userHash = hash('sha1', $user->id . (string) $user->created_at);
$fs = new Filesystem; $fs = new Filesystem;
foreach($collection as $import) foreach($collection as $import)
{ {
$caption = $import['caption']; $caption = $import['caption'];
try { try {
$min = Carbon::create(2010, 10, 6, 0, 0, 0); $min = Carbon::create(2010, 10, 6, 0, 0, 0);
$taken_at = Carbon::parse($import['taken_at']); $taken_at = Carbon::parse($import['taken_at']);
if(!$min->lt($taken_at)) { if(!$min->lt($taken_at)) {
$taken_at = Carbon::now(); $taken_at = Carbon::now();
} }
} catch (Exception $e) { } catch (Exception $e) {
} }
$filename = last( explode('/', $import['path']) ); $filename = last( explode('/', $import['path']) );
$importData = ImportData::whereJobId($job->id) $importData = ImportData::whereJobId($job->id)
->whereOriginalName($filename) ->whereOriginalName($filename)
->first(); ->first();
if(empty($importData) || is_file(storage_path("app/$importData->path")) == false) { if(empty($importData) || is_file(storage_path("app/$importData->path")) == false) {
continue; continue;
} }
DB::transaction(function() use( DB::transaction(function() use(
$fs, $job, $profile, $caption, $taken_at, $filename, $fs, $job, $profile, $caption, $taken_at, $filename,
$monthHash, $userHash, $importData $monthHash, $userHash, $importData
) { ) {
$status = new Status(); $status = new Status();
$status->profile_id = $profile->id; $status->profile_id = $profile->id;
$status->caption = strip_tags($caption); $status->caption = strip_tags($caption);
$status->is_nsfw = false; $status->is_nsfw = false;
$status->type = 'photo'; $status->type = 'photo';
$status->scope = 'unlisted'; $status->scope = 'unlisted';
$status->visibility = 'unlisted'; $status->visibility = 'unlisted';
$status->created_at = $taken_at; $status->created_at = $taken_at;
$status->save(); $status->save();
$path = storage_path("app/$importData->path"); $path = storage_path("app/$importData->path");
$storagePath = "public/m/{$monthHash}/{$userHash}"; $storagePath = "public/m/{$monthHash}/{$userHash}";
$dir = "app/$storagePath"; $dir = "app/$storagePath";
if(!is_dir(storage_path($dir))) { if(!is_dir(storage_path($dir))) {
mkdir(storage_path($dir), 0777, true); mkdir(storage_path($dir), 0755, true);
} }
$newPath = "$dir/$filename"; $newPath = "$dir/$filename";
$fs->move($path,storage_path($newPath)); $fs->move($path,storage_path($newPath));
$path = $newPath; $path = $newPath;
$hash = \hash_file('sha256', storage_path($path)); $hash = \hash_file('sha256', storage_path($path));
$media = new Media(); $media = new Media();
$media->status_id = $status->id; $media->status_id = $status->id;
$media->profile_id = $profile->id; $media->profile_id = $profile->id;
$media->user_id = $profile->user->id; $media->user_id = $profile->user->id;
$media->media_path = "$storagePath/$filename"; $media->media_path = "$storagePath/$filename";
$media->original_sha256 = $hash; $media->original_sha256 = $hash;
$media->size = $fs->size(storage_path($path)); $media->size = $fs->size(storage_path($path));
$media->mime = $fs->mimeType(storage_path($path)); $media->mime = $fs->mimeType(storage_path($path));
$media->filter_class = null; $media->filter_class = null;
$media->filter_name = null; $media->filter_name = null;
$media->order = 1; $media->order = 1;
$media->save(); $media->save();
ImageOptimize::dispatch($media); ImageOptimize::dispatch($media);
}); });
} }
$job->completed_at = Carbon::now(); $job->completed_at = Carbon::now();
$job->save(); $job->save();
} }
} }