1<?php 2/** 3 * @author Thomas Müller <thomas.mueller@tmit.eu> 4 * 5 * @copyright Copyright (c) 2019, ownCloud GmbH 6 * @license AGPL-3.0 7 * 8 * This code is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU Affero General Public License, version 3, 10 * as published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU Affero General Public License for more details. 16 * 17 * You should have received a copy of the GNU Affero General Public License, version 3, 18 * along with this program. If not, see <http://www.gnu.org/licenses/> 19 * 20 */ 21 22namespace OCA\DAV\TrashBin; 23 24use OCA\DAV\Connector\Sabre\Exception\FileLocked; 25use OCP\Files\FileInfo; 26use OCP\Files\ForbiddenException; 27use OCP\Files\StorageNotAvailableException; 28use OCP\Lock\LockedException; 29use Sabre\DAV\Exception\Forbidden; 30use Sabre\DAV\Exception\ServiceUnavailable as SabreServiceUnavailable; 31 32abstract class AbstractTrashBinNode implements ITrashBinNode { 33 34 /** 35 * @var FileInfo 36 */ 37 protected $fileInfo; 38 /** 39 * @var TrashBinManager 40 */ 41 protected $trashBinManager; 42 /** 43 * @var string 44 */ 45 protected $user; 46 47 public function __construct( 48 string $user, 49 FileInfo $fileInfo, 50 TrashBinManager $trashBinManager 51 ) { 52 $this->fileInfo = $fileInfo; 53 $this->trashBinManager = $trashBinManager; 54 $this->user = $user; 55 } 56 57 /** 58 * Returns the name of the node. 59 * 60 * This is used to generate the url. 61 * 62 * @return string 63 */ 64 public function getName() { 65 return (string)$this->fileInfo->getId(); 66 } 67 68 /** 69 * Returns the mime-type for a file 70 * 71 * If null is returned, we'll assume application/octet-stream 72 * 73 * @return string|null 74 */ 75 public function getContentType() { 76 return $this->fileInfo->getMimetype(); 77 } 78 79 public function getETag() { 80 return '"' . $this->fileInfo->getEtag() . '"'; 81 } 82 83 public function getLastModified() { 84 return $this->fileInfo->getMtime(); 85 } 86 public function getSize() { 87 return $this->fileInfo->getSize(); 88 } 89 90 public function getOriginalFileName() : string { 91 $path = $this->getPathInTrash(); 92 if (\count($path) === 1) { 93 $path = \end($path); 94 $pathInfo = \pathinfo($path); 95 return $pathInfo['filename']; 96 } 97 return \end($path); 98 } 99 100 public function getOriginalLocation() : string { 101 $pathElements = $this->getPathInTrash(); 102 $path = $pathElements[0]; 103 $pathParts = \pathinfo($path); 104 $timestamp = (int)\substr($pathParts['extension'], 1); 105 106 $pathElements[0] = $pathParts['filename']; 107 $originalPath = \implode('/', $pathElements); 108 109 $location = $this->trashBinManager->getLocation($this->user, $pathParts['filename'], $timestamp); 110 if ($location !== '.') { 111 $originalPath = $location . '/' . $originalPath; 112 } 113 114 return $originalPath; 115 } 116 117 public function getDeleteTimestamp() : int { 118 $path = $this->getPathInTrash(); 119 $path = $path[0]; 120 $pathParts = \pathinfo($path); 121 return (int)\substr($pathParts['extension'], 1); 122 } 123 124 /** 125 * @param string $targetLocation 126 * @return bool 127 */ 128 public function restore(string $targetLocation): bool { 129 try { 130 return $this->trashBinManager->restore($this->user, $this, $targetLocation); 131 } catch (ForbiddenException $ex) { 132 throw new \OCA\DAV\Connector\Sabre\Exception\Forbidden($ex->getMessage(), $ex->getRetry()); 133 } catch (LockedException $e) { 134 throw new FileLocked($e->getMessage(), $e->getCode(), $e); 135 } catch (StorageNotAvailableException $e) { 136 throw new SabreServiceUnavailable($e->getMessage()); 137 } 138 } 139 140 public function delete() { 141 try { 142 $this->trashBinManager->delete($this->user, $this); 143 } catch (ForbiddenException $ex) { 144 throw new \OCA\DAV\Connector\Sabre\Exception\Forbidden($ex->getMessage(), $ex->getRetry()); 145 } catch (LockedException $e) { 146 throw new FileLocked($e->getMessage(), $e->getCode(), $e); 147 } catch (StorageNotAvailableException $e) { 148 throw new SabreServiceUnavailable($e->getMessage()); 149 } 150 } 151 152 /** 153 * @return array 154 */ 155 public function getPathInTrash() { 156 $path = $this->fileInfo->getPath(); 157 $path = \explode('/', $path); 158 return \array_splice($path, 4); 159 } 160 161 public function setName($name) { 162 throw new Forbidden('Permission denied to rename this resource'); 163 } 164} 165