destination ? Volumes::getVolumeByHandle($settings->destination) : null; if (! $volume) { return $this->result(false, 'Please set a file destination in settings so images can be saved'); } $photo = $unsplash->getPhoto($request->input('id')); $tempPath = tempnam(sys_get_temp_dir(), 'unsplash').'.jpg'; Http::sink($tempPath)->get($photo->download())->throw(); $subpath = $settings->folder ? Template::renderObjectTemplate($settings->folder, $settings) : ''; $asset = new Asset; $asset->tempFilePath = $tempPath; $asset->filename = 'photo-'.$photo->id.'.jpg'; /** @phpstan-ignore-next-line */ if ($photo->description) { $asset->alt = $photo->description; } $asset->newFolderId = Folders::ensureFolderByFullPathAndVolume($subpath, $volume)->id; $asset->volumeId = $volume->id; /** @phpstan-ignore-next-line */ $asset->title = 'Photo by '.$photo->photographer()->name; $asset->avoidFilenameConflicts = true; $asset->ruleset->useScenario(AssetRules::SCENARIO_CREATE); return Elements::saveElement($asset) ? $this->result(true, 'Image saved!') : $this->result(false, 'Oops, something went wrong...'); } private function result(bool $success, string $message): JsonResponse { return new JsonResponse(['success' => $success, 'message' => t($message, category: 'splashing-images')]); } }