| @@ -1,17 +1,5 @@ | |||
| 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.domain.po.MallResource; | |||
| import com.iformall.domain.po.base.TenantEntity; | |||
| @@ -30,9 +18,7 @@ import org.springframework.web.multipart.MultipartHttpServletRequest; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| import java.io.IOException; | |||
| import java.net.URL; | |||
| import java.util.Map; | |||
| import java.util.UUID; | |||
| public class Uploader { | |||
| @@ -41,34 +27,14 @@ public class Uploader { | |||
| private HttpServletRequest request = null; | |||
| private Map<String, Object> conf = null; | |||
| private MallResourceService mallResourceService = null; | |||
| private AmazonS3 s3 = null; | |||
| private AliyunOSS aliyunOSS; | |||
| private String getFileName(TenantEntity tenantEntity, String fileName) { | |||
| fileName = tenantEntity.getTenantId() + "/" + fileName; | |||
| return fileName; | |||
| } | |||
| public Uploader(HttpServletRequest request, Map<String, Object> conf, MallResourceService mallResourceService, AliyunOSS aliyunOSS) { | |||
| this.request = request; | |||
| this.conf = conf; | |||
| this.mallResourceService = mallResourceService; | |||
| 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() { | |||
| @@ -89,32 +55,17 @@ public class Uploader { | |||
| logger.error("TENANT is null"); | |||
| } | |||
| String fileName = UUID.randomUUID().toString(); | |||
| int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | |||
| String fileFormat = ""; | |||
| if (dot >= 0) { | |||
| 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("title", filedName); | |||
| JSONObject stateJson = JSON.parseObject(state.toJSONString()); | |||
| ObjectMetadata metadata = new ObjectMetadata(); | |||
| metadata.setContentType(multiReq.getContentType()); | |||
| metadata.setContentLength(multiReq.getSize()); | |||
| 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()); | |||
| Map<String, String> map = (Map<String, String>) data.data; | |||
| state.putInfo("url",map.get("url")); | |||
| @@ -122,38 +73,10 @@ public class Uploader { | |||
| MallResource mr = new MallResource(); | |||
| mr.updateTenantInfo(tenantEntity); | |||
| mr.setBucket(map.get("bucketName")); | |||
| //mr.setS3Key(fileName); | |||
| 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); | |||
| } 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) { | |||
| //logger.warn("Caught an IOException: " + ioe.getMessage()); | |||
| ioe.printStackTrace(); | |||
| } | |||
| } | |||