From 140a77330c561fe29bdd0651637b73146c632124 Mon Sep 17 00:00:00 2001 From: wayn <1669738430@qq.com> Date: Sun, 4 Apr 2021 18:41:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=A7=BB=E5=8A=A8=E7=AB=AF):=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/system/DictTypeController.java | 2 +- .../controller/system/ProfileController.java | 4 +- .../api/controller/system/RoleController.java | 2 +- .../api/controller/system/UserController.java | 2 +- .../admin/framework/config/MvcConfig.java | 1 + .../admin/framework/config/SwaggerConfig.java | 1 + .../base/controller}/CommonController.java | 30 +++++- .../common/base}/service/UploadService.java | 2 +- .../base}/service/impl/UploadServiceImpl.java | 7 +- .../com/wayn/common}/config/WaynConfig.java | 2 +- .../wayn/common/util/file/FileUploadUtil.java | 50 +++++++++- .../mobile/api/controller/UserController.java | 21 +++++ .../api/service/impl/OrderServiceImpl.java | 2 +- .../mobile/framework/config/MvcConfig.java | 1 + .../framework/config/SwaggerConfig.java | 1 + .../mobile/framework/config/WaynConfig.java | 91 ------------------- .../manager/upload/service/UploadService.java | 29 ------ .../service/impl/UploadServiceImpl.java | 89 ------------------ 18 files changed, 112 insertions(+), 225 deletions(-) rename {waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system => waynboot-common/src/main/java/com/wayn/common/base/controller}/CommonController.java (77%) rename {waynboot-admin-api/src/main/java/com/wayn/admin/framework/manager/upload => waynboot-common/src/main/java/com/wayn/common/base}/service/UploadService.java (91%) rename {waynboot-admin-api/src/main/java/com/wayn/admin/framework/manager/upload => waynboot-common/src/main/java/com/wayn/common/base}/service/impl/UploadServiceImpl.java (94%) rename {waynboot-admin-api/src/main/java/com/wayn/admin/framework => waynboot-common/src/main/java/com/wayn/common}/config/WaynConfig.java (97%) delete mode 100644 waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/WaynConfig.java delete mode 100644 waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/upload/service/UploadService.java delete mode 100644 waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/upload/service/impl/UploadServiceImpl.java diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/DictTypeController.java b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/DictTypeController.java index 867880d..172c551 100644 --- a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/DictTypeController.java +++ b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/DictTypeController.java @@ -1,8 +1,8 @@ package com.wayn.admin.api.controller.system; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import com.wayn.admin.framework.config.WaynConfig; import com.wayn.common.base.controller.BaseController; +import com.wayn.common.config.WaynConfig; import com.wayn.common.constant.SysConstants; import com.wayn.common.core.domain.system.Dict; import com.wayn.common.core.service.system.IDictService; diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/ProfileController.java b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/ProfileController.java index 5e8411c..9708b69 100644 --- a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/ProfileController.java +++ b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/ProfileController.java @@ -1,8 +1,8 @@ package com.wayn.admin.api.controller.system; -import com.wayn.admin.framework.config.WaynConfig; -import com.wayn.admin.framework.manager.upload.service.UploadService; import com.wayn.admin.framework.security.service.TokenService; +import com.wayn.common.base.service.UploadService; +import com.wayn.common.config.WaynConfig; import com.wayn.common.core.domain.system.User; import com.wayn.common.core.model.LoginUserDetail; import com.wayn.common.core.service.system.IUserService; diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/RoleController.java b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/RoleController.java index bfc6f1e..6e9398d 100644 --- a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/RoleController.java +++ b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/RoleController.java @@ -1,8 +1,8 @@ package com.wayn.admin.api.controller.system; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import com.wayn.admin.framework.config.WaynConfig; import com.wayn.common.base.controller.BaseController; +import com.wayn.common.config.WaynConfig; import com.wayn.common.constant.SysConstants; import com.wayn.common.core.domain.system.Role; import com.wayn.common.core.service.system.IRoleService; diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/UserController.java b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/UserController.java index ccda66f..fd6a6b9 100644 --- a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/UserController.java +++ b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/UserController.java @@ -3,8 +3,8 @@ package com.wayn.admin.api.controller.system; import cn.afterturn.easypoi.excel.entity.ImportParams; import cn.afterturn.easypoi.excel.imports.ExcelImportService; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import com.wayn.admin.framework.config.WaynConfig; import com.wayn.common.base.controller.BaseController; +import com.wayn.common.config.WaynConfig; import com.wayn.common.constant.SysConstants; import com.wayn.common.core.domain.system.User; import com.wayn.common.core.service.system.IRoleService; diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/framework/config/MvcConfig.java b/waynboot-admin-api/src/main/java/com/wayn/admin/framework/config/MvcConfig.java index 4215ead..141aaa5 100644 --- a/waynboot-admin-api/src/main/java/com/wayn/admin/framework/config/MvcConfig.java +++ b/waynboot-admin-api/src/main/java/com/wayn/admin/framework/config/MvcConfig.java @@ -1,5 +1,6 @@ package com.wayn.admin.framework.config; +import com.wayn.common.config.WaynConfig; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/framework/config/SwaggerConfig.java b/waynboot-admin-api/src/main/java/com/wayn/admin/framework/config/SwaggerConfig.java index dce4b61..a5d4776 100644 --- a/waynboot-admin-api/src/main/java/com/wayn/admin/framework/config/SwaggerConfig.java +++ b/waynboot-admin-api/src/main/java/com/wayn/admin/framework/config/SwaggerConfig.java @@ -1,5 +1,6 @@ package com.wayn.admin.framework.config; +import com.wayn.common.config.WaynConfig; import io.swagger.annotations.ApiOperation; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/CommonController.java b/waynboot-common/src/main/java/com/wayn/common/base/controller/CommonController.java similarity index 77% rename from waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/CommonController.java rename to waynboot-common/src/main/java/com/wayn/common/base/controller/CommonController.java index f40f431..e1effb4 100644 --- a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/system/CommonController.java +++ b/waynboot-common/src/main/java/com/wayn/common/base/controller/CommonController.java @@ -1,7 +1,7 @@ -package com.wayn.admin.api.controller.system; +package com.wayn.common.base.controller; -import com.wayn.admin.framework.config.WaynConfig; -import com.wayn.admin.framework.manager.upload.service.UploadService; +import com.wayn.common.base.service.UploadService; +import com.wayn.common.config.WaynConfig; import com.wayn.common.exception.BusinessException; import com.wayn.common.util.R; import com.wayn.common.util.file.FileUploadUtil; @@ -18,6 +18,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; +import java.util.Base64; /** * 通用请求处理类 @@ -101,4 +102,27 @@ public class CommonController { return R.error(e.getMessage()); } } + + /** + * 通用上传请求 + */ + @PostMapping("base64uploadFile") + @ResponseBody + public R base64uploadFile(String filename, + String base64content, + HttpServletRequest request) { + try { + byte[] decode = Base64.getDecoder().decode(base64content.substring(base64content.indexOf(IMAGE_BASE64_FLAG) + IMAGE_BASE64_FLAG.length())); + // 上传文件路径 + String filePath = WaynConfig.getUploadDir(); + String fileName = FileUploadUtil.uploadFile(decode, filename, filePath); + String fileUrl = uploadService.uploadFile(fileName); + return R.success().add("url", fileUrl).add("fileName", fileName); + } catch (Exception e) { + log.error(e.getMessage(), e); + return R.error(e.getMessage()); + } + } + + public static final String IMAGE_BASE64_FLAG = ";base64,"; } diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/framework/manager/upload/service/UploadService.java b/waynboot-common/src/main/java/com/wayn/common/base/service/UploadService.java similarity index 91% rename from waynboot-admin-api/src/main/java/com/wayn/admin/framework/manager/upload/service/UploadService.java rename to waynboot-common/src/main/java/com/wayn/common/base/service/UploadService.java index d171e2e..a9bc128 100644 --- a/waynboot-admin-api/src/main/java/com/wayn/admin/framework/manager/upload/service/UploadService.java +++ b/waynboot-common/src/main/java/com/wayn/common/base/service/UploadService.java @@ -1,4 +1,4 @@ -package com.wayn.admin.framework.manager.upload.service; +package com.wayn.common.base.service; import com.qiniu.common.QiniuException; diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/framework/manager/upload/service/impl/UploadServiceImpl.java b/waynboot-common/src/main/java/com/wayn/common/base/service/impl/UploadServiceImpl.java similarity index 94% rename from waynboot-admin-api/src/main/java/com/wayn/admin/framework/manager/upload/service/impl/UploadServiceImpl.java rename to waynboot-common/src/main/java/com/wayn/common/base/service/impl/UploadServiceImpl.java index d999614..f9605a0 100644 --- a/waynboot-admin-api/src/main/java/com/wayn/admin/framework/manager/upload/service/impl/UploadServiceImpl.java +++ b/waynboot-common/src/main/java/com/wayn/common/base/service/impl/UploadServiceImpl.java @@ -1,4 +1,4 @@ -package com.wayn.admin.framework.manager.upload.service.impl; +package com.wayn.common.base.service.impl; import com.alibaba.fastjson.JSONObject; import com.qiniu.common.QiniuException; @@ -6,8 +6,8 @@ import com.qiniu.http.Response; import com.qiniu.storage.Configuration; import com.qiniu.storage.UploadManager; import com.qiniu.util.Auth; -import com.wayn.admin.framework.config.WaynConfig; -import com.wayn.admin.framework.manager.upload.service.UploadService; +import com.wayn.common.base.service.UploadService; +import com.wayn.common.config.WaynConfig; import com.wayn.common.core.domain.tool.QiniuConfig; import com.wayn.common.core.service.tool.IQiniuConfigService; import com.wayn.common.exception.BusinessException; @@ -86,5 +86,4 @@ public class UploadServiceImpl implements UploadService { return requestUrl + "/upload/" + fileName; } } - } diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/framework/config/WaynConfig.java b/waynboot-common/src/main/java/com/wayn/common/config/WaynConfig.java similarity index 97% rename from waynboot-admin-api/src/main/java/com/wayn/admin/framework/config/WaynConfig.java rename to waynboot-common/src/main/java/com/wayn/common/config/WaynConfig.java index 6fd974e..6d9c904 100644 --- a/waynboot-admin-api/src/main/java/com/wayn/admin/framework/config/WaynConfig.java +++ b/waynboot-common/src/main/java/com/wayn/common/config/WaynConfig.java @@ -1,4 +1,4 @@ -package com.wayn.admin.framework.config; +package com.wayn.common.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; diff --git a/waynboot-common/src/main/java/com/wayn/common/util/file/FileUploadUtil.java b/waynboot-common/src/main/java/com/wayn/common/util/file/FileUploadUtil.java index 63fe7fa..86c1726 100644 --- a/waynboot-common/src/main/java/com/wayn/common/util/file/FileUploadUtil.java +++ b/waynboot-common/src/main/java/com/wayn/common/util/file/FileUploadUtil.java @@ -3,10 +3,12 @@ package com.wayn.common.util.file; import com.wayn.common.exception.BusinessException; import com.wayn.common.util.date.DateUtils; import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.web.multipart.MultipartFile; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.util.Objects; @@ -15,6 +17,14 @@ import java.util.Objects; */ public class FileUploadUtil { + /** + * 上传文件 + * + * @param file spring MultipartFile文件对象 + * @param filePath 要保存的文件目录 + * @return 新文件名称 + * @throws IOException 上传异常 + */ public static String uploadFile(MultipartFile file, String filePath) throws IOException { int fileNameLength = Objects.requireNonNull(file.getOriginalFilename()).length(); if (fileNameLength > 100) { @@ -26,7 +36,7 @@ public class FileUploadUtil { extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType())); } String encodingFilename = FileUtils.encodingFilename(fileName); - fileName = DateUtils.datePath() + "/" + encodingFilename + "." + extension; + fileName = genNewFilename(encodingFilename, extension); File desc = new File(filePath, fileName); if (!desc.getParentFile().exists()) { desc.getParentFile().mkdirs(); @@ -37,4 +47,42 @@ public class FileUploadUtil { file.transferTo(desc); return fileName; } + + /** + * @param fileBytes 文件base64编码后内容 + * @param fileName 文件名称 + * @param filePath 要保存的文件按目录 + * @return 新文件名称 + * @throws IOException 上传异常 + */ + public static String uploadFile(byte[] fileBytes, String fileName, String filePath) throws IOException { + int fileNameLength = Objects.requireNonNull(fileName).length(); + if (fileNameLength > 100) { + throw new BusinessException("文件名称过长"); + } + String encodingFilename = FileUtils.encodingFilename(fileName); + String extension = FilenameUtils.getExtension(fileName); + fileName = genNewFilename(encodingFilename, extension); + File desc = new File(filePath, fileName); + if (!desc.getParentFile().exists()) { + desc.getParentFile().mkdirs(); + } + if (!desc.exists()) { + desc.createNewFile(); + } + IOUtils.write(fileBytes, new FileOutputStream(desc)); + return fileName; + } + + /** + * 生成新的文件名称 + * + * @param encodingFilename 编码后文件名 + * @param extension 扩展名称 + * @return 新的文件名称 + */ + private static String genNewFilename(String encodingFilename, String extension) { + return DateUtils.datePath() + "/" + encodingFilename + "." + extension; + } + } diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/UserController.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/UserController.java index 5b9ce18..f308c97 100644 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/UserController.java +++ b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/UserController.java @@ -1,11 +1,15 @@ package com.wayn.mobile.api.controller; +import com.wayn.common.core.domain.shop.Member; +import com.wayn.common.core.service.shop.IMemberService; +import com.wayn.common.exception.BusinessException; import com.wayn.common.util.R; import com.wayn.common.util.ServletUtils; import com.wayn.mobile.framework.security.LoginUserDetail; import com.wayn.mobile.framework.security.service.TokenService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -16,9 +20,26 @@ public class UserController { @Autowired private TokenService tokenService; + @Autowired + private IMemberService iMemberService; + @GetMapping("info") public R getInfo() { LoginUserDetail loginUser = tokenService.getLoginUser(ServletUtils.getRequest()); return R.success().add("info", loginUser.getMember()); } + + @PostMapping("uploadAvatar") + public R uploadAvatar(String avatar) { + LoginUserDetail loginUser = tokenService.getLoginUser(ServletUtils.getRequest()); + Member member = loginUser.getMember(); + member.setAvatar(avatar); + boolean update = iMemberService.updateById(member); + if (!update) { + throw new BusinessException(""); + } + loginUser.setMember(member); + tokenService.refreshToken(loginUser); + return R.result(update).add("userInfo", member); + } } diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/service/impl/OrderServiceImpl.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/service/impl/OrderServiceImpl.java index 9e21671..ddcabcd 100644 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/service/impl/OrderServiceImpl.java +++ b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/service/impl/OrderServiceImpl.java @@ -13,6 +13,7 @@ import com.github.binarywang.wxpay.bean.result.BaseWxPayResult; import com.github.binarywang.wxpay.constant.WxPayConstants; import com.github.binarywang.wxpay.exception.WxPayException; import com.github.binarywang.wxpay.service.WxPayService; +import com.wayn.common.config.WaynConfig; import com.wayn.common.constant.SysConstants; import com.wayn.common.core.domain.shop.*; import com.wayn.common.core.domain.vo.OrderVO; @@ -31,7 +32,6 @@ import com.wayn.mobile.api.service.ICartService; import com.wayn.mobile.api.service.IOrderService; import com.wayn.mobile.api.task.OrderUnpaidTask; import com.wayn.mobile.api.util.OrderSnGenUtil; -import com.wayn.mobile.framework.config.WaynConfig; import com.wayn.mobile.framework.redis.RedisCache; import com.wayn.mobile.framework.security.util.SecurityUtils; import lombok.extern.slf4j.Slf4j; diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/MvcConfig.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/MvcConfig.java index e8baf75..043bcb8 100644 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/MvcConfig.java +++ b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/MvcConfig.java @@ -1,5 +1,6 @@ package com.wayn.mobile.framework.config; +import com.wayn.common.config.WaynConfig; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/SwaggerConfig.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/SwaggerConfig.java index 22d0a43..eec63c6 100644 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/SwaggerConfig.java +++ b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/SwaggerConfig.java @@ -1,5 +1,6 @@ package com.wayn.mobile.framework.config; +import com.wayn.common.config.WaynConfig; import io.swagger.annotations.ApiOperation; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/WaynConfig.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/WaynConfig.java deleted file mode 100644 index 369a2f7..0000000 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/WaynConfig.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.wayn.mobile.framework.config; - -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; - -@Component -@ConfigurationProperties(prefix = "wayn") -public class WaynConfig { - /** - * 上传路径 - */ - private static String uploadDir; - - /** - * 项目名称 - */ - private static String name; - /** - * 项目版本 - */ - private static String version; - /** - * 联系邮件 - */ - private static String email; - - /** - * 管理后台地址 - */ - private static String adminUrl; - /** - * 商城移动端地址 - */ - private static String mobileUrl; - - public static String getUploadDir() { - return uploadDir; - } - - public void setUploadDir(String uploadDir) { - WaynConfig.uploadDir = uploadDir; - } - - public static String getDownloadPath() { - return getUploadDir() + "/download/"; - } - - public static String getAvatarPath() { - return getUploadDir() + "/avatar/"; - } - - public static String getName() { - return name; - } - - public void setName(String name) { - WaynConfig.name = name; - } - - public static String getVersion() { - return version; - } - - public void setVersion(String version) { - WaynConfig.version = version; - } - - public static String getEmail() { - return email; - } - - public void setEmail(String email) { - WaynConfig.email = email; - } - - public static String getAdminUrl() { - return adminUrl; - } - - public void setAdminUrl(String adminUrl) { - WaynConfig.adminUrl = adminUrl; - } - - public static String getMobileUrl() { - return mobileUrl; - } - - public void setMobileUrl(String mobileUrl) { - WaynConfig.mobileUrl = mobileUrl; - } -} diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/upload/service/UploadService.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/upload/service/UploadService.java deleted file mode 100644 index 5859960..0000000 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/upload/service/UploadService.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.wayn.mobile.framework.manager.upload.service; - -import com.qiniu.common.QiniuException; - -import java.io.IOException; - -/** - *

- * 七牛云上传Service - *

- * - * @package: com.xkcoding.upload.service - * @description: 七牛云上传Service - * @author: yangkai.shen - * @date: Created in 2018/11/6 17:21 - * @copyright: Copyright (c) 2018 - * @version: V1.0 - * @modified: yangkai.shen - */ -public interface UploadService { - /** - * 七牛云上传文件 - * - * @param filename 文件名 - * @return 上传后的文件访问路径 - * @throws QiniuException 七牛异常 - */ - String uploadFile(String filename) throws IOException; -} diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/upload/service/impl/UploadServiceImpl.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/upload/service/impl/UploadServiceImpl.java deleted file mode 100644 index c677872..0000000 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/upload/service/impl/UploadServiceImpl.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.wayn.mobile.framework.manager.upload.service.impl; - -import com.alibaba.fastjson.JSONObject; -import com.qiniu.common.QiniuException; -import com.qiniu.http.Response; -import com.qiniu.storage.Configuration; -import com.qiniu.storage.UploadManager; -import com.qiniu.util.Auth; -import com.wayn.common.core.domain.tool.QiniuConfig; -import com.wayn.common.core.service.tool.IQiniuConfigService; -import com.wayn.common.exception.BusinessException; -import com.wayn.common.util.ServletUtils; -import com.wayn.common.util.file.FileUtils; -import com.wayn.common.util.file.QiniuUtil; -import com.wayn.common.util.http.HttpUtil; -import com.wayn.mobile.framework.config.WaynConfig; -import com.wayn.mobile.framework.manager.upload.service.UploadService; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import java.io.File; - -/** - *

- * 七牛云上传Service - *

- * - * @package: com.xkcoding.upload.service.impl - * @description: 七牛云上传Service - * @author: yangkai.shen - * @date: Created in 2018/11/6 17:22 - * @copyright: Copyright (c) 2018 - * @version: V1.0 - * @modified: yangkai.shen - */ -@Service -@Slf4j -public class UploadServiceImpl implements UploadService { - - @Autowired - private IQiniuConfigService iQiniuConfigService; - - /** - * 七牛云上传文件 - * - * @param fileName 文件名 - * @return 上传后的文件访问路径 - * @throws QiniuException 七牛异常 - */ - @Override - public String uploadFile(String fileName) { - QiniuConfig qiniuConfig = iQiniuConfigService.getById(1); - if (qiniuConfig != null && qiniuConfig.getEnable()) { - File file = new File(WaynConfig.getUploadDir() + File.separator + fileName); - Configuration cfg = new Configuration(QiniuUtil.getRegion(qiniuConfig.getRegion())); - UploadManager uploadManager = new UploadManager(cfg); - Auth auth = Auth.create(qiniuConfig.getAccessKey(), qiniuConfig.getSecretKey()); - String upToken = auth.uploadToken(qiniuConfig.getBucket()); - Response response; - try { - response = uploadManager.put(file, file.getName(), upToken); - int retry = 0; - while (response.needRetry() && retry < 3) { - response = uploadManager.put(file, file.getName(), upToken); - retry++; - } - if (response.isOK()) { - JSONObject jsonObject = JSONObject.parseObject(response.bodyString()); - String yunFileName = jsonObject.getString("key"); - String yunFilePath = qiniuConfig.getHost() + "/" + yunFileName; - FileUtils.deleteQuietly(file); - log.info("【文件上传至七牛云】绝对路径:{}", yunFilePath); - return yunFilePath; - } else { - log.error("【文件上传至七牛云】失败,{}", JSONObject.toJSONString(response)); - FileUtils.deleteQuietly(file); - throw new BusinessException("文件上传至七牛云失败"); - } - } catch (QiniuException e) { - FileUtils.deleteQuietly(file); - throw new BusinessException("文件上传至七牛云失败"); - } - } else { - String requestUrl = HttpUtil.getRequestContext(ServletUtils.getRequest()); - return requestUrl + "/upload/" + fileName; - } - } -}