HEX
Server: LiteSpeed
System: Linux d8 4.18.0-553.121.1.lve.el8.x86_64 #1 SMP Thu Apr 30 16:40:41 UTC 2026 x86_64
User: wbwebdes (3015)
PHP: 8.1.31
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: /home/wbwebdes/domains/files.wb-cloud.nl/private_html/apps/photos/lib/Sabre/CollectionPhoto.php
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OCA\Photos\Sabre;

use OCA\Photos\DB\PhotosFile;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\ITagManager;
use OCP\ITags;
use OCP\Server;
use Sabre\DAV\Exception\Forbidden;

class CollectionPhoto {
	public function __construct(
		protected PhotosFile $file,
		protected Folder $userFolder,
	) {
	}

	public function getName() {
		return $this->file->getFileId() . '-' . $this->file->getName();
	}

	public function setName($name): never {
		throw new Forbidden('Can\'t rename photos through this api');
	}

	public function getLastModified() {
		return $this->file->getMTime();
	}

	public function put($data) {
		$nodes = $this->userFolder->getById($this->file->getFileId());
		$node = current($nodes);
		if ($node) {
			/** @var Node $node */
			if ($node instanceof File) {
				return $node->putContent($data);
			} else {
				throw new NotFoundException('Photo is a folder');
			}
		} else {
			throw new NotFoundException('Photo not found for user');
		}
	}

	public function getFileId(): int {
		return $this->file->getFileId();
	}

	public function getContentType() {
		return $this->file->getMimeType();
	}

	public function getETag() {
		return $this->file->getEtag();
	}

	public function getSize() {
		return $this->file->getSize();
	}

	public function getFile(): PhotosFile {
		return $this->file;
	}

	public function isFavorite(): bool {
		$tagManager = Server::get(ITagManager::class);
		$tagger = $tagManager->load('files');
		if ($tagger === null) {
			return false;
		}
		$tags = $tagger->getTagsForObjects([$this->getFileId()]);

		if ($tags === false || empty($tags)) {
			return false;
		}

		return array_search(ITags::TAG_FAVORITE, current($tags)) !== false;
	}

	public function setFavoriteState($favoriteState): bool {
		$tagManager = Server::get(ITagManager::class);
		$tagger = $tagManager->load('files');

		switch ($favoriteState) {
			case '0':
				return $tagger->removeFromFavorites($this->file->getFileId());
			case '1':
				return $tagger->addToFavorites($this->file->getFileId());
			default:
				new \Exception('Favorite state is invalide, should be 0 or 1.');
		}
	}
}