|
@@ -67,7 +67,141 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
|
|
|
|
|
|
|
|
|
@Override
|
|
|
+ public Result upload2(MultipartFile file, String type) {
|
|
|
+ log.info("now time: " + new Date());
|
|
|
+
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ // 检查非法文件上传
|
|
|
+ boolean checkFile = FileUtils.checkFile(file);
|
|
|
+ if (!checkFile) {
|
|
|
+ return Result.failure("上传文件格式有误, 请重新上传");
|
|
|
+ }
|
|
|
+
|
|
|
+ String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ // 文件名校验长度
|
|
|
+ String prefix = StringUtils.substringBeforeLast(fileName, ".");
|
|
|
+ if (prefix.length() > 50) {
|
|
|
+ return Result.failure(3003, "文件名称不允许超过50个字节");
|
|
|
+ }
|
|
|
+ String suffix = StringUtils.substringAfterLast(fileName, ".");
|
|
|
+ String newName = time + "." +suffix;
|
|
|
+ String dirType = "fodder/";
|
|
|
+ long size = file.getSize();
|
|
|
+ size = size/1024;
|
|
|
+ log.info("fileSize: " + size);
|
|
|
+
|
|
|
+ String ossUrl = null;
|
|
|
+ String iconPath = "0";
|
|
|
+ String dpi = "0";
|
|
|
+ String ossPath = configConstant.ossBasePath+dirType+newName;
|
|
|
+ String savePath = configConstant.serverBasePath;
|
|
|
+ String ossPreviewIcon = null;
|
|
|
+
|
|
|
+ FodderEntity entity = new FodderEntity();
|
|
|
+ try {
|
|
|
+ if (type.equals("pano")) {
|
|
|
+
|
|
|
+ if ((size/1024) >= 120) {
|
|
|
+ return Result.failure(MsgCode.e3004, "全景图文件不能超过120MB");
|
|
|
+ }
|
|
|
+
|
|
|
+ String sceneCode = RandomUtils.getSceneCode("fd720_");
|
|
|
+ newName = sceneCode+"." + suffix;
|
|
|
+ savePath =savePath + sceneCode + "/" + newName;
|
|
|
+
|
|
|
+
|
|
|
+ log.info("准备写入全景图: " + new Date());
|
|
|
+ Thread.sleep(10000);
|
|
|
+
|
|
|
+ FileUtil.writeFromStream(file.getInputStream(), savePath);
|
|
|
+
|
|
|
+ log.info("pano serverPath: " + savePath);
|
|
|
+ log.info("全景图片写入完成");
|
|
|
+
|
|
|
+ log.info("睡眠10s: " + new Date());
|
|
|
+ Thread.sleep(10000);
|
|
|
+
|
|
|
+ log.info("准备执行压缩图片: " + new Date());
|
|
|
+ // 压缩图片并上传oss
|
|
|
+ iconPath = fileUtils.compressImgAndUploadOss2(
|
|
|
+ savePath, configConstant.ossBasePath + sceneCode , configConstant.ossDomain, 600, 300, "/thumb_"+sceneCode+".jpg");
|
|
|
+
|
|
|
+
|
|
|
+ log.info("压缩图片2: " + new Date());
|
|
|
+ // 全景图的预览图
|
|
|
+ ossPreviewIcon = fileUtils.compressImgAndUploadOss2(
|
|
|
+ savePath, configConstant.ossBasePath + sceneCode , configConstant.ossDomain, 1920, 960, "/preview.jpg");
|
|
|
+
|
|
|
+
|
|
|
+ ossUrl = configConstant.ossDomain+configConstant.ossBasePath + sceneCode;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ entity.setSceneCode(sceneCode);
|
|
|
+ entity.setFilePath(savePath);
|
|
|
+ entity.setStatus(1);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ ossUrl = configConstant.ossDomain+ossPath;
|
|
|
+
|
|
|
+ if (type.equals("image")) {
|
|
|
+ // 图片大于1MB生成缩略图, 小于1MB使用原图作为缩略图
|
|
|
+ if (size > 1024) {
|
|
|
+ savePath = savePath + dirType + newName;
|
|
|
+ FileUtil.writeFromStream(file.getInputStream(), savePath);
|
|
|
+ iconPath = fileUtils.scaleImgAndUploadOss(savePath, configConstant.ossBasePath + dirType , configConstant.ossDomain);
|
|
|
+ } else {
|
|
|
+ iconPath = ossUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ dpi = ImageUtil.dpi(file);
|
|
|
+
|
|
|
+ }
|
|
|
+ // 普通素材直接上传oss, 服务器不保留文件
|
|
|
+ aliyunOssUtil.upload(file.getBytes(), ossPath);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ entity.setType(type);
|
|
|
+ // 用前缀做名称
|
|
|
+ entity.setName(prefix);
|
|
|
+ entity.setFileName(newName);
|
|
|
+ log.info("ossUrl: " + ossUrl);
|
|
|
+ log.info("iconPath:" + iconPath);
|
|
|
+
|
|
|
+ entity.setOssPath(ossUrl);
|
|
|
+ entity.setIcon(iconPath);
|
|
|
+ entity.setUserId("15015980188");
|
|
|
+ entity.setFileSize(size+"");
|
|
|
+ entity.setDpi(dpi);
|
|
|
+ entity.setPreviewIcon(ossPreviewIcon);
|
|
|
+
|
|
|
+// save(entity);
|
|
|
+
|
|
|
+// if (type.equals("pano")) {
|
|
|
+// //发消息到mq
|
|
|
+// rabbitTemplate.convertAndSend(RabbitConfig.PANO_EXCHANGE, RabbitConfig.PANO_QUEUE_ROUTING, entity.getId());
|
|
|
+// log.info("发送消息到队列完成: " + entity.getId());
|
|
|
+// }
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ log.info("上传完成,耗时: {} s" , (end-start)/1000);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error(e.getMessage());
|
|
|
+ throw new BaseRuntimeException(MsgCode.e5003, "不支持此图片");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return Result.success(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
public Result upload(MultipartFile file, String type) {
|
|
|
+
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
// 检查非法文件上传
|
|
|
boolean checkFile = FileUtils.checkFile(file);
|
|
|
if (!checkFile) {
|
|
@@ -173,13 +307,15 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
|
|
|
rabbitTemplate.convertAndSend(RabbitConfig.PANO_EXCHANGE, RabbitConfig.PANO_QUEUE_ROUTING, entity.getId());
|
|
|
log.info("发送消息到队列完成: " + entity.getId());
|
|
|
}
|
|
|
-
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ log.info("上传完成,耗时: {} s" , (end-start)/1000);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
log.error(e.getMessage());
|
|
|
throw new BaseRuntimeException(MsgCode.e5003, "不支持此图片");
|
|
|
}
|
|
|
|
|
|
+
|
|
|
return Result.success(entity);
|
|
|
}
|
|
|
|
|
@@ -240,4 +376,5 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
}
|