Abro éste post para ampliar un poco como se usa pyoocclient, una librería de python para gestionar un nextcloud. En éste post voy a mostrar como he solucionado el tema de crear directorios, subir ficheros, compartir ficheros, descargar un fichero (binario) y eliminar ficheros, pero la librería permite incluso hacer gestión de usuarios y algunas cosas mas, realmente da para seguir jugando :) En página podéis ver el código de la librería.
Primero de todo su instalación se hace mediante pip y requiere de las librerías request y django-storage-webdav
(venv) laura@melatonina:~/dev/colibri$ pip install request (venv) laura@melatonina:~/dev/colibri$ pip install django-storage-webdav (venv) laura@melatonina:~/dev/colibri$ pip install pyocclient
Luego he creado las funciones siguientes:
def webdav_login():
# get configuration from database
_dav_host = str(Config.objects.get(config_type='colibri', name='dav.host'))
_dav_username = str(Config.objects.get(config_type='colibri', name='dav.username'))
_dav_password = str(Config.objects.get(config_type='colibri', name='dav.password'))
oc = owncloud.Client(_dav_host)
oc.login(_dav_username, _dav_password)
return oc
def webdav_upload_file(file, path, share):
# save file to temporary directory and write file to disk
_working_directory = str(Config.objects.get(name='core.working_directory'))
_file = _working_directory + '/web/tmp/' + str(file.name)
with open(_file, 'wb+') as destination:
for chunk in file.chunks():
destination.write(chunk)
_dav_path = str(Config.objects.get(config_type='colibri', name='dav.path'))
_remote_path = _dav_path + '/' + str(path) + '/' + str(file.name)
# generate random password
from django.utils.crypto import get_random_string
_password = get_random_string(16, allowed_chars='abcdefghijklmnopqrstuvwxyz0123456789')
oc = webdav_login()
webdav_create_directory(path)
oc.put_file(_remote_path, _file)
oc.logout()
if share == 1:
link_info = oc.share_file_with_link(_dav_path + '/' + str(path) + '/' + str(file.name), password=_password)
_out = {'url': link_info.get_link(), 'password': _password, 'remote': _remote_path, 'name': file.name}
else:
_out = {'remote': _remote_path, 'name': file.name}
# delete temporary file
fs = FileSystemStorage()
fs.delete(_file)
return _out
def webdav_create_directory(directory):
oc = webdav_login()
_dav_path = str(Config.objects.get(config_type='colibri', name='dav.path'))
_path = _dav_path + '/' + str(directory)
# check directory exists
_dir = directory.split('/')
if len(_dir) >= 2:
# test root
try:
oc.list(_dav_path + '/' + _dir[0])
_out = "Folder already exists"
except Exception as e:
oc.mkdir(_dav_path + '/' + _dir[0])
_out = "Folder created"
# test first level
try:
oc.list(_dav_path + '/' + _dir[0] + '/' + _dir[1])
_out = "Folder already exists"
except Exception as e:
oc.mkdir(_dav_path + '/' + _dir[0] + '/' + _dir[1])
_out = "Folder created"
else:
try:
oc.list(_path)
_out = "Folder already exists"
except Exception as e:
oc.mkdir(_path)
_out = "Folder created"
oc.logout()
return _out
def webdav_get_file(path):
oc = webdav_login()
file = oc.get_file_contents(str(path))
file_data = oc.file_info(str(path))
oc.logout()
# get filename
_f = path.split('/')
_last = len(_f)
_last -= 1
_name = _f[_last]
from django.http import HttpResponse
response = HttpResponse(file, content_type=str(file_data.attributes['{DAV:}getcontenttype']))
response['Content-Disposition'] = 'attachment; filename=' + str(_name)
return response
def webdav_delete_file(path):
oc = webdav_login()
_out = oc.delete(str(path))
oc.logout()
return _out
Y listos, deseo que éste pedacito de código ayude a despejar vuestras dudas :)
Doy las gracias a rama por ayudarme a entender un poco mas como funciona todo ésto :D