Fix archive logic for mounts

pull/221/head
Varun Patil 2022-11-15 21:45:32 -08:00
parent 46ac8cf55e
commit c41ad9549a
1 changed files with 42 additions and 29 deletions

View File

@ -64,52 +64,65 @@ class ArchiveController extends ApiBase
if (null === $timelineFolder || !$timelineFolder instanceof Folder) { if (null === $timelineFolder || !$timelineFolder instanceof Folder) {
return new JSONResponse(['message' => 'Cannot get timeline'], Http::STATUS_INTERNAL_SERVER_ERROR); return new JSONResponse(['message' => 'Cannot get timeline'], Http::STATUS_INTERNAL_SERVER_ERROR);
} }
if (!$timelineFolder->isCreatable()) {
return new JSONResponse(['message' => 'Cannot create archive folder'], Http::STATUS_FORBIDDEN); // Bubble up from file until we reach the correct folder
$fileStorageId = $file->getStorage()->getId();
$parent = $file->getParent();
$isArchived = false;
while (true) {
if (null === $parent) {
throw new \Exception('Cannot get correct parent of file');
} }
// Get path of current file relative to the timeline folder if ($parent->getPath() === $timelineFolder->getPath()) {
// remove timelineFolder path from start of file path break;
$timelinePath = $timelineFolder->getPath(); // no trailing slash
if (substr($file->getPath(), 0, \strlen($timelinePath)) !== $timelinePath) {
return new JSONResponse(['message' => 'Files outside timeline cannot be archived'], Http::STATUS_INTERNAL_SERVER_ERROR);
} }
$relativePath = substr($file->getPath(), \strlen($timelinePath)); // has a leading slash
// Final path of the file including the file name if ($parent->getParent()->getStorage()->getId() !== $fileStorageId) {
$destinationPath = ''; break;
}
if ($parent->getName() === \OCA\Memories\Util::$ARCHIVE_FOLDER) {
$isArchived = true;
break;
}
$parent = $parent->getParent();
}
// Get path of current file relative to the parent folder
$relativeFilePath = $parent->getRelativePath($file->getPath());
// Check if we want to archive or unarchive // Check if we want to archive or unarchive
$body = $this->request->getParams(); $body = $this->request->getParams();
$unarchive = isset($body['archive']) && false === $body['archive']; $unarchive = isset($body['archive']) && false === $body['archive'];
if ($isArchived && !$unarchive) {
// Get if the file is already in the archive (relativePath starts with archive)
$archiveFolderWithLeadingSlash = '/'.\OCA\Memories\Util::$ARCHIVE_FOLDER;
if (substr($relativePath, 0, \strlen($archiveFolderWithLeadingSlash)) === $archiveFolderWithLeadingSlash) {
// file already in archive, remove it instead
$destinationPath = substr($relativePath, \strlen($archiveFolderWithLeadingSlash));
if (!$unarchive) {
return new JSONResponse(['message' => 'File already archived'], Http::STATUS_BAD_REQUEST); return new JSONResponse(['message' => 'File already archived'], Http::STATUS_BAD_REQUEST);
} } else if (!$isArchived && $unarchive) {
} else {
// file not in archive, put it in there
$destinationPath = Exif::removeExtraSlash(\OCA\Memories\Util::$ARCHIVE_FOLDER.$relativePath);
if ($unarchive) {
return new JSONResponse(['message' => 'File not archived'], Http::STATUS_BAD_REQUEST); return new JSONResponse(['message' => 'File not archived'], Http::STATUS_BAD_REQUEST);
} }
// 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
$af = \OCA\Memories\Util::$ARCHIVE_FOLDER;
$destinationPath = Exif::removeExtraSlash($af.$relativeFilePath);
} }
// Remove the filename // Remove the filename
$destinationFolders = explode('/', $destinationPath); $destinationFolders = array_filter(explode('/', $destinationPath));
array_pop($destinationFolders); array_pop($destinationFolders);
// Create folder tree // Create folder tree
$folder = $timelineFolder; $folder = $parent;
foreach ($destinationFolders as $folderName) { foreach ($destinationFolders as $folderName) {
if ('' === $folderName) {
continue;
}
try { try {
$existingFolder = $folder->get($folderName.'/'); $existingFolder = $folder->get($folderName.'/');
if (!$existingFolder instanceof Folder) { if (!$existingFolder instanceof Folder) {