Skip to content

Commit

Permalink
feat: reference data - find file id by link
Browse files Browse the repository at this point in the history
  • Loading branch information
LinneyS committed Jan 29, 2025
1 parent 2378351 commit 50e05b5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- setting for setup unknown author display name
- setting for sending notifications by email
- opening docm, xlsm, pptm files
- support IMPORTRANGE

## Changed
- URL for requests to Conversion API
Expand Down
44 changes: 43 additions & 1 deletion controller/editorcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,13 +614,14 @@ public function mention($fileId, $anchor, $comment, $emails) {
*
* @param array $referenceData - reference data
* @param string $path - file path
* @param string $link - file link
*
* @return array
*
* @NoAdminRequired
* @PublicPage
*/
public function reference($referenceData, $path = null) {
public function reference($referenceData, $path = null, $link = null) {
$this->logger->debug("reference: " . json_encode($referenceData) . " $path", ["app" => $this->appName]);

if (!$this->config->isUserAllowedToUse()) {
Expand Down Expand Up @@ -655,6 +656,15 @@ public function reference($referenceData, $path = null) {
}
}

if ($file === null
&& !empty($link)
) {
$fileId = $this->getFileIdByLink($link);
if (!empty($fileId)) {
list($file, $error, $share) = $this->getFile($userId, $fileId);
}
}

if ($file === null) {
$this->logger->error("Reference not found: $fileId $path", ["app" => $this->appName]);
return ["error" => $this->trans->t("File not found")];
Expand Down Expand Up @@ -1572,6 +1582,38 @@ private function getUserId($userId) {
return $userId;
}

/**
* Get File id from by link
*
* @param string $link - link to the file
*
* @return string|null
*/
private function getFileIdByLink(string $link) {
$path = parse_url($link, PHP_URL_PATH);
$encodedPath = array_map("urlencode", explode("/", $path));
$link = str_replace($path, implode("/", $encodedPath), $link);
if (filter_var($link, FILTER_VALIDATE_URL) === false) {
return null;
}

if (!empty($this->config->getStorageUrl())) {
$storageUrl = $this->config->getStorageUrl();
} else {
$storageUrl = $this->urlGenerator->getAbsoluteURL("/");
}

if (parse_url($link, PHP_URL_HOST) !== parse_url($storageUrl, PHP_URL_HOST)) {
return null;
}

if (preg_match('/\/onlyoffice\/(\d+)/', $link, $matches)) {
return $matches[1];
}

return null;
}

/**
* Print error page
*
Expand Down
2 changes: 2 additions & 0 deletions js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@
};

OCA.Onlyoffice.onRequestReferenceData = function (event) {
const link = event.data.link
const referenceData = event.data.referenceData;
const path = event.data.path;

Expand All @@ -693,6 +694,7 @@
{
referenceData,
path,
link,
},
function onSuccess(response) {
if (response.error) {
Expand Down

0 comments on commit 50e05b5

Please sign in to comment.