Last active
March 25, 2025 16:00
-
-
Save onyx-and-iris/31f0188cc57c1118dee9c7182b637794 to your computer and use it in GitHub Desktop.
Monkey Patches the owncloud.Client class to allow setting an expiration date on a file share link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This script monkey patches owncloud.Client.share_file_with_link to add the expire_date parameter. | |
This script is a proof of concept and should not be used in production. | |
""" | |
import datetime | |
import os | |
from xml.etree import ElementTree as ET | |
import dotenv | |
import owncloud | |
import six | |
from owncloud import ShareInfo | |
from owncloud.owncloud import HTTPResponseError | |
dotenv.load_dotenv() | |
def share_file_with_link(self, path, **kwargs): | |
"""Shares a remote file with link | |
:param path: path to the remote file to share | |
:param perms (optional): permission of the shared object | |
defaults to read only (1) | |
:param public_upload (optional): allows users to upload files or folders | |
:param password (optional): sets a password | |
:param expire_date (optional): sets an expiration date for the shared link | |
https://doc.owncloud.com/server/next/admin_manual/configuration/files/file_sharing_configuration.html | |
:param name (optional): display name for the link | |
:returns: instance of :class:`ShareInfo` with the share info | |
or False if the operation failed | |
:raises: HTTPResponseError in case an HTTP error status was returned | |
""" | |
perms = kwargs.get("perms", None) | |
public_upload = kwargs.get("public_upload", "false") | |
password = kwargs.get("password", None) | |
expire_date = kwargs.get("expire_date", None) | |
name = kwargs.get("name", None) | |
path = self._normalize_path(path) | |
post_data = { | |
"shareType": self.OCS_SHARE_TYPE_LINK, | |
"path": self._encode_string(path), | |
} | |
if (public_upload is not None) and (isinstance(public_upload, bool)): | |
post_data["publicUpload"] = str(public_upload).lower() | |
if isinstance(password, six.string_types): | |
post_data["password"] = password | |
if isinstance(expire_date, datetime.date): | |
post_data["expireDate"] = expire_date | |
if name is not None: | |
post_data["name"] = self._encode_string(name) | |
if perms: | |
post_data["permissions"] = perms | |
res = self._make_ocs_request( | |
"POST", self.OCS_SERVICE_SHARE, "shares", data=post_data | |
) | |
if res.status_code == 200: | |
tree = ET.fromstring(res.content) | |
self._check_ocs_status(tree) | |
data_el = tree.find("data") | |
return ShareInfo( | |
{ | |
"id": data_el.find("id").text, | |
"path": path, | |
"url": data_el.find("url").text, | |
"token": data_el.find("token").text, | |
"name": data_el.find("name").text, | |
"expiration": int( | |
round( | |
datetime.datetime.strptime( | |
data_el.find("expiration").text, "%Y-%m-%d %H:%M:%S" | |
).timestamp() | |
) | |
), | |
} | |
) | |
raise HTTPResponseError(res) | |
owncloud.Client.share_file_with_link = share_file_with_link | |
def main(): | |
oc = owncloud.Client(os.environ["OWNCLOUD_HOST"]) | |
oc.login(os.environ["OWNCLOUD_USER"], os.environ["OWNCLOUD_PASSWORD"]) | |
oc.put_file("test.txt", "test.txt") | |
tomorrow = datetime.datetime.now() + datetime.timedelta(1) | |
args = { | |
"perms": 1, | |
"expire_date": tomorrow.date(), | |
"password": None, | |
"public_upload": False, | |
} | |
link_info = oc.share_file_with_link("test.txt", **args) | |
print(f"Here is your link: {link_info.get_link()}") | |
print(f"Expires on: {link_info.get_expiration()}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment