You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
depot_django/common/utils/owncloud_utls.py

41 lines
1.6 KiB
Python

import os
import uuid
from datetime import datetime
from django.conf import settings
from rest_framework import status
from rest_framework.response import Response
import owncloud
from owncloud import (HTTPResponseError)
class Owncloud:
@staticmethod
def upload_damage_photo(filename, depot_id):
_, ext = os.path.splitext(filename)
oc = owncloud.Client(settings.OWNCLOUD_URL)
oc.login(settings.OWNCLOUD_USER, settings.OWNCLOUD_PASSWORD)
path = f"{settings.OWNCLOUD_DAMAGES_FOLDER}{str(depot_id)}"
try:
oc.file_info(path)
except HTTPResponseError:
_ = oc.mkdir(path)
owncloud_filename = f"{datetime.today().strftime('%Y%m%d')}_{uuid.uuid4()}{ext}"
owncloud_fullpath = path + "/" + owncloud_filename
oc.put_file(owncloud_fullpath, filename)
file_info = oc.share_file_with_link(owncloud_fullpath)
return file_info.get_link()
@staticmethod
def get_damages(depot_id):
oc = owncloud.Client(settings.OWNCLOUD_URL)
oc.login(settings.OWNCLOUD_USER, settings.OWNCLOUD_PASSWORD)
files = oc.list(f"{settings.OWNCLOUD_DAMAGES_FOLDER}{str(depot_id)}")
damages = []
for file_info in files:
if file_info.is_dir():
continue # Пропускаме поддиректории
# Създаване на публичен линк за всеки файл
link_info = oc.share_file_with_link(file_info.path)
damages.append(link_info.get_link())
return Response(damages, status=status.HTTP_200_OK)