ShowCtrl.java 7.69 KB
package com.example.demo;

import freemarker.template.utility.StringUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;

/**
 * Created by jack on 2018/4/18.
 */
@RestController
@RequestMapping(path = "/show")
public class ShowCtrl {
    private static  final Logger logger = LoggerFactory.getLogger(ShowCtrl.class);
//    private static  final String  DEFAULT_DIR = "E:\\图片\\demo.txt";
    private static  final String  DEFAULT_DIR = "/home/jack/makesample/zhurui/shoes/ali_similar.info";

    @Autowired
    ShowSer showSer;


    @RequestMapping("/index")
    public ModelAndView index() {
        return new ModelAndView("index");
    }

    @GetMapping(path = "/load")
    public ModelAndView loadPhotoDir(@RequestParam String curDir) {
        ModelAndView mv = new ModelAndView("showImage");
        showSer.loadDir(curDir);
        mv.addObject("curDir",curDir);
        mv.addObject("photoViewObj", showSer.fetchCurentPhotos(curDir, 0));
        return mv;
    }

    //跳转至样本展示页面
    @GetMapping(path = "/toSampleShow")
    public ModelAndView toSampleShow() {
       return new ModelAndView("showSamples");
    }

    /**
     * 获取样本table 分页数据
     * @param req
     * @param sampleDir
     * @return
     */
    @RequestMapping ("/getSamplesByPage")
    @ResponseBody
    public PageListResp<SampleViewObj> getSamplesByPage(PageTableReq req,String sampleDir ) {
        if(! showSer.samplesMap.containsKey(sampleDir)){
            showSer.loadSampleInfo(sampleDir);//统计样本个数
        }
        Integer count = showSer.samplesMap.get(sampleDir);
        if(count == null || count == 0){//加载文件失败
            return PageListResp.newBuilder().iTotalDisplayRecords(0).iTotalRecords(0).aaData(new ArrayList()).build();
        }
        PageListResp.Builder pageListResp = PageListResp.newBuilder().iTotalDisplayRecords(count.intValue()).iTotalRecords(count.intValue());
        pageListResp.aaData(showSer.fetchSamplesByPage(sampleDir, req.getIDisplayStart(), req.getIDisplayLength()));
        return pageListResp.build();
    }

    @GetMapping(path = "/next")
    public ModelAndView nextPhoto(@RequestParam String curDir) {
        ModelAndView mv = new ModelAndView("showImage");
        mv.addObject("curDir",curDir);
        mv.addObject("photoViewObj", showSer.fetchCurentPhotos(curDir, 1));
        return mv;
    }

    @GetMapping(path = "/prev")
    public ModelAndView prevPhoto(@RequestParam String curDir) {
        ModelAndView mv = new ModelAndView("showImage");
        mv.addObject("curDir",curDir);
        mv.addObject("photoViewObj", showSer.fetchPrevPhotos(curDir));
        return mv;

    }

    @GetMapping(path = "/jump")
    public ModelAndView jumpSknPhoto(@RequestParam("curDir") String curDir, @RequestParam String skn) {
        ModelAndView mv = new ModelAndView("showImage");
        mv.addObject("curDir",curDir);
        mv.addObject("photoViewObj", showSer.fetchSknPhotos(curDir, skn));
        return mv;

    }

    @GetMapping(path = "/read")
    public Object readPhotoBytes(@RequestParam("curDir") String curDir, @RequestParam("skn") String skn, @RequestParam("order") String order) {
        try {
            return showSer.readPhotoBytes(curDir, skn, order);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping(path = "/readAli")
    public Object readPhotoBytes(@RequestParam("file") String filePath) {
        try {
            String s  = URLDecoder.decode(filePath, "UTF-8");
            logger.info("readPhotoBytes --文件路径filePath {}" , filePath );
            return showSer.readPhotoBytes( s);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 更新update文件
     * @param flag
     * @return
     */
 /*   @GetMapping("/updateSampleInfo")
    public String updateSampleInfo(boolean flag, int currentIndex, String sampleDir){
        File file = new File(sampleDir + "/update.info");
        if(!showSer.checkFile(file)){
            return "创建文件失败!";
        }

        if( !showSer.modifyFile(file, currentIndex, flag)){
            return "更新失败!";
        }
        return "";
    }
*/

    @GetMapping("/updateSamplesByBatch")
    public String updateSamplesByBatch(boolean flag, String indexListStr, String sampleDir){
        File file = new File(sampleDir + "/update.info");
        if(!showSer.checkFile(file)){
            return "创建文件失败!";
        }
        if( !showSer.modifyFile(file, indexListStr, flag)){
            return "更新失败!";
        }
        return "";
    }

    @RequestMapping("/loadSingleImage")
    public ModelAndView loadSingleImage(){
        return  new ModelAndView("loadSingleImage");
    }



    @RequestMapping("/updateImageFile")
    @ResponseBody
    public String updateImageFile(@RequestParam String mainPicSrc, @RequestParam String removeImageSrc, @RequestParam String curDir){
        try {
            //特殊字符--解码
            mainPicSrc = URLDecoder.decode(mainPicSrc, "UTF-8");
            removeImageSrc = URLDecoder.decode(removeImageSrc, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "解析字符串出错!";
        }
       String s[] = StringUtils.split(mainPicSrc, "&");
       String skn = "";//skn名称
       for(int i = 0 ; i<s.length; i++){
           if(StringUtils.startsWith(s[i], "skn")){
               skn = s[i].substring( s[i].indexOf("=") + 1);
           }
       }

        File file = new File(curDir + "/" + skn + ".txt" );
        if( !showSer.checkFile(file)){
            return "创建文件失败!";
        };//创建文件

        String mainImageName = showSer.getImageNameBySrc( mainPicSrc, curDir, skn) ;//main image name
        String removeImageName = showSer.getImageNameBySrc(removeImageSrc, curDir, skn) ;//remove image name
        String content =  mainImageName + "," + removeImageName;
        if(  !showSer.insertLine(file, content)){
            return "写入文件失败!";
        };//写入文件

        return "";

    }

    //跳转至样本展示页面--样本 和 前 50个展示
    @GetMapping(path = "/toSamples")
    public ModelAndView toSamples() {
        return new ModelAndView("showSampleAndSimilar");
    }

    /**
     * 获取样本table 分页数据
     * @param req
     * @param sampleDir
     * @return
     */
    @RequestMapping ("/loadImagesFromFile")
    @ResponseBody
    public PageListResp<ImageAndSimilarResp> loadImagesFromFile(PageTableReq req,String sampleDir ) {
        int count = 0;
        PageListResp.Builder pageListResp = PageListResp.newBuilder().iTotalDisplayRecords(count).iTotalRecords(count);
        if(StringUtils.isEmpty(  sampleDir )){
            sampleDir = DEFAULT_DIR;
        }
        File file = new File(sampleDir);
        if(!file.exists()){
            return null;
        }
        count = showSer.countFileLines(file);//总行数


        if( count > 0){//当前行的数据
            pageListResp.iTotalDisplayRecords(count).iTotalRecords(count);
            pageListResp.aaData(showSer.getImagesPyPage(file, req.getIDisplayStart(), req.getIDisplayLength()));
        }else{
            pageListResp.aaData(new ArrayList());
        }


        return pageListResp.build();
    }




}