/*
 * @Author: Kane
 * @Date: 2023-01-22 23:11:26
 * @LastEditors: Kane
 * @LastEditTime: 2023-01-23 23:22:10
 * @FilePath: \AdminSys\src\main\java\com\cpic\xim\web\controllers\FileUpload\FileUpload.java
 * @Description: 用于接受上传文件的Controller。
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */

package com.cpic.xim.web.controllers.FileUpload;

import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@SuppressWarnings( "unused" )
@Controller
@RequestMapping( path = "/file" )
public class FileUpload
{
    @RequestMapping( path = "/file-upload.do" )
    @ResponseBody
    public FileUploadResult getUploadFile( @RequestParam( "task-name" ) String taskName,
            @RequestParam( "file" ) MultipartFile[] files, HttpServletRequest request )
    {
        // session id用来创建临时目录，避免重复
        String sessionID = request.getSession().getId();
        FileUploadResult result = new FileUploadResult();
        Vector<String> fileNames = new Vector<String>();
        int fileCount = files.length;

        if ( files.length == 0 )
        {
            result.setSuccess( false );
            result.setMessage( "此接口用于上传文件!" );
        }

        result.setSuccess( true );
        result.setMessage( "上传成功!" );

        for ( MultipartFile file : files )
        {
            String fileName = file.getName();

            fileNames.add( fileName );
        }

        result.setFileList( fileNames );

        return result;
    }
}
