Python: Hide Download and Set Expiration of Share Links

Using the ownCloud Python package I have written a small script (see below) to upload files via a bash alias. While the desktop clients are great for quickly sharing files I often find using this script more efficient and it also works in headless environments.

With Nextcloud 15+ the ability to “Hide Download Link” was added via the files interface. I’m wondering if this feature is accessible from the Python package. In the same vein, I’m wondering if there’s a function to set expiration.

I’m fairly new to writing and reading Python and also know that the class was originally for ownCloud and hasn’t been updated in some time. If there’s another Python package for Nextcloud or WebDAV that I should be using please point me in that direction.

Ideally, the goal is to be able to run it like this:
python 3 nextcloud.py Some/File/image.jpg true 86400 which would upload the file, hide the download link, then set the expiration to one day.

Looking forward to the communities thoughts.

import sys
from pathlib import Path

domain = 'https://example.com/'
user = 'python'
password = '12345abcxyz'
remoteDirectory = 'public-uploads'

fullFilePath = str( ' '.join( sys.argv[1:] ) )

try:
 pathParts = fullFilePath.rsplit( '/', 1 )
 localName = pathParts[1]
except IndexError:
 localName = fullFilePath

fileName = remoteDirectory + '/' + localName.replace(" ", "_")

if not Path( fullFilePath ).exists() :
 print ( "File not found, check the following: \n1) Did you include the absolute path to file? \n2) Did you use quotes around the filename?" )
 sys.exit (1)

oc = owncloud.Client( domain )
oc.login( user, password )

oc.put_file( fileName, fullFilePath )
linkData = oc.share_file_with_link( fileName )
print ('Public link: %s' % ( linkData.get_link() ) )
1 Like

With the help() command you can list the parameters and return values the function takes:

help(oc.share_file_with_link)

There are pull requests for the expiry date but they weren’t merged:https://github.com/owncloud/pyocclient/pull/143

Currently, it seems there is not much development on this part to implement new features. If you put in new features that are common to NC and OC, you can create a pull request in their repository. If we want to add features unique to Nextcloud, we’ll probably have to create a separate repository.

Thanks for the tips @tflidd! I’ll keep digging. I did see that one of the issues of the ownCloud library is to split into modules ( # ). If that happens then perhaps exploring add Nextcloud specific modules as submodules would make sense.

Unfortunately, it doesn’t look like there’s a lot of interest at the moment of maintaining a Python pack from either Nextcloud or ownCloud. Which is fine, and what I have is working. I’ll keep an eye on things and if I get ambitious experiment with expanding my script.

Thanks again!

There are still a few missing features that are supported in both solutions (tagging, sharing options, …). So if we manage to implement them first, then we can see if we create specific submodules (or they are loaded if these functions are detected via the capabilities() query).
Unfortunately, I already found one small issue during authentication: https://github.com/nextcloud/server/issues/13561

Such a library can be useful. There are regularly a few problems here that could be easily solved with a small script.