package com.bonus.file.controller;

import com.bonus.common.core.utils.Base64Utils;
import com.bonus.common.core.utils.global.SystemGlobal;
import com.bonus.file.utils.FileDownloadUtils;
import com.bonus.system.api.domain.SysFileVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.bonus.common.core.domain.R;
import com.bonus.common.core.utils.file.FileUtils;
import com.bonus.file.service.ISysFileService;
import com.bonus.system.api.domain.SysFile;

import java.io.IOException;

/**
 * 文件请求处理
 * 
 * @author bonus
 */
@RestController
public class SysFileController {
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);

    @Autowired
    private ISysFileService sysFileService;
    /**
     * 存储文件
     */
    @Value("${file.save-type}")
    public    String saveType;
    /**
     * 文件上传请求
     */
    @PostMapping("upload")
    public R<SysFileVo> upload(MultipartFile file) {
        try {
            if(saveType.equals(SystemGlobal.LOCAL)){
                SysFileVo  fileVo= uploadByLocal(file);
                return R.ok(fileVo);
            }else {//默认本地存储
                SysFileVo  fileVo= uploadByLocal(file);
                return R.ok(fileVo);
            }
        }
        catch (Exception e) {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }
    /**
     * 本地文件上传
     * @return
     */
    public   SysFileVo  uploadByLocal(MultipartFile file) throws Exception {
        // 上传并返回访问地址
        String url = sysFileService.uploadFile(file);
        SysFileVo sysFile = new SysFileVo();
        sysFile.setFileName(FileUtils.getName(url));
        sysFile.setUrl(url);
            return  sysFile;
    }


    @GetMapping("/download")
    public R<Boolean> downloadFile(String url, String destination) {
        try {
                String fileUrl = Base64Utils.decodeUrl(url);
                if (fileUrl != null) {
                String fileName = Base64Utils.getFileNameFromURL(fileUrl);
                sysFileService.downloadFile(fileUrl, destination + "/" + fileName);
                return R.ok();
                }
        } catch (Exception e) {
            log.error("下载文件失败", e);
            return R.fail(e.getMessage());
        }
        return R.fail("下载文件失败");
    }

    @GetMapping("/delete")
    public R<Boolean> deleteFile(String url) {
        try {
            String fileUrl = Base64Utils.decodeUrl(url);
            if (fileUrl != null) {
                sysFileService.deleteFile(fileUrl);
                return R.ok();
            }
        }  catch (Exception e) {
            log.error("delete文件失败", e);
            return R.fail(e.getMessage());
        }
        return R.fail("传入参数不满足要求");
    }
}