2022-10-29 18:05:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2022 Varun Patil <radialapps@gmail.com>
|
|
|
|
* @author Varun Patil <radialapps@gmail.com>
|
|
|
|
* @license AGPL-3.0-or-later
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Memories\Controller;
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
use OCA\Memories\Exceptions;
|
|
|
|
use OCA\Memories\Util;
|
2022-10-29 18:05:05 +00:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\Files\Folder;
|
2023-10-30 19:30:04 +00:00
|
|
|
use OCP\Lock\ILockingProvider;
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2023-03-22 18:40:56 +00:00
|
|
|
class ArchiveController extends GenericApiController
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* Move one file to the archive folder
|
|
|
|
*
|
2023-10-15 19:46:35 +00:00
|
|
|
* @param string $id File ID to archive / unarchive
|
2022-10-29 18:05:05 +00:00
|
|
|
*/
|
2023-03-19 03:38:37 +00:00
|
|
|
public function archive(string $id): Http\Response
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () use ($id) {
|
|
|
|
$userFolder = Util::getUserFolder();
|
|
|
|
|
|
|
|
// Check for permissions and get numeric Id
|
|
|
|
$file = $userFolder->getById((int) $id);
|
|
|
|
if (0 === \count($file)) {
|
|
|
|
throw Exceptions::NotFound("file id {$id}");
|
2022-11-16 05:45:32 +00:00
|
|
|
}
|
2023-03-23 20:32:23 +00:00
|
|
|
$file = $file[0];
|
2022-11-16 05:45:32 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Check if user has permissions
|
|
|
|
if (!$file->isUpdateable()) {
|
|
|
|
throw Exceptions::ForbiddenFileUpdate($file->getName());
|
2022-11-16 05:45:32 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Create archive folder in the root of the user's configured timeline
|
2023-04-16 23:03:59 +00:00
|
|
|
$configPaths = Util::getTimelinePaths(Util::getUID());
|
2023-03-23 20:32:23 +00:00
|
|
|
$timelinePaths = [];
|
|
|
|
|
|
|
|
// Get all timeline paths
|
|
|
|
foreach ($configPaths as $path) {
|
|
|
|
try {
|
2023-10-15 01:15:01 +00:00
|
|
|
$timelinePaths[] = $userFolder->get($path)->getPath();
|
2023-03-23 20:32:23 +00:00
|
|
|
} catch (\OCP\Files\NotFoundException $e) {
|
|
|
|
throw Exceptions::NotFound("timeline folder {$path}");
|
|
|
|
}
|
2023-03-03 20:29:17 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Bubble up from file until we reach the correct folder
|
|
|
|
$fileStorageId = $file->getStorage()->getId();
|
|
|
|
$parent = $file->getParent();
|
|
|
|
$isArchived = false;
|
2023-10-20 01:47:58 +00:00
|
|
|
$depth = 0;
|
2023-03-23 20:32:23 +00:00
|
|
|
while (true) {
|
2023-10-15 19:46:35 +00:00
|
|
|
/** @psalm-suppress DocblockTypeContradiction */
|
2023-03-23 20:32:23 +00:00
|
|
|
if (null === $parent) {
|
|
|
|
throw new \Exception('Cannot get correct parent of file');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hit a timeline folder
|
|
|
|
if (\in_array($parent->getPath(), $timelinePaths, true)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hit the user's root folder
|
|
|
|
if ($parent->getPath() === $userFolder->getPath()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hit a storage root
|
|
|
|
try {
|
|
|
|
if ($parent->getParent()->getStorage()->getId() !== $fileStorageId) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (\OCP\Files\NotFoundException $e) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hit an archive folder root
|
2023-10-20 01:47:58 +00:00
|
|
|
if (Util::ARCHIVE_FOLDER === $parent->getName()) {
|
2023-03-23 20:32:23 +00:00
|
|
|
$isArchived = true;
|
|
|
|
|
2022-11-16 10:30:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-03-23 20:32:23 +00:00
|
|
|
|
2023-10-20 01:47:58 +00:00
|
|
|
// Too deep
|
|
|
|
if (++$depth > 32) {
|
|
|
|
throw new \Exception('[Archive] Max recursion depth exceeded');
|
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
$parent = $parent->getParent();
|
2022-11-16 05:45:32 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Get path of current file relative to the parent folder
|
|
|
|
$relativeFilePath = $parent->getRelativePath($file->getPath());
|
2023-10-15 19:46:35 +00:00
|
|
|
if (!$relativeFilePath) {
|
|
|
|
throw new \Exception('Cannot get relative path of file');
|
|
|
|
}
|
2022-11-16 05:45:52 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Check if we want to archive or unarchive
|
|
|
|
$body = $this->request->getParams();
|
|
|
|
$unarchive = isset($body['archive']) && false === $body['archive'];
|
|
|
|
if ($isArchived && !$unarchive) {
|
|
|
|
throw Exceptions::BadRequest('File already archived');
|
|
|
|
}
|
|
|
|
if (!$isArchived && $unarchive) {
|
|
|
|
throw Exceptions::BadRequest('File not archived');
|
2022-11-16 05:45:32 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Final path of the file including the file name
|
|
|
|
$destinationPath = '';
|
|
|
|
|
|
|
|
// Get if the file is already in the archive (relativePath starts with archive)
|
|
|
|
if ($isArchived) {
|
|
|
|
// file already in archive, remove it
|
|
|
|
$destinationPath = $relativeFilePath;
|
|
|
|
$parent = $parent->getParent();
|
|
|
|
} else {
|
|
|
|
// file not in archive, put it in there
|
2023-10-20 01:47:58 +00:00
|
|
|
$destinationPath = Util::sanitizePath(Util::ARCHIVE_FOLDER.$relativeFilePath);
|
|
|
|
if (null === $destinationPath) {
|
|
|
|
throw Exceptions::BadRequest('Invalid archive destination path');
|
|
|
|
}
|
2023-03-23 20:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the filename
|
|
|
|
$destinationFolders = array_filter(explode('/', $destinationPath));
|
|
|
|
array_pop($destinationFolders);
|
|
|
|
|
|
|
|
// Create folder tree
|
|
|
|
$folder = $parent;
|
|
|
|
foreach ($destinationFolders as $folderName) {
|
2023-10-30 19:30:04 +00:00
|
|
|
$folder = $this->getOrCreateFolder($folder, $folderName);
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Move file to archive folder
|
2022-10-29 18:05:05 +00:00
|
|
|
$file->move($folder->getPath().'/'.$file->getName());
|
2023-03-23 20:32:23 +00:00
|
|
|
|
|
|
|
return new JSONResponse([], Http::STATUS_OK);
|
|
|
|
});
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
2023-10-30 19:30:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get or create a folder in the given parent folder.
|
|
|
|
*
|
|
|
|
* @param Folder $parent Parent folder
|
|
|
|
* @param string $name Folder name
|
|
|
|
* @param int $tries Number of tries to create the folder
|
|
|
|
*
|
|
|
|
* @throws \OCA\Memories\HttpResponseException
|
|
|
|
*/
|
|
|
|
private function getOrCreateFolder(Folder $parent, string $name, int $tries = 3): Folder
|
|
|
|
{
|
|
|
|
// Path of the folder we want to create (for error messages)
|
|
|
|
$finalPath = $parent->getPath().'/'.$name;
|
|
|
|
|
|
|
|
// Attempt to create the folder
|
|
|
|
if (!$parent->nodeExists($name)) {
|
|
|
|
$lockingProvider = \OC::$server->get(ILockingProvider::class);
|
|
|
|
$lockKey = "memories/create/{$finalPath}";
|
|
|
|
$lockType = ILockingProvider::LOCK_EXCLUSIVE;
|
|
|
|
$locked = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Attempt to acquire exclusive lock
|
|
|
|
$lockingProvider->acquireLock($lockKey, $lockType);
|
|
|
|
$locked = true;
|
|
|
|
} catch (\OCP\Lock\LockedException) {
|
|
|
|
// Someone else is creating, wait and try to get the folder
|
|
|
|
usleep(1000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($locked) {
|
|
|
|
// Green light to create the folder
|
|
|
|
try {
|
|
|
|
return $parent->newFolder($name);
|
|
|
|
} catch (\OCP\Files\NotPermittedException $e) {
|
|
|
|
// Cannot create folder, throw error
|
|
|
|
throw Exceptions::ForbiddenFileUpdate("{$finalPath} [create]");
|
|
|
|
} catch (\OCP\Lock\LockedException $e) {
|
|
|
|
// This is the Files lock ... well
|
|
|
|
throw Exceptions::ForbiddenFileUpdate("{$finalPath} [locked]");
|
|
|
|
} finally {
|
|
|
|
// Release our lock
|
|
|
|
$lockingProvider->releaseLock($lockKey, $lockType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second check if the folder exists
|
|
|
|
if (!$parent->nodeExists($name)) {
|
|
|
|
throw Exceptions::NotFound("Folder not found: {$finalPath}");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to get the folder that should already exist
|
|
|
|
$existing = $parent->get($name);
|
|
|
|
if (!$existing instanceof Folder) {
|
|
|
|
throw Exceptions::NotFound("Not a folder: {$existing->getPath()}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $existing;
|
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|