switch from ownCloud to minIO
buttons in table are buttons and do edit and delete properly
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
from minio import Minio
|
||||
from django.conf import settings
|
||||
from datetime import datetime, timedelta
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
import boto3
|
||||
from botocore.client import Config
|
||||
|
||||
client = Minio(
|
||||
settings.MINIO_ENDPOINT,
|
||||
access_key=settings.MINIO_ACCESS_KEY,
|
||||
secret_key=settings.MINIO_SECRET_KEY,
|
||||
secure=settings.MINIO_SECURE
|
||||
)
|
||||
|
||||
|
||||
def upload_damage_photo(photo_file, depot_id, content_type):
|
||||
try:
|
||||
s3_client = boto3.client('s3',
|
||||
endpoint_url=f'http://{settings.MINIO_ENDPOINT}',
|
||||
aws_access_key_id=settings.MINIO_ACCESS_KEY,
|
||||
aws_secret_access_key=settings.MINIO_SECRET_KEY,
|
||||
config=Config(signature_version='s3v4'),
|
||||
region_name='us-east-1'
|
||||
)
|
||||
|
||||
# Use the full filename as the key, which includes the depot_id folder
|
||||
s3_client.upload_fileobj(
|
||||
photo_file,
|
||||
settings.MINIO_BUCKET_NAME,
|
||||
photo_file.name,
|
||||
ExtraArgs={'ContentType': content_type}
|
||||
)
|
||||
|
||||
# Generate a presigned URL for the uploaded object
|
||||
url = s3_client.generate_presigned_url('get_object',
|
||||
Params={
|
||||
'Bucket': settings.MINIO_BUCKET_NAME,
|
||||
'Key': photo_file.name
|
||||
},
|
||||
ExpiresIn=3600
|
||||
)
|
||||
|
||||
return url
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error uploading to MinIO: {str(e)}")
|
||||
raise
|
||||
|
||||
|
||||
def get_damages(depot_id):
|
||||
try:
|
||||
objects = client.list_objects(
|
||||
settings.MINIO_BUCKET_NAME,
|
||||
prefix=f"{depot_id}/"
|
||||
)
|
||||
|
||||
damages = []
|
||||
for obj in objects:
|
||||
url = client.presigned_get_object(
|
||||
settings.MINIO_BUCKET_NAME,
|
||||
obj.object_name,
|
||||
expires=timedelta(days=7)
|
||||
)
|
||||
damages.append({
|
||||
"url": url,
|
||||
"filename": obj.object_name.split('/')[-1]
|
||||
})
|
||||
|
||||
return Response(damages, status=status.HTTP_200_OK)
|
||||
except Exception as e:
|
||||
print(f"Error listing objects from MinIO: {str(e)}")
|
||||
return Response([], status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
@@ -2,11 +2,15 @@ import os
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from django.conf import settings
|
||||
from django.http import HttpResponse
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
import owncloud
|
||||
from owncloud import (HTTPResponseError)
|
||||
|
||||
import xml.etree.ElementTree as ET
|
||||
import requests
|
||||
|
||||
|
||||
class Owncloud:
|
||||
@staticmethod
|
||||
@@ -33,8 +37,41 @@ class Owncloud:
|
||||
damages = []
|
||||
for file_info in files:
|
||||
if file_info.is_dir():
|
||||
continue # Пропускаме поддиректории
|
||||
# Създаване на публичен линк за всеки файл
|
||||
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)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def get_damages_webdav(depot_id):
|
||||
oc = owncloud.Client(settings.OWNCLOUD_URL)
|
||||
oc.login(settings.OWNCLOUD_USER, settings.OWNCLOUD_PASSWORD)
|
||||
path = f"{settings.OWNCLOUD_DAMAGES_FOLDER}{str(depot_id)}"
|
||||
files = oc.list(path)
|
||||
damages = []
|
||||
|
||||
for file_info in files:
|
||||
if file_info.is_dir():
|
||||
continue
|
||||
|
||||
original_filename = os.path.basename(file_info.path)
|
||||
link_info = oc.share_file_with_link(file_info.path)
|
||||
share_url = link_info.get_link()
|
||||
token = share_url.rstrip('/').split('/')[-1]
|
||||
|
||||
damages.append({
|
||||
"token": token,
|
||||
"filename": original_filename
|
||||
})
|
||||
|
||||
return Response(damages, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
def proxy_owncloud_image(request, token, filename):
|
||||
url = f"{settings.OWNCLOUD_URL}/public.php/webdav/{filename}"
|
||||
r = requests.get(url, auth=(token, ''), stream=True)
|
||||
if r.status_code == 200:
|
||||
content_type = r.headers.get('Content-Type', 'image/jpeg')
|
||||
return HttpResponse(r.content, content_type=content_type)
|
||||
return HttpResponse("Not found", status=404)
|
||||
Reference in New Issue
Block a user