AbstractFYunFileService.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.fdkankan.fyun.face;
  2. import com.fdkankan.fyun.config.FYunFileConfig;
  3. import com.fdkankan.fyun.constant.FYunConstants;
  4. import com.fdkankan.fyun.model.StreamGobbler;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Component;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.net.URL;
  12. import java.util.List;
  13. import java.util.Map;
  14. @Component
  15. public abstract class AbstractFYunFileService implements FYunFileServiceInterface {
  16. private Logger log = LoggerFactory.getLogger(this.getClass().getName());
  17. @Autowired
  18. public FYunFileConfig fYunFileConfig;
  19. @Autowired
  20. public FYunConstants fYunConstants;
  21. public String getFyunType(){
  22. return fYunFileConfig.getFyunType();
  23. }
  24. @Override
  25. public String uploadFile(byte[] data, String remoteFilePath) {
  26. uploadFile(fYunFileConfig.getBucket(), data, remoteFilePath);
  27. return fYunFileConfig.getHost().concat(remoteFilePath);
  28. }
  29. @Override
  30. public String uploadFile(String filePath, String remoteFilePath) {
  31. uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath);
  32. return fYunFileConfig.getHost().concat(remoteFilePath);
  33. }
  34. @Override
  35. public String uploadFile(String filePath, String remoteFilePath, Map<String, String> headers) {
  36. uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath, headers);
  37. return fYunFileConfig.getHost().concat(remoteFilePath);
  38. }
  39. @Override
  40. public String uploadFile(InputStream inputStream, String remoteFilePath) {
  41. uploadFile(fYunFileConfig.getBucket(), inputStream, remoteFilePath);
  42. return fYunFileConfig.getHost().concat(remoteFilePath);
  43. }
  44. @Override
  45. public String uploadFileByCommand(String filePath, String remoteFilePath) {
  46. uploadFileByCommand(fYunFileConfig.getBucket(), filePath, remoteFilePath);
  47. return fYunFileConfig.getHost().concat(remoteFilePath);
  48. }
  49. @Override
  50. public void downloadFileByCommand(String filePath, String remoteFilePath) {
  51. downloadFileByCommand(fYunFileConfig.getBucket(),filePath,remoteFilePath);
  52. }
  53. @Override
  54. public void downloadByCommand(String filePath, String remoteFilePath, boolean isDir) {
  55. downloadByCommand(fYunFileConfig.getBucket(),filePath,remoteFilePath, isDir);
  56. }
  57. @Override
  58. public void deleteFile(String remoteFilePath) throws IOException {
  59. deleteFile(fYunFileConfig.getBucket(), remoteFilePath);
  60. }
  61. @Override
  62. public void deleteFolder(String remoteFolderPath) {
  63. deleteFolder(fYunFileConfig.getBucket(), remoteFolderPath);
  64. }
  65. @Override
  66. public void uploadMulFiles(Map<String, String> filepaths) {
  67. uploadMulFiles(fYunFileConfig.getBucket(), filepaths);
  68. }
  69. @Override
  70. public List<String> listRemoteFiles(String sourcePath) {
  71. return listRemoteFiles(fYunFileConfig.getBucket(), sourcePath);
  72. }
  73. public void copyFileInBucket(String sourcePath, String targetPath) {
  74. copyFileBetweenBucket(fYunFileConfig.getBucket(), sourcePath, fYunFileConfig.getBucket(), targetPath);
  75. }
  76. public void copyFileInBucketParallel(String sourcePath, String targetPath) {
  77. copyFileBetweenBucketParallel(fYunFileConfig.getBucket(), sourcePath, fYunFileConfig.getBucket(), targetPath);
  78. }
  79. @Override
  80. public void copyFileToArchive(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
  81. copyFileToArchive(sourceBucketName, sourcePath, targetBucketName, targetPath);
  82. }
  83. public void copyFileInBucket(String bucket, String sourcePath, String targetPath) {
  84. copyFileBetweenBucket(bucket, sourcePath, bucket, targetPath);
  85. }
  86. @Override
  87. public void copyFileBetweenBucket(String sourcePath, String targetBucketName, String targetPath) {
  88. copyFileBetweenBucket(fYunFileConfig.getBucket(), sourcePath, targetBucketName, targetPath);
  89. }
  90. @Override
  91. public void copyFilesBetweenBucket(String targetBucketName, Map<String, String> pathMap) {
  92. copyFilesBetweenBucket(fYunFileConfig.getBucket(), targetBucketName, pathMap);
  93. }
  94. @Override
  95. public String getFileContent(String remoteFilePath) {
  96. return getFileContent(fYunFileConfig.getBucket(), remoteFilePath);
  97. }
  98. @Override
  99. public boolean fileExist(String objectName) {
  100. return fileExist(fYunFileConfig.getBucket(), objectName);
  101. }
  102. @Override
  103. public void downloadFile(String remoteFilePath, String localPath) {
  104. downloadFile(fYunFileConfig.getBucket(), remoteFilePath, localPath);
  105. }
  106. public void callshell(String command){
  107. try {
  108. Long start = System.currentTimeMillis();
  109. Process process = Runtime.getRuntime().exec(command);
  110. StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");
  111. errorGobbler.start();
  112. StreamGobbler outGobbler = new StreamGobbler(process.getInputStream(), "STDOUT");
  113. outGobbler.start();
  114. process.waitFor();
  115. log.info("脚本{}执行完毕,用时:{}ms",command,System.currentTimeMillis()-start);
  116. } catch (Exception e) {
  117. log.error("调用脚本文件上传下载失败, command : " + command, e);
  118. }
  119. }
  120. @Override
  121. public URL getPresignedUrl(String url) {
  122. return getPresignedUrl(fYunFileConfig.getBucket(), url);
  123. }
  124. @Override
  125. public long getSubFileNums(String url) {
  126. return getSubFileNums(fYunFileConfig.getBucket(), url);
  127. }
  128. }