/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.cqcis.com</a>
 */
package com.crunii.micro.service.sysmgr.controller;

import com.crunii.micro.common.annotations.LimitUploadFileTypeCheck;
import com.crunii.micro.common.dto.FileResponse;
import com.crunii.micro.common.dto.Result;
import com.crunii.micro.domain.dto.Worker;
import com.crunii.micro.domain.entity.main.TsmEncryptTest;
import com.crunii.micro.service.sysmgr.dto.sysmgr.FileInfo;
import com.crunii.micro.service.sysmgr.service.FileService;
import com.crunii.micro.web.mvc.CurrWorker;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * @author 田平 create 2023-02-21 10:47:48
 */
@Api(value = "文件操作")
@RestController
@RequestMapping("/FileController")
public class FileController {
	@Autowired
	private FileService fileService;

	private final Long fileLimit = 50L * 1024 * 1024;

	@ApiOperation(value = "上传图片")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "fileName", value = "form的input名称", required = true)
	})
	@PostMapping(path = "/auth/uploadImage")
	@ResponseBody
	public Result<String> uploadImage(@RequestParam("fileName") MultipartFile file) {
		Result<String> result = new Result<String>();
		result.setData(fileService.doStoreImage(file));
		return result;
	}

	@ApiOperation(value = "通过文件id下载文件")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "fileId", value = "文件Id", required = true)
	})
	@GetMapping(path = "/downloadFile")
	public ResponseEntity<byte[]> downloadFile(String fileId) {
		FileInfo fileInfo = fileService.getFileInfo(fileId);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentDispositionFormData("attachment", fileInfo.getFile().getFileStoreName());
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentLength(fileInfo.getFileBytes().length);
		ResponseEntity<byte[]> resp = new ResponseEntity<byte[]>(fileInfo.getFileBytes(), headers, HttpStatus.OK);
		return resp;
	}

	@GetMapping(path = "/encryptTest")
	@ResponseBody
	public Result<TsmEncryptTest> encryptTest() {
		Result<TsmEncryptTest> result = new Result<TsmEncryptTest>();
		result.setData(fileService.getEncrypt(1));
		return result;
	}

	@ApiOperation(value = "上传附件")
	@PostMapping(path = "/auth/uploadFile")
	@LimitUploadFileTypeCheck(message = "不支持的文件格式")
	@ResponseBody
	public Result<FileResponse> uploadFile(@ApiParam @Valid @RequestPart("fileName") MultipartFile file, @CurrWorker @ApiIgnore Worker worker) {
		Result<FileResponse> result = new Result();
		if (file.getSize() <= this.fileLimit) {
			// 小于10M
			result.setSuccess(true);
			result.setData(this.fileService.doStoreFile(file, worker));
		} else {
			result.setSuccess(false);
			result.setMsg("上传文件大小超过了指定大小" + this.fileLimit / 1024 / 1024 + "MB大小!");
		}

		return result;
	}
}
