Skip to content

404 error when trying to delete "ghost links" #388

@Apfelkuchenbemme

Description

@Apfelkuchenbemme

Describe the bug

The idea

We're storing a bunch of videos and images on Nextcloud, that we want to use elsewhere via sharelinks. e.g. .mp4 files get used in video elements like so: <video src=".../s/.../download" loop=1 autoplay=1 width="100%" height="360"></video>

The problem

Nooow, I wasn't around when all of this got set up, but the "main folder we're working with", pretty much started as a personal folder of a colleague, who shared the folder with the rest of the team. That colleague left and transferred ownership of the folder to someone else, somehow deleting all sharelinks in the process. We now want to create sharelinks again, but after an update to Nextcloud 32.0.0, there's a default rate limit of 20 sharelinks per 10 minutes (I know the rate limit got introduced before 32.0.0, but we skipped a few versions). We don't want to change that, so we instead create sharelinks via a python script that uses nc_py_api to create up to 20 sharelinks at a time. To avoid creating sharelinks for files, that already have a sharelink, the script first checks if a link for a given file exists with:

  • FilePermissions.PERMISSION_SHARE|FilePermissions.PERMISSION_READ permissions
  • a datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc) expiration date

If that's the case, we just use that link. Otherwise, we create a new one.

Finally, the bug:

A colleague ran the script and replied that it didn't create any new links, instead using "links that don't work". That confused me, because from my and her browser view of the Nextcloud folder in question, there are no sharelinks to any of the files. When I checked via:

from nc_py_api import Nextcloud
username = ...
password = ...
url = ...
folder = ...
nc = Nextcloud(nextcloud_url=url, nc_auth_user=username, nc_auth_pass=password)
all_shares = nc.files.sharing.get_list(subfiles=True, path=folder)
print(len(all_shares))

I was surprised to see some 3400 sharelinks, all which didn't work (tested this via urllib.request.urlopen(url).read(), which threw a 404 for all links). According to the .share_owner of the shares, they all belong to the colleague in quesion, who, again, doesn't see them in the browser herself. New sharelinks she created, both manually in the browser and via a python script, are visible to both of us in the browser.

When I tried to delete these "ghost links" via nc.files.sharing.delete(share_id), I got this error, certain paths of course being replaced with "...":

404 error when trying to delete a link
>>> nc.files.sharing.delete(share.share_id)
Traceback (most recent call last):
  File "C:\...\env\lib\site-packages\nc_py_api\_exceptions.py", line 65, in check_error
    response.raise_for_status()
  File "C:\...\env\lib\site-packages\niquests\models.py", line 1590, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
niquests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://.../ocs/v1.php/apps/files_sharing/api/v1/shares/26324?format=json

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\...\env\lib\site-packages\nc_py_api\files\sharing.py", line 100, in delete
    self._session.ocs("DELETE", f"{self._ep_base}/shares/{share_id}")
  File "C:\...\env\lib\site-packages\nc_py_api\_session.py", line 222, in ocs
    check_error(response, info)
  File "C:\...\env\lib\site-packages\nc_py_api\_exceptions.py", line 67, in check_error
    raise NextcloudException(status_code, reason=response.reason, info=info) from e
nc_py_api._exceptions.NextcloudException: [404] Not Found <request: DELETE /ocs/v1.php/apps/files_sharing/api/v1/shares/26324>

Steps/Code to Reproduce

I guess something along the lines of:

  • create a folder on Nextcloud ~28.x
  • upload a bunch of files and create sharelinks
  • transfer ownership of the folder
  • update Nextcloud to 32.0.0

Expected Results

That these "ghost links" get deleted. Sharelinks we create manually in the browser or via nc_py_api get deleted "successfully".

Actual Results

The aforementioned error.

Setup configuration

nc_py_api = 0.23.0
Nextcloud = 32.0.0 now, probably 28.x before

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions