validateToken($token); // Count needs to be at least 1 if ($limit < 1) { throw new OCSBadRequestException('Limit needs to be greater or equal than 1'); } // Getting existing limit and init if unset $insert = false; try { $shareLimit = $this->mapper->get($token); } catch (DoesNotExistException $e) { $shareLimit = new Limit(); $shareLimit->setId($token); $insert = true; } // Set new limit $shareLimit->setLimit($limit); // Reset existing counter $shareLimit->setDownloads(0); // Update DB if ($insert) { $this->mapper->insert($shareLimit); } else { $this->mapper->update($shareLimit); } return new DataResponse(); } /** * Remove the download limit for a given link share */ #[NoAdminRequired] public function removeDownloadLimit(string $token): DataResponse { $this->validateToken($token); try { $shareLimit = $this->mapper->get($token); $this->mapper->delete($shareLimit); } catch (DoesNotExistException $e) { // Ignore if does not exists } return new DataResponse(); } /** * Get the download limit for a given link share */ #[NoAdminRequired] public function getDownloadLimit(string $token): DataResponse { $this->validateToken($token); try { $shareLimit = $this->mapper->get($token); } catch (DoesNotExistException $e) { return new DataResponse([ 'limit' => null, 'count' => null ]); } return new DataResponse([ 'limit' => $shareLimit->getLimit(), 'count' => $shareLimit->getDownloads() ]); } protected function validateToken(string $token = '') { $user = $this->userSession->getUser(); try { $share = $this->shareManager->getShareByToken($token); } catch (ShareNotFound $e) { throw new OCSNotFoundException('Unknown share'); } // Make sure the user is the owner of the share if ($user == null || ($share->getSharedBy() !== $user->getUID())) { throw new OCSNotFoundException('Unknown share'); } // Download count limit only works on links if ($share->getShareType() !== IShare::TYPE_LINK && $share->getShareType() !== IShare::TYPE_EMAIL) { throw new OCSNotFoundException('Invalid share type'); } // Download count limit only works on single file shares if ($share->getNodeType() !== 'file') { throw new OCSNotFoundException('Invalid file type'); } } }