add Immich send
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
use App\Jobs\SendToImmichJob;
|
||||
|
||||
#[Signature("immich:watch-folder")]
|
||||
#[Description("Dispatch photos folder")]
|
||||
class WatchFolder extends Command
|
||||
{
|
||||
private bool $continue = true;
|
||||
|
||||
private $instance;
|
||||
|
||||
private $resourceHandle;
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$watchDir = config("immich.folder_path");
|
||||
|
||||
// Gracefully exit when TERM signal received
|
||||
pcntl_async_signals(true);
|
||||
pcntl_signal(SIGTERM, function () {
|
||||
$this->continue = false;
|
||||
});
|
||||
|
||||
$this->instance = inotify_init();
|
||||
stream_set_blocking($this->instance, 0);
|
||||
$this->resourceHandle = inotify_add_watch(
|
||||
$this->instance,
|
||||
$watchDir,
|
||||
IN_CLOSE_WRITE | IN_ATTRIB | IN_MOVED_TO | IN_MOVE_SELF,
|
||||
);
|
||||
|
||||
while ($this->continue) {
|
||||
$events = inotify_read($this->instance);
|
||||
|
||||
if (!empty($events)) {
|
||||
// Get filenames only and remove duplicates
|
||||
$fileNames = array_unique(
|
||||
array_map(function ($event) {
|
||||
return $event["name"];
|
||||
}, $events),
|
||||
);
|
||||
|
||||
foreach ($fileNames as $fileName) {
|
||||
echo $fileName . PHP_EOL;
|
||||
SendToImmichJob::dispatch($fileName);
|
||||
}
|
||||
}
|
||||
sleep(1);
|
||||
}
|
||||
fclose($this->instance);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Queue\Queueable;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class SendToImmichJob implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct(private string $path)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
try {
|
||||
$path = config("immich.folder_path") . "/" . $this->path;
|
||||
$imageContent = file_get_contents($path);
|
||||
$imageName = basename($this->path);
|
||||
$headers = [
|
||||
"Accept" => "application/json",
|
||||
"x-api-key" => config("immich.key"),
|
||||
];
|
||||
$deviceId = "Laravel";
|
||||
$albumId = "6ae6dda5-1681-4cf5-9ebe-d0038c9308a8";
|
||||
$id = "503832b6-afa0-4dca-8fca-a7df89c8f658";
|
||||
|
||||
$url = config("immich.url");
|
||||
|
||||
$res = Http::withHeaders($headers)
|
||||
->attach("assetData", $imageContent, $imageName)
|
||||
->post("$url/api/assets", [
|
||||
"deviceAssetId" => $id,
|
||||
"deviceId" => $deviceId,
|
||||
"fileCreatedAt" => now()->format("Y-m-d H:i:s"),
|
||||
"fileModifiedAt" => now()->format("Y-m-d H:i:s"),
|
||||
]);
|
||||
|
||||
$response = $res->json();
|
||||
|
||||
if (isset($response["status"])) {
|
||||
$content = $res->json();
|
||||
if ($response["status"] === "duplicate") {
|
||||
throw new Exception(
|
||||
"duplicate image : " . $response["id"],
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
Http::withHeaders($headers)->put(
|
||||
"$url/api/albums/$albumId/assets",
|
||||
[
|
||||
"ids" => [$content["id"]],
|
||||
],
|
||||
);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
Log::error($e->getMessage(), ["path" => $this->path]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
#[Fillable(['name', 'email', 'password'])]
|
||||
#[Hidden(['password', 'remember_token'])]
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user