import os

from django.core.paginator import Paginator

from DCbackend.utils.common import success, fail, pageData, handle_uploaded_file, handle_uploaded_image, oss_upload_file, oss_upload_image, handle_uploaded_image
from backend.Form.ProjectCategoryForm import ProjectCategoryForm
from backend.Form.ProjectForm import ProjectForm
from backend.Service.ProjectCategoryService import ProjectCategoryService
from backend.models import ProjectCategory, DefaultStatus, Project
from DCbackend.utils.common import success_image_upload


class UploadService:

    @staticmethod
    def File(request):
        file = request.FILES['file']
        localFile=handle_uploaded_file(file)
        ossFile = oss_upload_file(localFile)
        data = {
            'fileUrl': ossFile,
        }
        if os.path.exists(localFile):
            os.remove(localFile)

        return success(data)

    @staticmethod
    def Image(request):
        file = request.FILES['upload']
        localFile = handle_uploaded_image(file)
        original_filename = file.name  # 获取原始文件名
        ossFile = oss_upload_image(localFile)

        # 如果 handle_uploaded_image 返回完整路径，这里就不需要 os.path.join
        if os.path.exists(localFile):
            os.remove(localFile)

        return success_image_upload(url=ossFile, filename=original_filename)