Fix filename for shared folders

old-stable24^2
Varun Patil 2022-10-28 18:29:28 -07:00
parent cd2f714e92
commit 2c40f8d57e
2 changed files with 44 additions and 37 deletions

View File

@ -87,13 +87,8 @@ class ApiController extends Controller
*/ */
public function days(): JSONResponse public function days(): JSONResponse
{ {
$user = $this->userSession->getUser();
if (null === $user && !$this->getShareToken()) {
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
}
$uid = $user ? $user->getUID() : '';
// Get the folder to show // Get the folder to show
$uid = $this->getUid();
$folder = null; $folder = null;
try { try {
@ -122,7 +117,7 @@ class ApiController extends Controller
); );
// Preload some day responses // Preload some day responses
$this->preloadDays($list, $folder, $recursive, $archive); $this->preloadDays($list, $uid, $folder, $recursive, $archive);
// Add subfolder info if querying non-recursively // Add subfolder info if querying non-recursively
if (!$recursive) { if (!$recursive) {
@ -157,11 +152,8 @@ class ApiController extends Controller
*/ */
public function day(string $id): JSONResponse public function day(string $id): JSONResponse
{ {
$user = $this->userSession->getUser(); // Get user
if (null === $user && !$this->getShareToken()) { $uid = $this->getUid();
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
}
$uid = $user ? $user->getUID() : '';
// Check for wildcard // Check for wildcard
$day_ids = []; $day_ids = [];
@ -698,6 +690,19 @@ class ApiController extends Controller
return $response; return $response;
} }
/** Get logged in user's UID or throw HTTP error */
private function getUid(): string
{
$user = $this->userSession->getUser();
if ($this->getShareToken()) {
$user = null;
} elseif (null === $user) {
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
}
return $user ? $user->getUID() : '';
}
/** /**
* Get transformations depending on the request. * Get transformations depending on the request.
* *
@ -768,15 +773,13 @@ class ApiController extends Controller
* Preload a few "day" at the start of "days" response. * Preload a few "day" at the start of "days" response.
* *
* @param array $days the days array * @param array $days the days array
* @param string $uid User ID or blank for public shares
* @param null|Folder $folder the folder to search in * @param null|Folder $folder the folder to search in
* @param bool $recursive search in subfolders * @param bool $recursive search in subfolders
* @param bool $archive search in archive folder only * @param bool $archive search in archive folder only
*/ */
private function preloadDays(array &$days, &$folder, bool $recursive, bool $archive) private function preloadDays(array &$days, string $uid, &$folder, bool $recursive, bool $archive)
{ {
$user = $this->userSession->getUser();
$uid = $user ? $user->getUID() : '';
$transforms = $this->getTransformations(false); $transforms = $this->getTransformations(false);
$preloaded = 0; $preloaded = 0;
$preloadDayIds = []; $preloadDayIds = [];
@ -822,21 +825,21 @@ class ApiController extends Controller
/** Get the Folder object relevant to the request */ /** Get the Folder object relevant to the request */
private function getRequestFolder() private function getRequestFolder()
{ {
$user = $this->userSession->getUser(); // Public shared folder
if (null === $user) { if ($token = $this->getShareToken()) {
// Public shares only $share = $this->shareManager->getShareByToken($token)->getNode(); // throws exception if not found
if ($token = $this->getShareToken()) { if (!$share instanceof Folder) {
$share = $this->shareManager->getShareByToken($token)->getNode(); // throws exception if not found throw new \Exception('Share not found or invalid');
if (!$share instanceof Folder) {
throw new \Exception('Share not found or invalid');
}
return $share;
} }
return null; return $share;
} }
// Anything else needs a user
$user = $this->userSession->getUser();
if (null === $user) {
return null;
}
$uid = $user->getUID(); $uid = $user->getUID();
$folder = null; $folder = null;

View File

@ -136,7 +136,7 @@ trait TimelineQueryDays
$rows = $cursor->fetchAll(); $rows = $cursor->fetchAll();
$cursor->closeCursor(); $cursor->closeCursor();
return $this->processDay($rows, $folder); return $this->processDay($rows, $uid, $folder);
} }
/** /**
@ -158,12 +158,13 @@ trait TimelineQueryDays
* Process the single day response. * Process the single day response.
* *
* @param array $day * @param array $day
* @param string $uid User or blank if not logged in
* @param null|Folder $folder * @param null|Folder $folder
*/ */
private function processDay(&$day, $folder) private function processDay(&$day, $uid, $folder)
{ {
$basePath = '#__#__#'; $basePath = '#__#__#';
$davPath = '/'; $davPath = '';
if (null !== $folder) { if (null !== $folder) {
// No way to get the internal path from the folder // No way to get the internal path from the folder
$query = $this->connection->getQueryBuilder(); $query = $this->connection->getQueryBuilder();
@ -177,13 +178,16 @@ trait TimelineQueryDays
// Get user facing path // Get user facing path
// getPath looks like /user/files/... but we want /files/user/... // getPath looks like /user/files/... but we want /files/user/...
// Split at / and swap these // Split at / and swap these
$actualPath = $folder->getPath(); // For public shares, we just give the relative path
$actualPath = explode('/', $actualPath); if (!empty($uid)) {
if (\count($actualPath) >= 3) { $actualPath = $folder->getPath();
$tmp = $actualPath[1]; $actualPath = explode('/', $actualPath);
$actualPath[1] = $actualPath[2]; if (\count($actualPath) >= 3) {
$actualPath[2] = $tmp; $tmp = $actualPath[1];
$davPath = implode('/', $actualPath); $actualPath[1] = $actualPath[2];
$actualPath[2] = $tmp;
$davPath = implode('/', $actualPath);
}
} }
} }