HttpFileService.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package com.fdkankan.fyun.http;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.fdkankan.common.util.FileUtils;
  5. import com.fdkankan.fyun.face.AbstractFYunFileService;
  6. import com.fdkankan.fyun.http.config.HttpFyunConfig;
  7. import com.fdkankan.fyun.http.entity.Result;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  12. import org.springframework.http.HttpStatus;
  13. import org.springframework.http.ResponseEntity;
  14. import org.springframework.stereotype.Component;
  15. import org.springframework.util.ObjectUtils;
  16. import org.springframework.web.client.RestTemplate;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.net.URL;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. @Component
  25. @ConditionalOnProperty(name = "fyun.type", havingValue = "https")
  26. public class HttpFileService extends AbstractFYunFileService {
  27. private Logger log = LoggerFactory.getLogger(this.getClass().getName());
  28. private RestTemplate restTemplate = new RestTemplate();
  29. @Autowired
  30. private HttpFyunConfig httpFyunConfig;
  31. @Override
  32. public String uploadFile(String bucket, byte[] data, String remoteFilePath) {
  33. try {
  34. // 先将文件保存至本地
  35. String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/")+1);
  36. FileUtils.writeFile(httpFyunConfig.getLocalTempPath(), fileName, data);
  37. uploadFile(bucket,httpFyunConfig.getLocalTempPath() + fileName,remoteFilePath,null);
  38. FileUtils.deleteFile(httpFyunConfig.getLocalTempPath() + fileName);
  39. } catch (Exception e) {
  40. log.error("oss上传文件失败", e);
  41. e.printStackTrace();
  42. }
  43. return null;
  44. }
  45. @Override
  46. public String uploadFile(String bucket, String filePath, String remoteFilePath) {
  47. return uploadFile(bucket, filePath, remoteFilePath, null);
  48. }
  49. @Override
  50. public String uploadFile(String bucket, InputStream inputStream, String remoteFilePath) {
  51. try {
  52. // 先将文件保存至本地
  53. String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/") + 1);
  54. FileUtil.writeFromStream(inputStream, httpFyunConfig.getLocalTempPath() + fileName);
  55. uploadFile(bucket,httpFyunConfig.getLocalTempPath() + fileName,remoteFilePath,null);
  56. FileUtils.deleteFile(httpFyunConfig.getLocalTempPath() + fileName);
  57. } catch (Exception e) {
  58. log.error("oss上传文件失败", e);
  59. e.printStackTrace();
  60. }
  61. return null;
  62. }
  63. @Override
  64. public String uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) {
  65. try {
  66. File file = new File(filePath);
  67. if (!file.exists()) {
  68. log.error("要上传的文件不存在:" + filePath);
  69. return null;
  70. }
  71. Map<String, Object> params = new HashMap<>();
  72. params.put("appName", fYunFileConfig.getKey());
  73. params.put("secret", fYunFileConfig.getSecret());
  74. params.put("fileName", filePath);
  75. params.put("targetPath", remoteFilePath);
  76. String url = fYunFileConfig.getEndPoint() + httpFyunConfig.getUploadFile();
  77. ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
  78. log.info("Fyun Http Utils upload,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
  79. if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
  80. log.error("Fyun Http Utils upload failed!");
  81. }
  82. log.info("文件上传成功,path:{}", filePath);
  83. } catch (Exception e) {
  84. log.error("oss上传文件失败", e);
  85. e.printStackTrace();
  86. }
  87. return null;
  88. }
  89. @Override
  90. public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) {
  91. // 上传文件夹
  92. Map<String, Object> params = new HashMap<>();
  93. params.put("appName", fYunFileConfig.getKey());
  94. params.put("secret", fYunFileConfig.getSecret());
  95. params.put("dirName", filePath);
  96. params.put("targetPath", remoteFilePath);
  97. String url = fYunFileConfig.getEndPoint() + httpFyunConfig.getUploadDir();
  98. ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
  99. log.info("Fyun Http Utils upload folder,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
  100. if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
  101. log.error("Fyun Http Utils upload folder failed!");
  102. }
  103. return null;
  104. }
  105. @Override
  106. public void downloadFileByCommand(String bucket, String localPath, String remoteFilePath) {
  107. // 下载文件夹
  108. File localFile = new File(localPath);
  109. if(localFile.isDirectory()){
  110. if(!localFile.exists()){
  111. localFile.mkdirs();
  112. }
  113. String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/")+1);
  114. log.info("未配置文件名,使用默认文件名:{}",fileName);
  115. localPath = localPath.concat(File.separator).concat(fileName);
  116. }else if(!localFile.getParentFile().exists()){
  117. localFile.getParentFile().mkdirs();
  118. }
  119. Map<String, Object> params = new HashMap<>();
  120. params.put("appName", fYunFileConfig.getKey());
  121. params.put("secret", fYunFileConfig.getSecret());
  122. params.put("fileName", remoteFilePath);
  123. params.put("targetPath", localPath);
  124. String url = fYunFileConfig.getEndPoint() + httpFyunConfig.getDownloadDir();
  125. ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
  126. log.info("Fyun Http Utils download folder,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
  127. if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
  128. log.error("Fyun Http Utils download folder failed!");
  129. }
  130. }
  131. @Override
  132. public void downloadByCommand(String bucket, String filePath, String remoteFilePath, boolean isDir) {
  133. }
  134. @Override
  135. public void deleteFile(String bucket, String remoteFilePath) throws IOException {
  136. try {
  137. Map<String, Object> params = new HashMap<>();
  138. params.put("appName", fYunFileConfig.getKey());
  139. params.put("secret", fYunFileConfig.getSecret());
  140. params.put("fileName", remoteFilePath);
  141. String url = fYunFileConfig.getEndPoint() + httpFyunConfig.getDeleteFile();
  142. ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
  143. log.info("Fyun Http Utils delete file,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
  144. if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
  145. log.error("Fyun Http Utils delete file failed!");
  146. }
  147. log.info("文件删除成功,path:{}", remoteFilePath);
  148. } catch (Exception e) {
  149. log.error("OSS删除文件失败,key=" + remoteFilePath);
  150. e.printStackTrace();
  151. }
  152. }
  153. @Override
  154. public void deleteFolder(String bucket, String remoteFolderPath) {
  155. try {
  156. Map<String, Object> params = new HashMap<>();
  157. params.put("appName", fYunFileConfig.getKey());
  158. params.put("secret", fYunFileConfig.getSecret());
  159. params.put("dirName", remoteFolderPath);
  160. String url = fYunFileConfig.getEndPoint() + httpFyunConfig.getDeleteDir();
  161. ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
  162. log.info("Fyun Http Utils delete folder,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
  163. if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
  164. log.error("Fyun Http Utils delete folder failed!");
  165. }
  166. log.info("文件夹删除成功,path:{}", remoteFolderPath);
  167. } catch (Exception e) {
  168. log.error("OSS删除文件失败,key=" + remoteFolderPath);
  169. e.printStackTrace();
  170. }
  171. }
  172. @Override
  173. public void uploadMulFiles(String bucket, Map<String, String> filepaths) {
  174. try {
  175. for (Map.Entry<String, String> entry : filepaths.entrySet()) {
  176. uploadFile(bucket, entry.getKey(), entry.getValue(), null);
  177. }
  178. } catch (Exception e) {
  179. log.error("OSS批量上传文件失败!");
  180. }
  181. }
  182. @Override
  183. public List<String> listRemoteFiles(String bucket, String sourcePath) {
  184. throw new UnsupportedOperationException("不支持列举文件列表!");
  185. }
  186. @Override
  187. public void copyFileBetweenBucket(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
  188. try {
  189. Map<String, Object> params = new HashMap<>();
  190. params.put("appName", fYunFileConfig.getKey());
  191. params.put("secret", fYunFileConfig.getSecret());
  192. params.put("targetDir", targetPath);
  193. String url;
  194. if(new File(sourcePath).isDirectory()){
  195. params.put("dirName", sourcePath);
  196. url = fYunFileConfig.getEndPoint() + httpFyunConfig.getCopyDir();
  197. }else{
  198. params.put("fileName", sourcePath);
  199. url = fYunFileConfig.getEndPoint() + httpFyunConfig.getCopyFile();
  200. }
  201. ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
  202. log.info("Fyun Http Utils copy file or dir,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
  203. if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
  204. log.error("Fyun Http Utils copy file or dir failed!");
  205. }
  206. log.info("文件拷贝成功,path:{}", targetPath);
  207. } catch (Exception e) {
  208. log.error("列举文件目录失败,key=" + sourcePath);
  209. }
  210. }
  211. @Override
  212. public void copyFileBetweenBucketParallel(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
  213. }
  214. @Override
  215. public void copyFilesBetweenBucket(String sourceBucketName, String targetBucketName, Map<String, String> pathMap) {
  216. if (ObjectUtils.isEmpty(pathMap)) {
  217. return;
  218. }
  219. try {
  220. for (Map.Entry<String, String> entry : pathMap.entrySet()) {
  221. copyFileBetweenBucket(sourceBucketName, entry.getKey(), targetBucketName, entry.getValue());
  222. }
  223. } catch (Exception e) {
  224. log.error("批量复制文件失败!");
  225. }
  226. }
  227. @Override
  228. public String getFileContent(String bucketName, String remoteFilePath) {
  229. try {
  230. // 先将文件下载到本地
  231. String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/")+1);
  232. downloadFile(remoteFilePath, httpFyunConfig.getLocalTempPath() + fileName);
  233. String content =FileUtils.readFile(httpFyunConfig.getLocalTempPath() + fileName);
  234. FileUtils.deleteFile(httpFyunConfig.getLocalTempPath() + fileName);
  235. return content;
  236. } catch (Exception e) {
  237. log.error("获取文件内容失败:{}", remoteFilePath);
  238. return null;
  239. }
  240. }
  241. @Override
  242. public boolean fileExist(String bucket, String objectName) {
  243. try {
  244. Map<String, Object> params = new HashMap<>();
  245. params.put("appName", fYunFileConfig.getKey());
  246. params.put("secret", fYunFileConfig.getSecret());
  247. params.put("fileName", objectName);
  248. String url = fYunFileConfig.getEndPoint() + httpFyunConfig.getFileExist();
  249. ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
  250. log.info("Fyun Http Utils file exist,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
  251. if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
  252. log.error("Fyun Http Utils copy file failed!");
  253. }
  254. return Boolean.parseBoolean(responseEntity.getBody().getData().toString());
  255. } catch (Exception e) {
  256. log.error("判断文件是否存在失败:{}", objectName);
  257. return false;
  258. }
  259. }
  260. @Override
  261. public void downloadFile(String bucket, String remoteFilePath, String localPath) {
  262. try {
  263. File localFile = new File(localPath);
  264. if (!localFile.getParentFile().exists()) {
  265. localFile.getParentFile().mkdirs();
  266. }
  267. if(localFile.isDirectory()){
  268. String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/")+1);
  269. log.info("未配置文件名,使用默认文件名:{}",fileName);
  270. localPath = localPath.concat(File.separator).concat(fileName);
  271. }
  272. Map<String, Object> params = new HashMap<>();
  273. params.put("appName", fYunFileConfig.getKey());
  274. params.put("secret", fYunFileConfig.getSecret());
  275. params.put("fileName", remoteFilePath);
  276. params.put("targetPath", localPath);
  277. String url = fYunFileConfig.getEndPoint() + httpFyunConfig.getDownloadFile();
  278. ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
  279. log.info("Fyun Http Utils file exist,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
  280. if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
  281. log.error("Fyun Http Utils copy file failed!");
  282. }
  283. } catch (Throwable throwable) {
  284. log.error("文件下载失败:{}", remoteFilePath);
  285. throwable.printStackTrace();
  286. }
  287. }
  288. @Override
  289. public URL getPresignedUrl(String bucket, String url) {
  290. throw new UnsupportedOperationException("不支持此操作!");
  291. }
  292. @Override
  293. public long getSubFileNums(String bucket, String url) {
  294. return 0;
  295. }
  296. @Override
  297. public Boolean checkStore(String bucket, String url) {
  298. return null;
  299. }
  300. @Override
  301. public void restoreFolder(String bucket, String url) {
  302. }
  303. @Override
  304. public Integer getRestoreFolderProcess(String bucket, String url) {
  305. return null;
  306. }
  307. @Override
  308. public void restoreFolder(String bucket, String folderName, Integer priority) {
  309. }
  310. @Override
  311. public void restoreFile(String bucket, String objectName, Integer priority) {
  312. }
  313. @Override
  314. public Long getSpace(String bucket, String key) {
  315. return null;
  316. }
  317. }