/**
 * 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.dto.Result;
import com.crunii.micro.service.sysmgr.service.UpDownDemoService;
import io.swagger.annotations.Api;
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;

/**
 * @author 田平 create 2023-02-21 10:48:35
 */
@Api(value = "上传下载相关")
@RestController
@RequestMapping("/UpDownDemoController")
public class UpDownDemoController {
	@Autowired
	private UpDownDemoService upDownDemoService;

	@GetMapping(path = "/downloadFile")
	public ResponseEntity<byte[]> downloadFile(String path, String name) {
		byte[] bytes = upDownDemoService.getFileBytes(path, name);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentDispositionFormData("attachment", "demoDown.txt");
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		ResponseEntity<byte[]> resp = new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
		return resp;
	}

	@PostMapping(path = "/uploadFile")
	@ResponseBody
	public Result<String> uploadFile(@RequestParam("fileName") MultipartFile file) {
		Result<String> result = new Result<>();
		upDownDemoService.storeFile(file);
		return result;
	}

	@PostMapping(path = "/uploadFiles")
	@ResponseBody
	public Result<String> uploadFiles(@RequestParam("fileName") MultipartFile[] files) {
		Result<String> result = new Result<>();
		for (MultipartFile file : files) {
			upDownDemoService.storeFile(file);
		}
		return result;
	}

}
