| @@ -1,17 +1,5 @@ | |||||
| package com.iformall.ueditor.upload; | package com.iformall.ueditor.upload; | ||||
| import com.alibaba.fastjson.JSON; | |||||
| import com.alibaba.fastjson.JSONObject; | |||||
| import com.amazonaws.AmazonClientException; | |||||
| import com.amazonaws.AmazonServiceException; | |||||
| import com.amazonaws.auth.AWSCredentials; | |||||
| import com.amazonaws.auth.AWSCredentialsProvider; | |||||
| import com.amazonaws.auth.BasicAWSCredentials; | |||||
| import com.amazonaws.services.s3.AmazonS3; | |||||
| import com.amazonaws.services.s3.AmazonS3ClientBuilder; | |||||
| import com.amazonaws.services.s3.model.CannedAccessControlList; | |||||
| import com.amazonaws.services.s3.model.ObjectMetadata; | |||||
| import com.amazonaws.services.s3.model.PutObjectRequest; | |||||
| import com.iformall.common.ResultData; | import com.iformall.common.ResultData; | ||||
| import com.iformall.domain.po.MallResource; | import com.iformall.domain.po.MallResource; | ||||
| import com.iformall.domain.po.base.TenantEntity; | import com.iformall.domain.po.base.TenantEntity; | ||||
| @@ -25,15 +13,12 @@ import org.apache.shiro.SecurityUtils; | |||||
| import org.apache.shiro.session.Session; | import org.apache.shiro.session.Session; | ||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| import org.springframework.web.multipart.MultipartFile; | import org.springframework.web.multipart.MultipartFile; | ||||
| import org.springframework.web.multipart.MultipartHttpServletRequest; | import org.springframework.web.multipart.MultipartHttpServletRequest; | ||||
| import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.net.URL; | |||||
| import java.util.Map; | import java.util.Map; | ||||
| import java.util.UUID; | |||||
| public class Uploader { | public class Uploader { | ||||
| @@ -42,38 +27,14 @@ public class Uploader { | |||||
| private HttpServletRequest request = null; | private HttpServletRequest request = null; | ||||
| private Map<String, Object> conf = null; | private Map<String, Object> conf = null; | ||||
| private MallResourceService mallResourceService = null; | private MallResourceService mallResourceService = null; | ||||
| private AmazonS3 s3 = null; | |||||
| private AliyunOSS aliyunOSS; | private AliyunOSS aliyunOSS; | ||||
| private String getFileName(TenantEntity tenantEntity, String fileName) { | |||||
| if (StringUtils.isBlank(tenantEntity.getParentTenantId())) { | |||||
| fileName = tenantEntity.getTenantId() + "/" + fileName; | |||||
| } else { | |||||
| fileName = tenantEntity.getTenantId() + "/" + tenantEntity.getParentTenantId() + "/" + fileName; | |||||
| } | |||||
| return fileName; | |||||
| } | |||||
| public Uploader(HttpServletRequest request, Map<String, Object> conf, MallResourceService mallResourceService, AliyunOSS aliyunOSS) { | public Uploader(HttpServletRequest request, Map<String, Object> conf, MallResourceService mallResourceService, AliyunOSS aliyunOSS) { | ||||
| this.request = request; | this.request = request; | ||||
| this.conf = conf; | this.conf = conf; | ||||
| this.mallResourceService = mallResourceService; | this.mallResourceService = mallResourceService; | ||||
| this.aliyunOSS = aliyunOSS; | this.aliyunOSS = aliyunOSS; | ||||
| // this.s3 = AmazonS3ClientBuilder.standard() | |||||
| // .withRegion(conf.get("clientRegion").toString()) | |||||
| // .withCredentials(new AWSCredentialsProvider() { | |||||
| // @Override | |||||
| // public AWSCredentials getCredentials() { | |||||
| // return new BasicAWSCredentials(conf.get("access").toString(), conf.get("secret").toString()); | |||||
| // } | |||||
| // | |||||
| // @Override | |||||
| // public void refresh() { | |||||
| // | |||||
| // } | |||||
| // }) | |||||
| // .build(); | |||||
| } | } | ||||
| public final State doExec() { | public final State doExec() { | ||||
| @@ -99,32 +60,17 @@ public class Uploader { | |||||
| logger.error("TENANT is null"); | logger.error("TENANT is null"); | ||||
| } | } | ||||
| String fileName = UUID.randomUUID().toString(); | |||||
| int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | ||||
| String fileFormat = ""; | String fileFormat = ""; | ||||
| if (dot >= 0) { | if (dot >= 0) { | ||||
| fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | ||||
| fileName = getFileName(tenantEntity, fileName + fileFormat); | |||||
| } else { | |||||
| fileName = getFileName(tenantEntity, fileName); | |||||
| } | } | ||||
| state = state = new BaseState(true); | |||||
| state = new BaseState(true); | |||||
| state.putInfo("size", multiReq.getSize()); | state.putInfo("size", multiReq.getSize()); | ||||
| state.putInfo("title", filedName); | state.putInfo("title", filedName); | ||||
| JSONObject stateJson = JSON.parseObject(state.toJSONString()); | |||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multiReq.getContentType()); | |||||
| metadata.setContentLength(multiReq.getSize()); | |||||
| try { | try { | ||||
| // s3.putObject( | |||||
| // new PutObjectRequest(conf.get("bucketName").toString(), fileName, multiReq.getInputStream(), metadata) | |||||
| // .withCannedAcl(CannedAccessControlList.PublicRead)); | |||||
| // URL url = s3.getUrl(conf.get("bucketName").toString(), fileName); | |||||
| //logger.info(url.toString()); | |||||
| ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ||||
| Map<String, String> map = (Map<String, String>) data.data; | Map<String, String> map = (Map<String, String>) data.data; | ||||
| @@ -133,38 +79,10 @@ public class Uploader { | |||||
| MallResource mr = new MallResource(); | MallResource mr = new MallResource(); | ||||
| mr.updateTenantInfo(tenantEntity); | mr.updateTenantInfo(tenantEntity); | ||||
| mr.setBucket(map.get("bucketName")); | mr.setBucket(map.get("bucketName")); | ||||
| //mr.setS3Key(fileName); | |||||
| mr.setUrl(map.get("url")); | mr.setUrl(map.get("url")); | ||||
| // state.putInfo("url", url.toString()); | |||||
| // | |||||
| // MallResource mr = new MallResource(); | |||||
| // mr.updateTenantInfo(tenantEntity); | |||||
| // mr.setBucket(conf.get("bucketName").toString()); | |||||
| // mr.setS3Key(fileName); | |||||
| // mr.setUrl(url.toString()); | |||||
| mallResourceService.saveOrUpdate(mr); | mallResourceService.saveOrUpdate(mr); | ||||
| } catch (AmazonServiceException ase) { | |||||
| /* | |||||
| logger.warn("Caught an AmazonServiceException, which " + | |||||
| "means your request made it " + | |||||
| "to Amazon S3, but was rejected with an error response" + | |||||
| " for some reason."); | |||||
| logger.warn(ase.getMessage()); | |||||
| */ | |||||
| ase.printStackTrace(); | |||||
| } catch (AmazonClientException ace) { | |||||
| /* | |||||
| logger.warn("Caught an AmazonClientException, which " + | |||||
| "means the client encountered " + | |||||
| "an internal error while trying to " + | |||||
| "communicate with S3, " + | |||||
| "such as not being able to access the network."); | |||||
| logger.warn("Error Message: " + ace.getMessage()); | |||||
| */ | |||||
| ace.printStackTrace(); | |||||
| } catch (IOException ioe) { | } catch (IOException ioe) { | ||||
| //logger.warn("Caught an IOException: " + ioe.getMessage()); | |||||
| ioe.printStackTrace(); | ioe.printStackTrace(); | ||||
| } | } | ||||
| } | } | ||||