AbstractFYunFileService.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.util.List;
  12. import java.util.Map;
  13. @Component
  14. public abstract class AbstractFYunFileService implements FYunFileServiceInterface {
  15. private Logger log = LoggerFactory.getLogger(this.getClass().getName());
  16. @Autowired
  17. public FYunFileConfig fYunFileConfig;
  18. @Autowired
  19. public FYunConstants fYunConstants;
  20. public String getFyunType(){
  21. return fYunFileConfig.getFyunType();
  22. }
  23. @Override
  24. public String uploadFile(byte[] data, String remoteFilePath) {
  25. uploadFile(fYunFileConfig.getBucket(), data, remoteFilePath);
  26. return fYunFileConfig.getHost().concat(remoteFilePath);
  27. }
  28. @Override
  29. public String uploadFile(String filePath, String remoteFilePath) {
  30. uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath);
  31. return fYunFileConfig.getHost().concat(remoteFilePath);
  32. }
  33. @Override
  34. public String uploadFile(String filePath, String remoteFilePath, Map<String, String> headers) {
  35. uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath, headers);
  36. return fYunFileConfig.getHost().concat(remoteFilePath);
  37. }
  38. @Override
  39. public String uploadFile(InputStream inputStream, String remoteFilePath) {
  40. uploadFile(fYunFileConfig.getBucket(), inputStream, remoteFilePath);
  41. return fYunFileConfig.getHost().concat(remoteFilePath);
  42. }
  43. @Override
  44. public String uploadFileByCommand(String filePath, String remoteFilePath) {
  45. uploadFileByCommand(fYunFileConfig.getBucket(), filePath, remoteFilePath);
  46. return fYunFileConfig.getHost().concat(remoteFilePath);
  47. }
  48. @Override
  49. public void downloadFileByCommand(String filePath, String remoteFilePath) {
  50. downloadFileByCommand(fYunFileConfig.getBucket(),filePath,remoteFilePath);
  51. }
  52. @Override
  53. public void deleteFile(String remoteFilePath) throws IOException {
  54. deleteFile(fYunFileConfig.getBucket(), remoteFilePath);
  55. }
  56. @Override
  57. public void deleteFolder(String remoteFolderPath) {
  58. deleteFolder(fYunFileConfig.getBucket(), remoteFolderPath);
  59. }
  60. @Override
  61. public void uploadMulFiles(Map<String, String> filepaths) {
  62. uploadMulFiles(fYunFileConfig.getBucket(), filepaths);
  63. }
  64. @Override
  65. public List<String> listRemoteFiles(String sourcePath) {
  66. return listRemoteFiles(fYunFileConfig.getBucket(), sourcePath);
  67. }
  68. public void copyFileInBucket(String sourcePath, String targetPath) {
  69. copyFileBetweenBucket(fYunFileConfig.getBucket(), sourcePath, fYunFileConfig.getBucket(), targetPath);
  70. }
  71. public void copyFileInBucket(String bucket, String sourcePath, String targetPath) {
  72. copyFileBetweenBucket(bucket, sourcePath, bucket, targetPath);
  73. }
  74. @Override
  75. public void copyFileBetweenBucket(String sourcePath, String targetBucketName, String targetPath) {
  76. copyFileBetweenBucket(fYunFileConfig.getBucket(), sourcePath, targetBucketName, targetPath);
  77. }
  78. @Override
  79. public void copyFilesBetweenBucket(String targetBucketName, Map<String, String> pathMap) {
  80. copyFilesBetweenBucket(fYunFileConfig.getBucket(), targetBucketName, pathMap);
  81. }
  82. @Override
  83. public String getFileContent(String remoteFilePath) {
  84. return getFileContent(fYunFileConfig.getBucket(), remoteFilePath);
  85. }
  86. @Override
  87. public boolean fileExist(String objectName) {
  88. return fileExist(fYunFileConfig.getBucket(), objectName);
  89. }
  90. @Override
  91. public void downloadFile(String remoteFilePath, String localPath) {
  92. downloadFile(fYunFileConfig.getBucket(), remoteFilePath, localPath);
  93. }
  94. public void callshell(String command){
  95. try {
  96. Long start = System.currentTimeMillis();
  97. Process process = Runtime.getRuntime().exec(command);
  98. StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");
  99. errorGobbler.start();
  100. StreamGobbler outGobbler = new StreamGobbler(process.getInputStream(), "STDOUT");
  101. outGobbler.start();
  102. process.waitFor();
  103. log.info("脚本{}执行完毕,用时:{}ms",command,System.currentTimeMillis()-start);
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. }