Fix filename for shared folders

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

View File

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

View File

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