package com.rf.psychological.file;

import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.utils.OSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author zsy
 * @description:download APK 下载安装包
 * @date 2021/5/11 10:57
 */
@Slf4j
@RestController
@RequestMapping("/apk")
@Api(tags = "下载安装包")
public class ApkDownloadController extends BaseController {

    @ApiOperation(value = "下载安装包")
    @GetMapping("")
    public String downloadFile(HttpServletResponse response) throws Exception {
        String fileName = "pluse_app_2.1.apk";
        if (fileName != null) {
//            fileName = fileName.replace('_','-')+""
            File file = null;
            //设置文件路径
            if(OSUtil.isLinux()){

                /*if("scale".equals(flag)){
                    file = new File("./scales/"+fileName);
                }else{
                    file = new File("./"+fileName);
                }*/
            }else{

                String filepath = ApkDownloadController.txt2String(new File("C:/DataPilot/file-group-apk.txt"));
                file = new File(filepath);
            }
            if (file.exists()) {
               /* response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                response.setContentType("application/binary;charset=ISO8859-1");*/
                response.reset();
                response.setContentType("application/octet-stream");
                response.setCharacterEncoding("utf-8");
                response.setContentLength((int) file.length());
                response.setHeader("Content-Disposition", "attachment;filename="+ java.net.URLEncoder.encode(fileName, "UTF-8"));

                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    return "下载成功";
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return "下载失败";
    }

    /**
     * 读取txt文件的内容
     * @param file 想要读取的文件对象
     * @return 返回文件内容
     */
    public static String txt2String(File file){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                //result.append(System.lineSeparator()+s);换行
                result.append(s);
            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }


}


