package com.jianshui.storage.service.impl;

import cn.hutool.core.codec.Base64;
import com.jianshui.common.config.JianshuiConfig;
import com.jianshui.common.core.domain.AjaxResult;
import com.jianshui.common.utils.AESUtil1;
import com.jianshui.common.utils.StringUtils;
import com.jianshui.common.utils.file.FileUploadUtils;
import com.jianshui.common.utils.file.FileUtils;
import com.jianshui.common.utils.spring.SpringUtils;
import com.jianshui.common.utils.uuid.IdUtils;
import com.jianshui.framework.config.ServerConfig;
import com.jianshui.storage.domain.StorageUrlDTO;
import com.jianshui.storage.service.StorageBaseAbstractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Description
 * @Author 巩权林
 * @Date 2023/2/28 21:01
 **/
@Service("storage_local")
@Slf4j
public class LocalStorageService extends StorageBaseAbstractService {

    @Autowired
    private ServerConfig serverConfig;

    private static final String AES_ENCRYPT_KEY = "jianshui_local_storage_aes_key_#EDC3edc$RFV4rfv";

    @Override
    public AjaxResult getFile(StorageUrlDTO storageUrlDTO) {
        String realFilePath = AESUtil1.decrypt(storageUrlDTO.getStoragePath(), AES_ENCRYPT_KEY);
        String fileName = realFilePath.substring(realFilePath.lastIndexOf(JianshuiConfig.getDownloadPath()) + 1);
        String url = serverConfig.getUrl() + fileName; // 下载路径名
        return AjaxResult.success("success", url);
    }

    @Override
    public AjaxResult upload(byte[] data, String storagePath) {
        // data存到临时文件
        // data转FileInputStream
        String key = IdUtils.fastSimpleUUID();
        String fileName = key + ".tmp";
        String fileDir = JianshuiConfig.getDownloadPath() + "/storage/";
        // 判断fileDir是否存在，不存在则创建
        File fileDirInst = new File(fileDir);
        if (!fileDirInst.exists()) {
            fileDirInst.mkdirs();
        }
        String realPath = fileDir + fileName;
        FileUtils.byteToFile(data, realPath);
        return SpringUtils.getAopProxy(this).upload(realPath, storagePath);
    }


    @Override
    public AjaxResult upload(String filePath, String storagePath) {
        // 上传文件路径
        File file = new File(filePath);
        // 转MultipartFile
        MultipartFile multipartFile = null;
        try {
            multipartFile = new MockMultipartFile(file.getName(), file.getName(), "text/plain", new FileInputStream(file));
        } catch (Exception e) {
            log.error("文件转换失败", e);
        }
        if (multipartFile == null) {
            return AjaxResult.error("文件转换失败");
        }
        // 上传并返回新文件名称
        String fileName = null;
        try {
            fileName = FileUploadUtils.upload(filePath, multipartFile); // 上传路径名
        } catch (IOException e) {
            log.error("上传文件失败", e);
            return AjaxResult.error("上传文件失败");
        }
        // String url = serverConfig.getUrl() + fileName; // 下载路径名
        String realFilePath = JianshuiConfig.getDownloadPath() + fileName; // 本地存储路径
        StorageUrlDTO storageUrlDTO = new StorageUrlDTO();
        String key = AESUtil1.aesencrypt(realFilePath, AES_ENCRYPT_KEY);
        storageUrlDTO.setStoragePath(key);
        storageUrlDTO.setServiceProvider("local");
        return AjaxResult.success(storageUrlDTO);
    }

    @Override
    public AjaxResult init() {
        return AjaxResult.success();
    }
}
