add Immich send
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user