package org.security.trans.common.service.impl;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.owasp.esapi.ESAPI;
import org.security.trans.common.service.UploadService;
import org.security.ht.vo.common.UploadVO;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zj 2022/5/11
 */
@Slf4j
@AllArgsConstructor
@Service
public class UploadServiceImpl implements UploadService {

	@Override
	public UploadVO uploadSingleFile(MultipartFile file, String groupType, String groupName) throws IOException {
		//将文件存储在本地
		String fileName = file.getOriginalFilename();
		String filePath = "E:\\安防信息化项目\\uploadFile";
		// if (!ESAPI.validator().isValidFileName("upload", fileName, false)) {
		// 	throw new IOException("非法的文件名");
		// }
		File dest = new File(new File(filePath).getAbsoluteFile() + File.separator + fileName);
		dest.createNewFile();
		try {
			file.transferTo(dest);
			log.info("文件上传本地成功");
		}catch (IOException e){
			log.error("文件上传本地失败"+e);
		}
		//将文件信息告知影像平台

		return UploadVO.builder()
			.name(fileName)
			.fileType(StringUtils.substring(fileName, fileName.lastIndexOf('.') + 1))
			.url(filePath)
			.groupName(groupName)
			.build();
	}

	@Override
	public List<UploadVO> uploadListFile(List<MultipartFile> files, String groupType, String groupName) {
		ArrayList<UploadVO> res = new ArrayList<>();
//		for (MultipartFile file : files) {
//			try {
//				String originalFileName = file.getOriginalFilename();
//				BladeFile bladeFile = qiniuTemplate.putFile(ossProperties.getBucketName(), originalFileName, file.getInputStream());
//				UploadVO uploadVO = new UploadVO();
//				uploadVO.setName(originalFileName);
//				uploadVO.setFileType(StringUtils.substring(originalFileName, originalFileName.lastIndexOf(".") + 1));
//				uploadVO.setUrl(bladeFile.getLink());
//				uploadVO.setGroupType(groupType);
//				uploadVO.setGroupName(groupName);
//				res.add(uploadVO);
//			} catch (IOException e) {
//				log.error("上传文件异常：", e);
//			}
//		}
		return res;
	}
}
