123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package com.fdkankan.fyun.local;
- import cn.hutool.core.io.FileUtil;
- import com.fdkankan.fyun.constant.FYunConstants;
- import com.fdkankan.fyun.constant.FYunTypeEnum;
- import com.fdkankan.fyun.face.AbstractFYunFileService;
- import com.fdkankan.fyun.local.constant.LocalConstants;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
- import org.springframework.stereotype.Component;
- import org.springframework.util.ObjectUtils;
- import java.io.File;
- import java.io.IOException;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- @Component
- @ConditionalOnProperty(name = "fyun.type", havingValue = "local")
- public class LocalFileService extends AbstractFYunFileService {
- private Logger log = LoggerFactory.getLogger(this.getClass().getName());
- @Override
- public String uploadFile(String bucket, byte[] data, String remoteFilePath) {
- FileUtil.writeBytes(data, getOssPath(bucket, remoteFilePath));
- return null;
- }
- @Override
- public String uploadFile(String bucket, String filePath, String remoteFilePath) {
- return uploadFile(bucket, filePath, remoteFilePath, null);
- }
- @Override
- public String uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) {
- FileUtil.copy(filePath, getOssPath(bucket, remoteFilePath), true);
- return null;
- }
- @Override
- public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) {
- try {
- String optType = new File(filePath).isDirectory() ? "folder" : "file";
- String command = String.format(FYunConstants.UPLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.LOCAL.code(), optType);
- log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
- callshell(command);
- } catch (Exception e) {
- log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
- e.printStackTrace();
- }
- return null;
- }
- @Override
- public void downloadFileByCommand(String bucket, String filePath, String remoteFilePath) {
- try {
- String optType = new File(filePath).isDirectory() ? "folder" : "file";
- String command = String.format(FYunConstants.DOWNLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.LOCAL.code(), optType);
- log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
- callshell(command);
- } catch (Exception e) {
- log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
- e.printStackTrace();
- }
- }
- @Override
- public void deleteFile(String bucket, String remoteFilePath) throws IOException {
- FileUtil.del(getOssPath(bucket, remoteFilePath));
- }
- @Override
- public void deleteFolder(String bucket, String remoteFolderPath) {
- FileUtil.del(getOssPath(bucket, remoteFolderPath));
- }
- @Override
- public void uploadMulFiles(String bucket, Map<String, String> filepaths) {
- try {
- for (Map.Entry<String, String> entry : filepaths.entrySet()) {
- uploadFile(bucket, entry.getKey(), entry.getValue(), null);
- }
- } catch (Exception e) {
- log.error("OSS批量上传文件失败!");
- }
- }
- @Override
- public List<String> listRemoteFiles(String bucket, String sourcePath) {
- return listRemoteFiles(bucket, sourcePath, true);
- }
- private List<String> listRemoteFiles(String bucket, String sourcePath, Boolean shutdown) {
- return FileUtil.loopFiles(getOssPath(bucket, sourcePath)).stream()
- .map(f -> f.getAbsolutePath().replace(LocalConstants.BASE_PATH.concat(bucket).concat(File.separator), ""))
- .collect(Collectors.toList());
- }
- @Override
- public void copyFileBetweenBucket(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
- FileUtil.copyContent(new File(getOssPath(sourceBucketName, sourcePath)), new File(getOssPath(targetBucketName, targetPath)), true);
- }
- @Override
- public void copyFilesBetweenBucket(String sourceBucketName, String targetBucketName, Map<String, String> pathMap) {
- if (ObjectUtils.isEmpty(pathMap)) {
- return;
- }
- try {
- for (Map.Entry<String, String> entry : pathMap.entrySet()) {
- copyFileBetweenBucket(sourceBucketName, entry.getKey(), targetBucketName, entry.getValue());
- }
- } catch (Exception e) {
- log.error("批量复制文件失败!");
- }
- }
- @Override
- public String getFileContent(String bucketName, String remoteFilePath) {
- return FileUtil.readUtf8String(getOssPath(bucketName, remoteFilePath));
- }
- @Override
- public boolean fileExist(String bucket, String objectName) {
- return FileUtil.exist(getOssPath(bucket, objectName));
- }
- @Override
- public void downloadFile(String bucket, String remoteFilePath, String localPath) {
- FileUtil.copy(getOssPath(bucket, remoteFilePath), localPath, true);
- }
- private String getOssPath(String bucket, String filePath) {
- return LocalConstants.BASE_PATH.concat(bucket).concat(File.separator).concat(filePath);
- }
- }
|