package com.qiaoba.module.file.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.qiaoba.common.base.constant.BaseConstant;
import com.qiaoba.common.base.exception.ServiceException;
import com.qiaoba.module.file.config.FileConfig;
import com.qiaoba.module.file.service.FileService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件管理 服务层实现
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/5/19 9:15
 */
@Service
@RequiredArgsConstructor
public class FileServiceImpl implements FileService {

    private FileConfig fileConfig;

    @Override
    public String upload(MultipartFile file) {
        return com.qiaoba.module.file.util.FileUtil.upload(file, fileConfig.getUploadPath());
    }

    @Override
    public boolean delete(String path) {
        String localPath = fileConfig.getUploadPath() + StrUtil.removePrefix(path, BaseConstant.RESOURCE_PREFIX);
        if (FileUtil.isDirectory(localPath)) {
            throw new ServiceException("删除路径异常, 请勿非法操作");
        }
        return FileUtil.del(localPath);
    }
}
