|
@@ -61,6 +61,9 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
|
|
|
@Autowired
|
|
|
AliyunOssUtil aliyunOssUtil;
|
|
|
|
|
|
+ // 用户最大空间3G
|
|
|
+ static int MAX_SIZE = 1024 * 1024 * 3;
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public IBaseMapper<FodderEntity, Long> getBaseMapper() {
|
|
@@ -72,6 +75,10 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
|
|
|
public Result upload(MultipartFile file, String type, String tempId) {
|
|
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
+ String phone = getUserNameForToken();
|
|
|
+
|
|
|
+ // 检查用户空间
|
|
|
+ checkUserSize(phone);
|
|
|
// 检查非法文件上传
|
|
|
boolean checkFile = FileUtils.checkFile(file);
|
|
|
if (!checkFile) {
|
|
@@ -107,7 +114,7 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
|
|
|
return Result.failure(MsgCode.e3004, "全景图文件不能超过120MB");
|
|
|
}
|
|
|
|
|
|
- // 次方法可以读取到图片内部结构, 可以检验非全景图
|
|
|
+ // 此方法可以读取到图片内部结构, 可以检验非全景图
|
|
|
String imgType = FileTypeUtil.getType(file.getInputStream());
|
|
|
// 全景图只支持jpg图片格式
|
|
|
if (!imgType.equalsIgnoreCase("jpg")) {
|
|
@@ -168,7 +175,7 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
|
|
|
|
|
|
entity.setOssPath(ossUrl);
|
|
|
entity.setIcon(iconPath);
|
|
|
- entity.setUserId(getUserNameForToken());
|
|
|
+ entity.setUserId(phone);
|
|
|
entity.setFileSize(size+"");
|
|
|
entity.setDpi(dpi);
|
|
|
entity.setPreviewIcon(ossPreviewIcon);
|
|
@@ -240,135 +247,7 @@ 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);
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
|
@@ -380,12 +259,6 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
|
|
|
}
|
|
|
|
|
|
|
|
|
- @Test
|
|
|
- public void test(){
|
|
|
- BufferedImage read = ImgUtil.read("E:\\cache\\aa.jpg");
|
|
|
- System.out.println(read.getWidth());
|
|
|
- System.out.println(read.getHeight());
|
|
|
- }
|
|
|
|
|
|
|
|
|
@Override
|
|
@@ -421,8 +294,16 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 2022-05-31 by owen
|
|
|
+ * 检查用户空间, 最大3G
|
|
|
+ * @param phone
|
|
|
+ */
|
|
|
+ private void checkUserSize(String phone){
|
|
|
+ Integer size = entityMapper.countSize(phone);
|
|
|
+ log.info("当前用户空间: {} M", (size / 1024));
|
|
|
+ BaseRuntimeException.isTrue(size >= MAX_SIZE, null, "该用户空间已满");
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|