refactor(移动端): 代码优化
parent
c0f4708081
commit
140a77330c
@ -1,4 +1,4 @@
|
||||
package com.wayn.admin.framework.manager.upload.service;
|
||||
package com.wayn.common.base.service;
|
||||
|
||||
import com.qiniu.common.QiniuException;
|
||||
|
@ -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;
|
@ -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;
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package com.wayn.mobile.framework.manager.upload.service;
|
||||
|
||||
import com.qiniu.common.QiniuException;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 七牛云上传Service
|
||||
* </p>
|
||||
*
|
||||
* @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;
|
||||
}
|
@ -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;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 七牛云上传Service
|
||||
* </p>
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue