stjdydayou 4 jaren geleden
bovenliggende
commit
1600544e8b

+ 9 - 0
src/main/java/com/zhiqiyun/open/core/service/UploadFileInfoService.java

@@ -1,7 +1,16 @@
 package com.zhiqiyun.open.core.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.zhiqiyun.open.core.models.UploadResult;
 import com.zhiqiyun.open.core.models.statistics.UploadFileInfo;
 
 public interface UploadFileInfoService extends IService<UploadFileInfo> {
+	/**
+	 * 处理上传图片
+	 *
+	 * @param originalFilename
+	 * @param fileBuff
+	 * @return
+	 */
+	UploadResult handle(String originalFilename, byte[] fileBuff);
 }

+ 107 - 0
src/main/java/com/zhiqiyun/open/core/service/impl/UploadFileInfoServiceImpl.java

@@ -2,10 +2,117 @@ package com.zhiqiyun.open.core.service.impl;
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.zhiqiyun.open.core.mapper.statistics.UploadFileInfoMapper;
+import com.zhiqiyun.open.core.models.UploadResult;
 import com.zhiqiyun.open.core.models.statistics.UploadFileInfo;
+import com.zhiqiyun.open.core.service.SequenceService;
 import com.zhiqiyun.open.core.service.UploadFileInfoService;
+import com.zhiqiyun.open.exception.UploadException;
+import com.zhiqiyun.open.utils.DateUtil;
+import lombok.extern.slf4j.Slf4j;
+import net.coobird.thumbnailator.Thumbnails;
+import net.sf.jmimemagic.Magic;
+import net.sf.jmimemagic.MagicMatch;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+@Slf4j
 @Service
 public class UploadFileInfoServiceImpl extends ServiceImpl<UploadFileInfoMapper, UploadFileInfo> implements UploadFileInfoService {
+	@Value("${uploader.max-size}")
+	private long maxSize;
+
+	@Value("${uploader.allow-file-types}")
+	private String[] allowExtensions;
+
+	@Value("${uploader.save-path}")
+	private String savePath;
+
+	@Value("${uploader.file-host}")
+	private String fileHost;
+
+	@Autowired
+	private SequenceService sequenceService;
+
+	@Override
+	public UploadResult handle(String originalFilename, byte[] fileBuff) {
+		UploadResult result = new UploadResult();
+		result.setSuccess(false);
+		result.setOriginalFileName(originalFilename);
+		try {
+			// 验证文件件大小
+			if (fileBuff.length > maxSize * 1024) {
+				throw new UploadException("上传文件大小不能超过" + (maxSize / 1024) + "M");
+			}
+
+			MagicMatch magicMatch = Magic.getMagicMatch(fileBuff);
+
+			String extension = magicMatch.getExtension();
+			String mimeType = magicMatch.getMimeType();
+
+			// 验证文件类型
+			if (extension == null) {
+				throw new UploadException("无的文件扩展名,请重新上传");
+			}
+
+			extension = extension.toLowerCase();
+			List<String> listAllowExtensions = Arrays.asList(allowExtensions);
+			if (!listAllowExtensions.contains(extension)) {
+				throw new UploadException(String.format("只能上传%s类型的文件", String.join(",", allowExtensions)));
+			}
+
+			String filePath = String.format("%s/%s", savePath, DateUtil.format("yyyy/MM/dd"));
+			File fileDir = new File(filePath);
+			if (!fileDir.exists()) {
+				boolean mkdirResult = fileDir.mkdirs();
+				if (!mkdirResult) {
+					log.warn("mkdir save file path error {}", filePath);
+					throw new UploadException("创建上传目录失败");
+				}
+			}
+			filePath = String.format("%s/%s.%s", filePath, DigestUtils.md5Hex(fileBuff), extension);
+			File file = new File(filePath);
+
+
+			try {
+				ByteArrayInputStream in = new ByteArrayInputStream(fileBuff);
+				BufferedImage bi = ImageIO.read(in);
+				Thumbnails.of(bi).size(1200, 1200).toFile(file);
+			} catch (IOException e) {
+				log.warn("upload is not image file", e);
+				throw new UploadException("只能上传图片文件");
+			}
+
+			result.setSuccess(true);
+			result.setMessage("上传文件成功");
+			result.setMimeType(mimeType);
+			// 获取服务地址和端口
+			String url = filePath.replaceFirst(this.savePath, "");
+			result.setSrc(fileHost + url);
+			result.setSize(fileBuff.length);
+			result.setExtension(extension);
+			result.setId(this.sequenceService.nextId());
+
+			UploadFileInfo fileInfo = new UploadFileInfo();
+			BeanUtils.copyProperties(result, fileInfo);
+			fileInfo.setCreatedTime(DateUtil.current());
+			this.save(fileInfo);
+		} catch (UploadException e) {
+			result.setMessage(e.getMessage());
+		} catch (Exception e) {
+			log.error("upload file error", e);
+			result.setMessage("服务异常,请稍后再试");
+		}
+		return result;
+	}
 }

+ 1 - 87
src/main/java/com/zhiqiyun/open/mvc/controller/UploaderController.java

@@ -42,25 +42,9 @@ import java.util.List;
 @RequestMapping("/uploader")
 public class UploaderController {
 
-    @Value("${uploader.max-size}")
-    private long maxSize;
-
-//	@Value("${uploader.file-host}")
-//	private String fileHost;
-
-    @Value("${uploader.allow-file-types}")
-    private String[] allowExtensions;
-
-    @Value("${uploader.save-path}")
-    private String savePath;
-
     @Autowired
     private UploadFileInfoService uploadFileInfoService;
 
-    @Autowired
-    private SequenceService sequenceService;
-
-
     @Permission(value = "upload.file.find", tags = "查询多媒体信息")
     @PostMapping("/findPage")
     public Result findPage(@RequestBody QueryUploadFileInfoParams params) {
@@ -104,7 +88,7 @@ public class UploaderController {
             originalFilename = String.format("%s.%s", DigestUtils.md5Hex(fileBuff), "png");
         }
 
-        UploadResult uploadResult = this.handle(originalFilename, fileBuff);
+        UploadResult uploadResult = this.uploadFileInfoService.handle(originalFilename, fileBuff);
         if (uploadResult.isSuccess()) {
             return Result.instance(Result.Code.SUCCESS).setData(uploadResult);
         } else {
@@ -112,74 +96,4 @@ public class UploaderController {
         }
     }
 
-    public UploadResult handle(String originalFilename, byte[] fileBuff) {
-        UploadResult result = new UploadResult();
-        result.setSuccess(false);
-        result.setOriginalFileName(originalFilename);
-        try {
-            // 验证文件件大小
-            if (fileBuff.length > maxSize * 1024) {
-                throw new UploadException("上传文件大小不能超过" + (maxSize / 1024) + "M");
-            }
-
-            MagicMatch magicMatch = Magic.getMagicMatch(fileBuff);
-
-            String extension = magicMatch.getExtension();
-            String mimeType = magicMatch.getMimeType();
-
-            // 验证文件类型
-            if (extension == null) {
-                throw new UploadException("无的文件扩展名,请重新上传");
-            }
-
-            extension = extension.toLowerCase();
-            List<String> listAllowExtensions = Arrays.asList(allowExtensions);
-            if (!listAllowExtensions.contains(extension)) {
-                throw new UploadException(String.format("只能上传%s类型的文件", String.join(",", allowExtensions)));
-            }
-
-            String filePath = String.format("%s/%s", savePath, DateUtil.format("yyyy/MM/dd"));
-            File fileDir = new File(filePath);
-            if (!fileDir.exists()) {
-                boolean mkdirResult = fileDir.mkdirs();
-                if (!mkdirResult) {
-                    log.warn("mkdir save file path error {}", filePath);
-                    throw new UploadException("创建上传目录失败");
-                }
-            }
-            filePath = String.format("%s/%s.%s", filePath, DigestUtils.md5Hex(fileBuff), extension);
-            File file = new File(filePath);
-
-
-            try {
-                ByteArrayInputStream in = new ByteArrayInputStream(fileBuff);
-                BufferedImage bi = ImageIO.read(in);
-                Thumbnails.of(bi).size(1200, 1200).toFile(file);
-            } catch (IOException e) {
-                log.warn("upload is not image file", e);
-                throw new UploadException("只能上传图片文件");
-            }
-
-            result.setSuccess(true);
-            result.setMessage("上传文件成功");
-            result.setMimeType(mimeType);
-            // 获取服务地址和端口
-            String url = filePath.replaceFirst(this.savePath, "");
-            result.setSrc(url);
-            result.setSize(fileBuff.length);
-            result.setExtension(extension);
-            result.setId(this.sequenceService.nextId());
-
-            UploadFileInfo fileInfo = new UploadFileInfo();
-            BeanUtils.copyProperties(result, fileInfo);
-            fileInfo.setCreatedTime(DateUtil.current());
-            this.uploadFileInfoService.save(fileInfo);
-        } catch (UploadException e) {
-            result.setMessage(e.getMessage());
-        } catch (Exception e) {
-            log.error("upload file error", e);
-            result.setMessage("服务异常,请稍后再试");
-        }
-        return result;
-    }
 }

+ 45 - 0
src/main/java/com/zhiqiyun/open/router/apis/UploadApi.java

@@ -0,0 +1,45 @@
+package com.zhiqiyun.open.router.apis;
+
+import com.dliyun.oap.framework.annotation.ServiceMethod;
+import com.dliyun.oap.framework.annotation.ServiceMethodBean;
+import com.dliyun.oap.framework.response.OapResponse;
+import com.zhiqiyun.open.core.models.UploadResult;
+import com.zhiqiyun.open.core.service.UploadFileInfoService;
+import com.zhiqiyun.open.mvc.Result;
+import com.zhiqiyun.open.mvc.params.UploaderBase64Param;
+import com.zhiqiyun.open.router.request.UploaderBase64Request;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+
+import java.io.IOException;
+
+@Slf4j
+@ServiceMethodBean
+public class UploadApi {
+	@Autowired
+	private UploadFileInfoService uploadFileInfoService;
+
+	@ServiceMethod(method = "uploader.handle.base64File", title = "文件上传")
+	public OapResponse handleBase64File(@RequestBody UploaderBase64Request request) throws IOException {
+
+		byte[] fileBuff = Base64.decodeBase64(request.getBase64File());
+
+		String originalFilename = request.getFilename();
+		if (StringUtils.isBlank(originalFilename)) {
+			originalFilename = String.format("%s.%s", DigestUtils.md5Hex(fileBuff), "png");
+		}
+
+		UploadResult uploadResult = this.uploadFileInfoService.handle(originalFilename, fileBuff);
+
+		if (uploadResult.isSuccess()) {
+			return OapResponse.success().setBody(uploadResult);
+		} else {
+			return OapResponse.fail("UPLOAD_FAIL", uploadResult.getMessage());
+		}
+	}
+}

+ 36 - 0
src/main/java/com/zhiqiyun/open/router/request/UploaderBase64Request.java

@@ -0,0 +1,36 @@
+/**
+ * 版权声明:中图一购网络科技有限公司 版权所有 违者必究 2012
+ * 日    期:12-7-14
+ */
+package com.zhiqiyun.open.router.request;
+
+import com.dliyun.oap.framework.annotation.ServiceParamField;
+import com.dliyun.oap.framework.request.AbstractOapRequest;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+import javax.validation.constraints.NotBlank;
+
+@Data
+@EqualsAndHashCode(callSuper = true)
+public class UploaderBase64Request extends AbstractOapRequest {
+
+	@ServiceParamField(describe = "文件原始文件名")
+	@NotBlank
+	private String filename;
+
+	@ServiceParamField(describe = "Base64文件")
+	@NotBlank
+	private String base64File;
+
+	public String getBase64File() {
+		if (this.base64File != null) {
+			String[] array = this.base64File.split("base64,");
+			if (array.length > 1) {
+				return array[1];
+			}
+		}
+		return base64File;
+	}
+}
+

+ 0 - 31
src/main/java/com/zhiqiyun/open/router/request/bz/HolidayVillageInfoRequest.java

@@ -1,31 +0,0 @@
-package com.zhiqiyun.open.router.request.bz;
-
-import com.dliyun.oap.framework.annotation.ServiceParamField;
-import com.dliyun.oap.framework.request.AbstractOapRequest;
-import lombok.Data;
-import lombok.EqualsAndHashCode;
-
-@Data
-@EqualsAndHashCode(callSuper = true)
-public class HolidayVillageInfoRequest extends AbstractOapRequest {
-    @ServiceParamField(describe = "当前页")
-    private Integer current;
-    @ServiceParamField(describe = "每页条数")
-    private Integer size;
-    @ServiceParamField(describe = "关键词搜索")
-    private String keyword;
-
-    public Integer getCurrent() {
-        if (this.current == null || this.current < 1) {
-            return 1;
-        }
-        return this.current;
-    }
-
-    public Integer getSize() {
-        if (this.size == null || this.size < 1) {
-            return 1;
-        }
-        return this.size;
-    }
-}

+ 0 - 31
src/main/java/com/zhiqiyun/open/router/request/bz/HomeStayRequest.java

@@ -1,31 +0,0 @@
-package com.zhiqiyun.open.router.request.bz;
-
-import com.dliyun.oap.framework.annotation.ServiceParamField;
-import com.dliyun.oap.framework.request.AbstractOapRequest;
-import lombok.Data;
-import lombok.EqualsAndHashCode;
-
-@Data
-@EqualsAndHashCode(callSuper = true)
-public class HomeStayRequest extends AbstractOapRequest {
-    @ServiceParamField(describe = "当前页")
-    private Integer current;
-    @ServiceParamField(describe = "每页条数")
-    private Integer size;
-    @ServiceParamField(describe = "关键词搜索")
-    private String keyword;
-
-    public Integer getCurrent() {
-        if (this.current == null || this.current < 1) {
-            return 1;
-        }
-        return this.current;
-    }
-
-    public Integer getSize() {
-        if (this.size == null || this.size < 1) {
-            return 1;
-        }
-        return this.size;
-    }
-}

+ 0 - 31
src/main/java/com/zhiqiyun/open/router/request/bz/HotelInfoRequest.java

@@ -1,31 +0,0 @@
-package com.zhiqiyun.open.router.request.bz;
-
-import com.dliyun.oap.framework.annotation.ServiceParamField;
-import com.dliyun.oap.framework.request.AbstractOapRequest;
-import lombok.Data;
-import lombok.EqualsAndHashCode;
-
-@Data
-@EqualsAndHashCode(callSuper = true)
-public class HotelInfoRequest extends AbstractOapRequest {
-    @ServiceParamField(describe = "当前页")
-    private Integer current;
-    @ServiceParamField(describe = "每页条数")
-    private Integer size;
-    @ServiceParamField(describe = "关键词搜索")
-    private String keyword;
-
-    public Integer getCurrent() {
-        if (this.current == null || this.current < 1) {
-            return 1;
-        }
-        return this.current;
-    }
-
-    public Integer getSize() {
-        if (this.size == null || this.size < 1) {
-            return 1;
-        }
-        return this.size;
-    }
-}

+ 0 - 31
src/main/java/com/zhiqiyun/open/router/request/bz/SceneryInfoRequest.java

@@ -1,31 +0,0 @@
-package com.zhiqiyun.open.router.request.bz;
-
-import com.dliyun.oap.framework.annotation.ServiceParamField;
-import com.dliyun.oap.framework.request.AbstractOapRequest;
-import lombok.Data;
-import lombok.EqualsAndHashCode;
-
-@Data
-@EqualsAndHashCode(callSuper = true)
-public class SceneryInfoRequest extends AbstractOapRequest {
-    @ServiceParamField(describe = "当前页")
-    private Integer current;
-    @ServiceParamField(describe = "每页条数")
-    private Integer size;
-    @ServiceParamField(describe = "关键词搜索")
-    private String keyword;
-
-    public Integer getCurrent() {
-        if (this.current == null || this.current < 1) {
-            return 1;
-        }
-        return this.current;
-    }
-
-    public Integer getSize() {
-        if (this.size == null || this.size < 1) {
-            return 1;
-        }
-        return this.size;
-    }
-}

+ 0 - 31
src/main/java/com/zhiqiyun/open/router/request/bz/TouristRequest.java

@@ -1,31 +0,0 @@
-package com.zhiqiyun.open.router.request.bz;
-
-import com.dliyun.oap.framework.annotation.ServiceParamField;
-import com.dliyun.oap.framework.request.AbstractOapRequest;
-import lombok.Data;
-import lombok.EqualsAndHashCode;
-
-@Data
-@EqualsAndHashCode(callSuper = true)
-public class TouristRequest extends AbstractOapRequest {
-    @ServiceParamField(describe = "当前页")
-    private Integer current;
-    @ServiceParamField(describe = "每页条数")
-    private Integer size;
-    @ServiceParamField(describe = "关键词搜索")
-    private String keyword;
-
-    public Integer getCurrent() {
-        if (this.current == null || this.current < 1) {
-            return 1;
-        }
-        return this.current;
-    }
-
-    public Integer getSize() {
-        if (this.size == null || this.size < 1) {
-            return 1;
-        }
-        return this.size;
-    }
-}

+ 0 - 31
src/main/java/com/zhiqiyun/open/router/request/bz/VenueInfoRequest.java

@@ -1,31 +0,0 @@
-package com.zhiqiyun.open.router.request.bz;
-
-import com.dliyun.oap.framework.annotation.ServiceParamField;
-import com.dliyun.oap.framework.request.AbstractOapRequest;
-import lombok.Data;
-import lombok.EqualsAndHashCode;
-
-@Data
-@EqualsAndHashCode(callSuper = true)
-public class VenueInfoRequest extends AbstractOapRequest {
-    @ServiceParamField(describe = "当前页")
-    private Integer current;
-    @ServiceParamField(describe = "每页条数")
-    private Integer size;
-    @ServiceParamField(describe = "关键词搜索")
-    private String keyword;
-
-    public Integer getCurrent() {
-        if (this.current == null || this.current < 1) {
-            return 1;
-        }
-        return this.current;
-    }
-
-    public Integer getSize() {
-        if (this.size == null || this.size < 1) {
-            return 1;
-        }
-        return this.size;
-    }
-}

+ 4 - 4
src/main/resources/application.properties

@@ -6,9 +6,9 @@ logger.root.level=DEBUG
 logger.root.path=/tmp/
 ####################### mysql ###############################
 spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
-spring.datasource.url=jdbc:mysql://rm-bp1e2451m5olnc4g6qo.mysql.rds.aliyuncs.com/liucheng_open?allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&useSSL=false
-spring.datasource.username=liucheng
-spring.datasource.password=liucheng123@
+spring.datasource.url=jdbc:mysql://47.106.38.249:3336/liucheng_open?allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&useSSL=false
+spring.datasource.username=app
+spring.datasource.password=!@qwaszx
 #######################mybatis###############################
 mybatis-plus.config-location=classpath:mybatis/sqlMapConfig.xml
 mybatis-plus.mapper-locations=classpath:mybatis/mappers/*.xml
@@ -37,7 +37,7 @@ spring.oap.security.service-access-controller-class=com.zhiqiyun.open.router.con
 #spring.oap.service.timeout-seconds=60
 ####################### uploader config ###############################
 uploader.max-size=20480000
-uploader.file-host=ss.zlanli.com
+uploader.file-host=http://127.0.0.1:9800/src
 uploader.allow-file-types=jpg,jpeg,png,gif
 uploader.save-path=/tmp/uploads
 

+ 2 - 2
src/test/java/com/zhiqiyun/SimplePageProcessor.java

@@ -12,7 +12,7 @@ import java.util.List;
 
 @Slf4j
 public class SimplePageProcessor implements PageProcessor {
-    private final Site site = Site.me().setDomain("www.188420.com").setRetryTimes(3).setSleepTime(1000);
+    private final Site site = Site.me().setDomain("news.qq.com").setRetryTimes(3).setSleepTime(1000);
 
     private static final List<String> LIST_SPIDER_URLS = new ArrayList<>();
 
@@ -39,6 +39,6 @@ public class SimplePageProcessor implements PageProcessor {
     }
 
     public static void main(String[] args) {
-        Spider.create(new SimplePageProcessor()).addUrl("https://www.188420.com").run();
+        Spider.create(new SimplePageProcessor()).addUrl("https://news.qq.com/").run();
     }
 }