package com.chinaweal.aiccs.common.util.filestorage.impl;

import cn.hutool.core.io.FileUtil;
import com.chinaweal.aiccs.common.util.filestorage.FileStorageService;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 本地文件存储下载服务实现.
 *
 * @author yangmc
 * @date 2021-11-06
 * @time 13:27
 */
@Component
@ConditionalOnProperty(prefix = "storage", name = "mode", havingValue = "normal")
@Slf4j
public class FileStorageServiceImpl implements FileStorageService {

    /**
     * 复制文件到指定目录，可选：
     *
     * @param path 服务端路径，可以为{@code null} 或者相对路径或绝对路径
     * @param file     文件
     * @return 是否复制成功
     */
    @Override
    public boolean writeFromFile(String path, File file) {
        FileUtil.copy(file, FileUtil.file(path),true);
        return true;
    }

    /**
     * 写入文件到指定目录
     *
     * @param path       文件路径
     * @param fileStream 文件流
     * @return 是否写入成功
     */
    @Override
    public boolean writeFromStream(String path, InputStream fileStream) {
        FileUtil.writeFromStream(fileStream, FileUtil.file(path), true);
        return true;
    }

    /**
     * 写入文件到指定目录
     *
     * @param path       文件夹路径
     * @param fileName   文件名
     * @param fileStream 文件流
     * @return 是否写入成功
     */
    @Override
    public boolean writeFromStream(String path, String fileName, InputStream fileStream) {
        FileUtil.writeFromStream(fileStream, FileUtil.file(path, fileName), true);
        return true;
    }

    @Override
    public void writeToFile(String path, File outFile) {
        FileUtil.copy(FileUtil.file(path), outFile, true);
    }

    @Override
    public void writeToFile(String path, String fileName, File outFile) {
        FileUtil.copy(FileUtil.file(path, fileName), outFile, true);
    }

    /**
     * 下载文件到输出流
     *
     * @param path      文件路径，包含文件名
     * @param out      输出位
     */
    @Override
    public void writeToStream(String path, OutputStream out) {
        FileUtil.writeToStream(FileUtil.file(path), out);
    }

    /**
     * 下载文件到输出流
     *
     * @param path     文件路径
     * @param fileName 文件名
     * @param out      输出位置
     */
    @Override
    public void writeToStream(String path, String fileName, OutputStream out) {
        FileUtil.writeToStream(FileUtil.file(path, fileName), out);
    }

    @Override
    public boolean delFile(String path) {
        if (!FileUtil.isFile(path)) {
            throw new BusinessException("删除文件失败，经检测path不是文件路径或文件不存在，请检查");
        }
        return FileUtil.del(path);
    }

    @Override
    public boolean delDir(String dirPath) {
        if (!FileUtil.isDirectory(dirPath)) {
            throw new BusinessException("删除文件夹失败，经检测dirPath不是文件夹路径或文件夹不存在，请检查");
        }
        return FileUtil.del(dirPath);
    }

    @Override
    public List<String> getFtpFileNames(String path) {
        List<String> filenames = new ArrayList<>();
        if (FileUtil.exist(path)) {
            filenames = FileUtil.listFileNames(path);
        }
        return filenames;
    }
}
