package tech.easyflow.common.filestorage.impl;

import org.dromara.x.file.storage.core.FileInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import tech.easyflow.common.filestorage.FileStorageService;
import tech.easyflow.common.filestorage.utils.PathGeneratorUtil;
import tech.easyflow.common.util.OkHttpUtil;

import java.io.*;

@Component("xFileStorage")
public class XFIleStorageServiceImpl implements FileStorageService {

    private static final Logger LOG = LoggerFactory.getLogger(XFIleStorageServiceImpl.class);

    @Autowired
    private org.dromara.x.file.storage.core.FileStorageService fileStorageService;

    @Override
    public String save(MultipartFile file) {
        return save(file, null);
    }

    @Override
    public String save(MultipartFile file, String prePath) {
        String uploadPath = PathGeneratorUtil.generateUserPath("");
        if (StringUtils.hasText(prePath)) {
            String normalized = prePath.replaceAll("^/+", "").replaceAll("/+$", "");
            uploadPath = "/" + normalized + uploadPath;
        }
        FileInfo fileInfo = fileStorageService.of(file)
                .setPath(uploadPath)
                .setSaveFilename(file.getOriginalFilename())
                .setContentType(getFileContentType(file))
                .upload();
        if (fileInfo == null || !StringUtils.hasText(fileInfo.getUrl())) {
            throw new RuntimeException("文件上传失败");
        }
        return fileInfo.getUrl();
    }

    @Override
    public void delete(String path) {
        boolean deleted = fileStorageService.delete(path);
        if (!deleted) {
            LOG.warn("删除文件失败或文件不存在，path={}", path);
        }
    }

    @Override
    public InputStream readStream(String fileUrl) {
        return OkHttpUtil.getInputStream(fileUrl);
    }

    /**
     * 获取S3中文件的大小（单位：字节）
     * @param path 文件路径
     * @return 文件大小（字节）
     */
    @Override
    public long getFileSize(String path) {
        try {
            return OkHttpUtil.getFileSize(path);
        } catch (Exception e) {
            LOG.error("获取文件大小失败", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取文件的 Content-Type
     */
    public static String getFileContentType(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String contentType = null;
        if (originalFilename != null && originalFilename.toLowerCase().endsWith(".txt")) {
            contentType = "text/plain; charset=utf-8";
        } else {
            // 其他类型文件可以按需设置
            contentType = file.getContentType();
        }
        return contentType;
    }
}
