|
@@ -7,6 +7,9 @@ import java.io.IOException;
|
|
|
import javax.servlet.ServletInputStream;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
+import cn.com.qmth.examcloud.web.filestorage.FileStoragePathEnvInfo;
|
|
|
+import cn.com.qmth.examcloud.web.filestorage.FileStorageUtil;
|
|
|
+import cn.com.qmth.examcloud.web.filestorage.YunPathInfo;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.poi.util.IOUtils;
|
|
@@ -40,98 +43,103 @@ import io.swagger.annotations.ApiOperation;
|
|
|
@RequestMapping("${$rmp.ctr.exchange.inner}/upyun")
|
|
|
public class UpyunController extends ControllerSupport {
|
|
|
|
|
|
- @Autowired
|
|
|
- SystemProperties systemConfig;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- UpyunService upyunService;
|
|
|
-
|
|
|
- @ApiOperation(value = "保存照片")
|
|
|
- @PutMapping("put/{siteId}/{fileSuffix}")
|
|
|
- public String putFile(@PathVariable String siteId, @PathVariable String fileSuffix,
|
|
|
- @RequestParam(required = false) String md5, HttpServletRequest req) {
|
|
|
-
|
|
|
- String contentLength = req.getHeader("Content-Length");
|
|
|
- if (StringUtils.isNotBlank(contentLength)) {
|
|
|
- long contentLengthLong = Long.parseLong(contentLength);
|
|
|
- if (contentLengthLong < 10) {
|
|
|
- throw new StatusException("600108", "empty IO stream");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (StringUtils.isNotBlank(md5)) {
|
|
|
- if (MD5.encrypt16("").equalsIgnoreCase(md5)) {
|
|
|
- throw new StatusException("600109", "empty IO stream");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- User accessUser = getAccessUser();
|
|
|
-
|
|
|
- if (!fileSuffix.matches("\\w+")) {
|
|
|
- throw new StatusException("600100", "fileSuffix is wrong");
|
|
|
- }
|
|
|
-
|
|
|
- fileSuffix = "." + fileSuffix;
|
|
|
-
|
|
|
- ServletInputStream in = null;
|
|
|
-
|
|
|
- if (StringUtils.isNotBlank(md5)) {
|
|
|
-
|
|
|
- try {
|
|
|
- in = req.getInputStream();
|
|
|
-
|
|
|
- UpyunPathEnvironmentInfo env = new UpyunPathEnvironmentInfo();
|
|
|
- env.setRootOrgId(String.valueOf(accessUser.getRootOrgId()));
|
|
|
- env.setUserId(String.valueOf(accessUser.getUserId()));
|
|
|
- env.setFileSuffix(fileSuffix);
|
|
|
- String url = upyunService.writeFile(siteId, env, in, md5).getUrl();
|
|
|
- url = new AES().encrypt(url);
|
|
|
- return url;
|
|
|
- } catch (IOException e) {
|
|
|
- throw new ExamCloudRuntimeException(e);
|
|
|
- } finally {
|
|
|
- IOUtils.closeQuietly(in);
|
|
|
- }
|
|
|
-
|
|
|
- } else {
|
|
|
-
|
|
|
- try {
|
|
|
- FileUtils.forceMkdir(new File(systemConfig.getTempDataDir()));
|
|
|
- } catch (IOException e1) {
|
|
|
- log.error("fail to make dir. path=" + systemConfig.getTempDataDir());
|
|
|
- }
|
|
|
-
|
|
|
- String filePath = systemConfig.getTempDataDir() + File.separator + UUID.randomUUID()
|
|
|
- + fileSuffix;
|
|
|
- File file = new File(filePath);
|
|
|
-
|
|
|
- FileOutputStream out = null;
|
|
|
- try {
|
|
|
- in = req.getInputStream();
|
|
|
- out = new FileOutputStream(file);
|
|
|
- IOUtils.copy(in, out);
|
|
|
-
|
|
|
- UpyunPathEnvironmentInfo env = new UpyunPathEnvironmentInfo();
|
|
|
- env.setRootOrgId(String.valueOf(accessUser.getRootOrgId()));
|
|
|
- env.setUserId(String.valueOf(accessUser.getUserId()));
|
|
|
- env.setFileSuffix(fileSuffix);
|
|
|
- String url = upyunService.writeFile(siteId, env, file, true).getUrl();
|
|
|
- url = new AES().encrypt(url);
|
|
|
- return url;
|
|
|
- } catch (IOException e) {
|
|
|
- throw new ExamCloudRuntimeException(e);
|
|
|
- } finally {
|
|
|
- IOUtils.closeQuietly(in);
|
|
|
- IOUtils.closeQuietly(out);
|
|
|
- try {
|
|
|
- FileUtils.forceDelete(file);
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("fail to delete file. path=" + filePath);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
+ @Autowired
|
|
|
+ SystemProperties systemConfig;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ UpyunService upyunService;
|
|
|
+
|
|
|
+ @ApiOperation(value = "保存照片")
|
|
|
+ @PutMapping("put/{siteId}/{fileSuffix}")
|
|
|
+ public String putFile(@PathVariable String siteId, @PathVariable String fileSuffix,
|
|
|
+ @RequestParam(required = false) String md5, HttpServletRequest req) {
|
|
|
+
|
|
|
+ String contentLength = req.getHeader("Content-Length");
|
|
|
+ if (StringUtils.isNotBlank(contentLength)) {
|
|
|
+ long contentLengthLong = Long.parseLong(contentLength);
|
|
|
+ if (contentLengthLong < 10) {
|
|
|
+ throw new StatusException("600108", "empty IO stream");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(md5)) {
|
|
|
+ if (MD5.encrypt16("").equalsIgnoreCase(md5)) {
|
|
|
+ throw new StatusException("600109", "empty IO stream");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ User accessUser = getAccessUser();
|
|
|
+
|
|
|
+ if (!fileSuffix.matches("\\w+")) {
|
|
|
+ throw new StatusException("600100", "fileSuffix is wrong");
|
|
|
+ }
|
|
|
+
|
|
|
+ fileSuffix = "." + fileSuffix;
|
|
|
+
|
|
|
+ ServletInputStream in = null;
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(md5)) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ in = req.getInputStream();
|
|
|
+
|
|
|
+ //通用存储
|
|
|
+ FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
|
|
|
+ env.setRootOrgId(String.valueOf(accessUser.getRootOrgId()));
|
|
|
+ env.setUserId(String.valueOf(accessUser.getUserId()));
|
|
|
+ env.setFileSuffix(fileSuffix);
|
|
|
+ YunPathInfo pi = FileStorageUtil.saveFile(siteId, env, in, md5);
|
|
|
+ String url = pi.getUrl();
|
|
|
+
|
|
|
+ url = new AES().encrypt(url);
|
|
|
+ return url;
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
+ } finally {
|
|
|
+ IOUtils.closeQuietly(in);
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ try {
|
|
|
+ FileUtils.forceMkdir(new File(systemConfig.getTempDataDir()));
|
|
|
+ } catch (IOException e1) {
|
|
|
+ log.error("fail to make dir. path=" + systemConfig.getTempDataDir());
|
|
|
+ }
|
|
|
+
|
|
|
+ String filePath = systemConfig.getTempDataDir() + File.separator + UUID.randomUUID()
|
|
|
+ + fileSuffix;
|
|
|
+ File file = new File(filePath);
|
|
|
+
|
|
|
+ FileOutputStream out = null;
|
|
|
+ try {
|
|
|
+ in = req.getInputStream();
|
|
|
+ out = new FileOutputStream(file);
|
|
|
+ IOUtils.copy(in, out);
|
|
|
+
|
|
|
+ //通用存储
|
|
|
+ FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
|
|
|
+ env.setRootOrgId(String.valueOf(accessUser.getRootOrgId()));
|
|
|
+ env.setUserId(String.valueOf(accessUser.getUserId()));
|
|
|
+ env.setFileSuffix(fileSuffix);
|
|
|
+ YunPathInfo pi = FileStorageUtil.saveFile(siteId, env, file, true);
|
|
|
+ String url = pi.getUrl();
|
|
|
+ url = new AES().encrypt(url);
|
|
|
+ return url;
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
+ } finally {
|
|
|
+ IOUtils.closeQuietly(in);
|
|
|
+ IOUtils.closeQuietly(out);
|
|
|
+ try {
|
|
|
+ FileUtils.forceDelete(file);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("fail to delete file. path=" + filePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
}
|