/**
 * @Author: Kane Wang <wangkane@qq.com>
 * @Date: 2025-10-15 11:42:56
 * @LastEditors: Kane Wang
 * @LastModified: 2025-10-15 17:48:52
 * @FilePath: src/main/java/com/cpic/xim/web/controllers/fileupload/FileUpload.java
 * @Description: 用于接受上传文件的Controller。
 *
 *               Copyright (c) 2025 by Kane All rights reserved
 */
package com.cpic.xim.web.controllers.fileupload;

import java.io.File;
import java.util.Vector;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
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;

import com.cpic.xim.utils.files.SaveUploadFile;
import com.cpic.xim.utils.files.UploadedFile;
import com.cpic.xim.utils.files.exceptions.MoveUploadedFileException;
import com.cpic.xim.utils.files.exceptions.ProcessUploadedFileException;

import jakarta.servlet.http.HttpServletRequest;

// @SuppressWarnings( "unused" )
@Controller
@RequestMapping( path = "/file" )
public class UploadFileController
{
    /**
     * 接收上传文件，并保存到临时目录：
     * 1、临时目录下再用sessionID作为子目录保存文件。
     * 2、保存时不更改文件名，会覆盖同名文件。
     * 3、MultipartFile参数形参名称必须和请求form中file标签的name属性一致，否则值为null。
     * 4、返回值为接收结果和文件保存绝对路径。
     * 
     * @param fileName
     * @param file
     * @param request
     * @return FileUploadResult 文件上传结果对象
     */
    @RequestMapping( path = "/file-upload.do" )
    @ResponseBody
    public UploadFileResponse saveUploadFile(
            @RequestParam( "fileName" ) String fileName,
            @RequestParam( "files" ) MultipartFile file,
            HttpServletRequest request
    )
    {
        // session id用来创建临时目录，避免重复
        String             sessionID = request.getSession().getId();
        UploadFileResponse result    = new UploadFileResponse();
        MultipartFile[]    files     = new MultipartFile[1];

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

        String relativeFilePath = "/temp/upload/" + sessionID;
        String absolutefilePath = request.getServletContext().getRealPath( relativeFilePath );
        File   dir              = new File( absolutefilePath );

        // 创建临时目录
        if ( (!dir.exists()) && (!dir.mkdirs()) )
        {
            result.setSuccess( false );
            result.setMessage( "创建临时目录失败：" + absolutefilePath );

            return result;
        }

        try
        {
            files[0] = file;

            Vector<UploadedFile> uploadFiles = SaveUploadFile.saveUploadFiles( files,
                                                                               absolutefilePath,
                                                                               relativeFilePath );

            result.setFileList( uploadFiles );
        }
        catch ( ProcessUploadedFileException error )
        {
            result.setSuccess( false );
            result.setMessage( error.getMessage() );
        }

        return result;
    }

    @RequestMapping( path = "/move-file.do" )
    @ResponseBody
    public MoveFileResponse moveFile( @RequestBody MoveFileRequest request )
    {
        MoveFileResponse response = new MoveFileResponse( false, null, null );

        try
        {
            SaveUploadFile.MoveUploadedFile( request.getOriginFilePath(), request.getDestFilePath() );

            response.setSuccess( true );
            response.setMessage( "移动文件路径完成！" );
            response.setNewFilePath( request.getDestFilePath() );
        }
        catch ( MoveUploadedFileException error )
        {
            response.setSuccess( false );
            response.setMessage( error.getMessage() );
            response.setNewFilePath( request.getDestFilePath() );
        }

        return response;
    }
}
