package org.xmx0632.deliciousfruit.service;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.xmx0632.deliciousfruit.api.v1.bo.TerminalType;
import org.xmx0632.deliciousfruit.api.v1.helper.PictureUrlHelper;
import org.xmx0632.deliciousfruit.global.ConfigConstant;
@Component
public class UploadFileService {
private static Logger log = LoggerFactory
.getLogger(UploadFileService.class);
@Autowired
private ConfigService configService;
@Autowired
private UploadFilePathService uploadFilePathService;
@Autowired
private WaterMarkService waterMarkService;
public void saveCategoryImageFile(MultipartFile file, Long id,
TerminalType terminalType) throws ServiceException {
if (file == null) {
log.debug("file is null,do nothing");
return;
}
String originalFilename = file.getOriginalFilename();
if (StringUtils.isBlank(originalFilename)) {
log.debug("Filename is null,do nothing");
return;
}
String targetDir = uploadFilePathService
.getCategoryTargetDirBy(terminalType);
checkIconAndSaveFile(id, terminalType, file, targetDir);
return;
}
private void checkIconAndSaveFile(Long id, TerminalType terminalType,
MultipartFile file, String targetDir) throws ServiceException {
String originalFilename = file.getOriginalFilename();
String tmpName = uploadFilePathService.getTargetTmpFilename(id,
originalFilename);
String uploadedTmpFilePath = saveUploadFile(file, targetDir, tmpName);
log.debug("targetDir:{} uploadedFilePath:{}", targetDir,
uploadedTmpFilePath);
if (saveFileSuccess(uploadedTmpFilePath)) {
// 判断文件分辨率是否与对应的终端类型匹配,如果分辨率不合法,则删除文件,提示分辨率不正确
try {
File imgFile = new File(uploadedTmpFilePath);
FileInputStream fis = new FileInputStream(imgFile);
BufferedImage bufferedImg = ImageIO.read(fis);
int imgWidth = bufferedImg.getWidth();
int imgHeight = bufferedImg.getHeight();
fis.close();
log.debug("imgWidth:{},imgHeight:{}", imgWidth, imgHeight);
if (isInvalidIcon(terminalType, imgWidth, imgHeight)) {
boolean deleteImgFile = imgFile.delete();
log.debug("deleteImgFile {} ok?:{}", uploadedTmpFilePath,
deleteImgFile);
throw new ServiceException(terminalType.name()
+ "图片分辨率不符合规定,上传图片规格必须为"
+ terminalType.getIconWidth() + "*"
+ terminalType.getIconHeight());
} else {
String targetFilename = uploadFilePathService
.getTargetFilename(id, originalFilename);
log.debug(
"targetDir:{} targetFilename:{} uploadedFilePath:{}",
targetDir, targetFilename, uploadedTmpFilePath);
File validTmpFile = new File(uploadedTmpFilePath);
File oldIcon = new File(targetDir + targetFilename);
if (oldIcon.exists()) {
oldIcon.delete();
}
FileUtils.moveFile(validTmpFile, new File(targetDir
+ targetFilename));
}
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
private boolean isInvalidIcon(TerminalType terminalType, int imgWidth,
int imgHeight) {
String checkUploadImage = configService
.getByName(ConfigConstant.CHECK_UPLOAD_IMAGE_SPECIFICATION);
if ("true".equals(checkUploadImage)) {
return imgWidth != terminalType.getIconWidth()
|| imgHeight != terminalType.getIconHeight();
}
return false;
}
private void checkPicAndSaveFile(Long id, TerminalType terminalType,
MultipartFile file, String targetDir) throws ServiceException {
String originalFilename = file.getOriginalFilename();
String tmpName = uploadFilePathService.getTargetTmpFilename(id,
originalFilename);
String uploadedTmpFilePath = saveUploadFile(file, targetDir, tmpName);
log.debug("targetDir:{} uploadedFilePath:{}", targetDir,
uploadedTmpFilePath);
if (saveFileSuccess(uploadedTmpFilePath)) {
// 判断文件分辨率是否与对应的终端类型匹配,如果分辨率不合法,则删除文件,提示分辨率不正确
try {
File imgFile = new File(uploadedTmpFilePath);
FileInputStream fis = new FileInputStream(imgFile);
BufferedImage bufferedImg = ImageIO.read(fis);
int imgWidth = bufferedImg.getWidth();
int imgHeight = bufferedImg.getHeight();
fis.close();
log.debug("imgWidth:{},imgHeight:{}", imgWidth, imgHeight);
if (isInvalidPicture(terminalType, imgWidth, imgHeight)) {
boolean deleteImgFile = imgFile.delete();
log.debug("deleteImgFile {} ok?:{}", uploadedTmpFilePath,
deleteImgFile);
throw new ServiceException(terminalType.name()
+ "图片分辨率不符合规定,上传图片规格必须为"
+ terminalType.getPicWidth() + "*"
+ terminalType.getPicHeight());
} else {
String targetFilename = uploadFilePathService
.getTargetFilename(id, originalFilename);
log.debug(
"targetDir:{} targetFilename:{} uploadedFilePath:{}",
targetDir, targetFilename, uploadedTmpFilePath);
File validTmpFile = new File(uploadedTmpFilePath);
File oldIcon = new File(targetDir + targetFilename);
if (oldIcon.exists()) {
oldIcon.delete();
}
FileUtils.moveFile(validTmpFile, new File(targetDir
+ targetFilename));
waterMarkService.printWaterMark(
targetDir + targetFilename,
PictureUrlHelper.toWaterMarkFileName(targetDir
+ targetFilename, configService));
}
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
private boolean isInvalidPicture(TerminalType terminalType, int imgWidth,
int imgHeight) {
String checkUploadImage = configService
.getByName(ConfigConstant.CHECK_UPLOAD_IMAGE_SPECIFICATION);
if ("true".equals(checkUploadImage)) {
return imgWidth != terminalType.getPicWidth()
|| imgHeight != terminalType.getPicHeight();
}
return false;
}
public void saveStoryImageFile(MultipartFile file, Long id,
TerminalType terminalType) {
if (file == null) {
log.debug("file is null,do nothing");
return;
}
String originalFilename = file.getOriginalFilename();
if (StringUtils.isBlank(originalFilename)) {
log.debug("Filename is null,do nothing");
return;
}
String targetDir = uploadFilePathService
.getStoryTargetDirBy(terminalType);
checkPicAndSaveFile(id, terminalType, file, targetDir);
return;
}
public void savePromotionImageFile(MultipartFile file, Long id,
TerminalType terminalType) {
if (file == null) {
log.debug("file is null,do nothing");
return;
}
String originalFilename = file.getOriginalFilename();
if (StringUtils.isBlank(originalFilename)) {
log.debug("Filename is null,do nothing");
return;
}
String targetDir = uploadFilePathService
.getPromotionTargetDirBy(terminalType);
checkPicAndSaveFile(id, terminalType, file, targetDir);
return;
}
public void saveProductImageFile(MultipartFile file, Long id,
TerminalType terminalType) {
if (file == null) {
log.debug("file is null,do nothing");
return;
}
String originalFilename = file.getOriginalFilename();
if (StringUtils.isBlank(originalFilename)) {
log.debug("Filename is null,do nothing");
return;
}
String targetDir = uploadFilePathService
.getProductTargetDirBy(terminalType);
checkPicAndSaveFile(id, terminalType, file, targetDir);
return;
}
public void saveProductDesImageFile(MultipartFile file, Long id,
TerminalType terminalType) {
if (file == null) {
log.debug("file is null,do nothing");
return;
}
String originalFilename = file.getOriginalFilename();
if (StringUtils.isBlank(originalFilename)) {
log.debug("Filename is null,do nothing");
return;
}
String targetDir = uploadFilePathService
.getProductDesTargetDirBy(terminalType);
checkPicAndSaveFile(id, terminalType, file, targetDir);
return;
}
public void saveStoryMenuImageFile(MultipartFile file, Long id,
TerminalType terminalType) {
if (file == null) {
log.debug("file is null,do nothing");
return;
}
String originalFilename = file.getOriginalFilename();
if (StringUtils.isBlank(originalFilename)) {
log.debug("Filename is null,do nothing");
return;
}
String targetDir = uploadFilePathService
.getStoryMenuTargetDirBy(terminalType);
checkPicAndSaveFile(id, terminalType, file, targetDir);
return;
}
public void saveStoryProcedureImageFile(MultipartFile file, Long id,
TerminalType terminalType) {
if (file == null) {
log.debug("file is null,do nothing");
return;
}
String originalFilename = file.getOriginalFilename();
if (StringUtils.isBlank(originalFilename)) {
log.debug("Filename is null,do nothing");
return;
}
String targetDir = uploadFilePathService
.getStoryProcedureTargetDirBy(terminalType);
checkPicAndSaveFile(id, terminalType, file, targetDir);
return;
}
private boolean saveFileSuccess(String uploadedFilePath) {
return StringUtils.isNotBlank(uploadedFilePath);
}
private String saveUploadFile(MultipartFile file, String targetDir,
String targetFilename) {
if (file == null) {
return null;
}
File dir = new File(targetDir);
if (!dir.exists()) {
log.info("dir {} not exists.create dir now.", dir.getAbsolutePath());
dir.mkdirs();
}
try {
String fileName = file.getOriginalFilename();
log.debug("fileName:{}", fileName);
InputStream is = file.getInputStream();
try {
byte[] fileBytes = IOUtils.toByteArray(is);
FileUtils.writeByteArrayToFile(new File(targetDir
+ targetFilename), fileBytes);
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(is);
}
return targetDir + targetFilename;
} catch (IOException e) {
log.error(e.getMessage(), e);
return null;
}
}
}