Bladeren bron

修改文件上传逻辑

tianboguang 2 jaren geleden
bovenliggende
commit
d69899bc70

+ 162 - 29
4dkankan-utils-fyun-https/src/main/java/com/fdkankan/fyun/http/HttpFileService.java

@@ -41,12 +41,15 @@ public class HttpFileService extends AbstractFYunFileService {
 
     @Override
     public String uploadFile(String bucket, byte[] data, String remoteFilePath) {
+        log.info("fyun http utils upload file by bytes,bucket:{},remoteFilePath:{}", bucket, remoteFilePath);
         try {
             // 先将文件保存至本地
-            String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/")+1);
-            FileUtils.writeFile(httpFyunConfig.getLocalTempPath(), fileName, data);
-            uploadFile(bucket,httpFyunConfig.getLocalTempPath() + fileName,remoteFilePath,null);
-            FileUtils.deleteFile(httpFyunConfig.getLocalTempPath() + fileName);
+            String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/") + 1);
+            String localFilePath = nasBasePath + httpFyunConfig.getLocalTempPath() + fileName;
+            log.info("local temp file path:{}", localFilePath);
+            FileUtil.writeBytes(data, localFilePath);
+            uploadFile(bucket, localFilePath, remoteFilePath, null);
+            // FileUtils.deleteFile(nasBasePath + httpFyunConfig.getLocalTempPath() + fileName);
         } catch (Exception e) {
             log.error("oss上传文件失败", e);
             e.printStackTrace();
@@ -56,17 +59,20 @@ public class HttpFileService extends AbstractFYunFileService {
 
     @Override
     public String uploadFile(String bucket, String filePath, String remoteFilePath) {
+        log.info("fyun http utils upload file by filePath,bucket:{},filePath:{},remoteFilePath:{}", bucket, filePath, remoteFilePath);
         return uploadFile(bucket, filePath, remoteFilePath, null);
     }
 
     @Override
     public String uploadFile(String bucket, InputStream inputStream, String remoteFilePath) {
+        log.info("fyun http utils upload file by stream,bucket:{},remoteFilePath:{}", bucket, remoteFilePath);
         try {
             // 先将文件保存至本地
             String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/") + 1);
-            FileUtil.writeFromStream(inputStream, httpFyunConfig.getLocalTempPath() + fileName);
-            uploadFile(bucket,httpFyunConfig.getLocalTempPath() + fileName,remoteFilePath,null);
-            FileUtils.deleteFile(httpFyunConfig.getLocalTempPath() + fileName);
+            String localFilePath = nasBasePath + httpFyunConfig.getLocalTempPath() + fileName;
+            FileUtil.writeFromStream(inputStream, localFilePath);
+            uploadFile(bucket, localFilePath, remoteFilePath, null);
+            // FileUtils.deleteFile(nasBasePath + httpFyunConfig.getLocalTempPath() + fileName);
         } catch (Exception e) {
             log.error("oss上传文件失败", e);
             e.printStackTrace();
@@ -76,22 +82,39 @@ public class HttpFileService extends AbstractFYunFileService {
 
     @Override
     public String uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) {
+        log.info("Fyun httpUtils upload File by filePath with headers , bucket:{},filePath:{},remoteFilePath:{},headers:{}",
+                bucket, filePath, remoteFilePath, headers == null ? "{}" : JSONObject.toJSONString(headers));
         try {
+
             File file = new File(filePath);
             if (!file.exists()) {
                 log.error("要上传的文件不存在:" + filePath);
                 return null;
             }
+
+            if (filePath.startsWith(nasBasePath)) {
+                filePath = filePath.replace(nasBasePath, "");
+            }
+
+            if (!remoteFilePath.startsWith(File.separator)) {
+                remoteFilePath = File.separator.concat(remoteFilePath);
+            }
+
             Map<String, Object> params = new HashMap<>();
             params.put("appName", fYunFileConfig.getKey());
             params.put("secret", fYunFileConfig.getSecret());
             params.put("fileName", filePath);
             params.put("targetPath", remoteFilePath);
+
             String url = fYunFileConfig.getEndPoint() + httpFyunConfig.getUploadFile();
+
+            log.info("url:{},params:{}", url, JSONObject.toJSONString(params));
+
             ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
             log.info("Fyun Http Utils upload,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
             if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
                 log.error("Fyun Http Utils upload failed!");
+                return null;
             }
             log.info("文件上传成功,path:{}", filePath);
         } catch (Exception e) {
@@ -103,13 +126,36 @@ public class HttpFileService extends AbstractFYunFileService {
 
     @Override
     public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) {
+
+        log.info("uploadFileByCommand,bucket:{},filePath:{},remoteFilePath:{}", bucket, filePath, remoteFilePath);
+
         // 上传文件夹
         Map<String, Object> params = new HashMap<>();
         params.put("appName", fYunFileConfig.getKey());
         params.put("secret", fYunFileConfig.getSecret());
+
+        if (filePath.startsWith(nasBasePath)) {
+            filePath = filePath.replace(nasBasePath, "");
+        }
+
+        if (filePath.endsWith(File.separator)) {
+            filePath = filePath.substring(0, remoteFilePath.length() - 1);
+        }
+
+        if (!remoteFilePath.startsWith(File.separator)) {
+            remoteFilePath = File.separator.concat(remoteFilePath);
+        }
+
+        if (remoteFilePath.endsWith(File.separator)) {
+            remoteFilePath = remoteFilePath.substring(0, remoteFilePath.length() - 1);
+        }
+
         params.put("dirName", filePath);
         params.put("targetPath", remoteFilePath);
         String url = fYunFileConfig.getEndPoint() + httpFyunConfig.getUploadDir();
+
+        log.info("url:{},params:{}", url, JSONObject.toJSONString(params));
+
         ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
         log.info("Fyun Http Utils upload folder,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
         if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
@@ -120,24 +166,34 @@ public class HttpFileService extends AbstractFYunFileService {
 
     @Override
     public void downloadFileByCommand(String bucket, String localPath, String remoteFilePath) {
+        log.info("Fyun http utils download folder by command ,bucket:{},localPath:{},remoteFilePath:{}", bucket, localPath, remoteFilePath);
         // 下载文件夹
-        File localFile = new File(localPath);
-        if(localFile.isDirectory()){
-            if(!localFile.exists()){
-                localFile.mkdirs();
-            }
-            String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/")+1);
-            log.info("未配置文件名,使用默认文件名:{}",fileName);
-            localPath = localPath.concat(File.separator).concat(fileName);
-        }else if(!localFile.getParentFile().exists()){
-            localFile.getParentFile().mkdirs();
-        }
         Map<String, Object> params = new HashMap<>();
         params.put("appName", fYunFileConfig.getKey());
         params.put("secret", fYunFileConfig.getSecret());
-        params.put("fileName", remoteFilePath);
+        if (remoteFilePath.endsWith(File.separator)) {
+            remoteFilePath = remoteFilePath.substring(0, remoteFilePath.length() - 1);
+        }
+
+        if (!remoteFilePath.startsWith(File.separator)) {
+            remoteFilePath = File.separator + remoteFilePath;
+        }
+
+        params.put("dirName", remoteFilePath);
+
+        if (localPath.startsWith(nasBasePath)) {
+            localPath = localPath.replace(nasBasePath, "");
+        }
+
+        if (localPath.endsWith(File.separator)) {
+            localPath = localPath.substring(0, localPath.length() - 1);
+        }
+
         params.put("targetPath", localPath);
         String url = fYunFileConfig.getEndPoint() + httpFyunConfig.getDownloadDir();
+
+        log.info("url:{},params:{}", url, JSONObject.toJSONString(params));
+
         ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
         log.info("Fyun Http Utils download folder,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
         if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
@@ -148,11 +204,22 @@ public class HttpFileService extends AbstractFYunFileService {
     @Override
     public void deleteFile(String bucket, String remoteFilePath) throws IOException {
         try {
+
+            log.info("deleteFile,bucket:{},remoteFilePath:{}", bucket, remoteFilePath);
+
             Map<String, Object> params = new HashMap<>();
             params.put("appName", fYunFileConfig.getKey());
             params.put("secret", fYunFileConfig.getSecret());
+
+            if (!remoteFilePath.startsWith(File.separator)) {
+                remoteFilePath = File.separator.concat(remoteFilePath);
+            }
+
             params.put("fileName", remoteFilePath);
             String url = fYunFileConfig.getEndPoint() + httpFyunConfig.getDeleteFile();
+
+            log.info("url:{},params:{}", url, JSONObject.toJSONString(params));
+
             ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
             log.info("Fyun Http Utils delete file,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
             if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
@@ -168,15 +235,27 @@ public class HttpFileService extends AbstractFYunFileService {
     @Override
     public void deleteFolder(String bucket, String remoteFolderPath) {
         try {
+
+            log.info("delete folder,bucket:{},remoteFilePath:{}", bucket, remoteFolderPath);
+
             Map<String, Object> params = new HashMap<>();
             params.put("appName", fYunFileConfig.getKey());
             params.put("secret", fYunFileConfig.getSecret());
+
+            if (!remoteFolderPath.startsWith(File.separator)) {
+                remoteFolderPath = File.separator.concat(remoteFolderPath);
+            }
+
             params.put("dirName", remoteFolderPath);
             String url = fYunFileConfig.getEndPoint() + httpFyunConfig.getDeleteDir();
+
+            log.info("url:{},params:{}", url, JSONObject.toJSONString(params));
+
             ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
             log.info("Fyun Http Utils delete folder,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
             if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
                 log.error("Fyun Http Utils delete folder failed!");
+                return;
             }
             log.info("文件夹删除成功,path:{}", remoteFolderPath);
         } catch (Exception e) {
@@ -204,22 +283,46 @@ public class HttpFileService extends AbstractFYunFileService {
     @Override
     public void copyFileBetweenBucket(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
         try {
+
+            log.info("copy file between bucket,sourceBucket:{},sourcePath:{},targetBucketName,{},targetPath:{}", sourceBucketName, sourcePath, targetBucketName, targetPath);
+
             Map<String, Object> params = new HashMap<>();
             params.put("appName", fYunFileConfig.getKey());
             params.put("secret", fYunFileConfig.getSecret());
+
+            if (!targetPath.startsWith(File.separator)) {
+                targetPath = File.separator.concat(targetPath);
+            }
+
+            if (targetPath.endsWith(File.separator)) {
+                targetPath = targetPath.substring(0, targetPath.length() - 1);
+            }
+
             params.put("targetDir", targetPath);
             String url;
-            if(new File(sourcePath).isDirectory()){
+
+            if (!sourcePath.startsWith(File.separator)) {
+                sourcePath = File.separator.concat(sourcePath);
+            }
+
+            if (new File(sourcePath).isDirectory()) {
+                if (sourcePath.endsWith(File.separator)) {
+                    sourcePath = sourcePath.substring(0, sourcePath.length() - 1);
+                }
                 params.put("dirName", sourcePath);
                 url = fYunFileConfig.getEndPoint() + httpFyunConfig.getCopyDir();
-            }else{
+            } else {
                 params.put("fileName", sourcePath);
                 url = fYunFileConfig.getEndPoint() + httpFyunConfig.getCopyFile();
             }
+
+            log.info("url:{},params:{}", url, JSONObject.toJSONString(params));
+
             ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
             log.info("Fyun Http Utils copy file or dir,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
             if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
                 log.error("Fyun Http Utils copy file or dir failed!");
+                return;
             }
             log.info("文件拷贝成功,path:{}", targetPath);
         } catch (Exception e) {
@@ -229,6 +332,7 @@ public class HttpFileService extends AbstractFYunFileService {
 
     @Override
     public void copyFilesBetweenBucket(String sourceBucketName, String targetBucketName, Map<String, String> pathMap) {
+        log.info("copy files between bucket,sourceBucket:{},targetBucketName:{},pathMaps:{}", sourceBucketName, targetBucketName, JSONObject.toJSONString(pathMap));
         if (ObjectUtils.isEmpty(pathMap)) {
             return;
         }
@@ -244,12 +348,17 @@ public class HttpFileService extends AbstractFYunFileService {
     @Override
     public String getFileContent(String bucketName, String remoteFilePath) {
         try {
-            log.info("获取文件内容:{}",remoteFilePath);
+            log.info("获取文件内容:{}", remoteFilePath);
+
+            if (!remoteFilePath.startsWith(File.separator)) {
+                remoteFilePath = File.separator.concat(remoteFilePath);
+            }
+
             // 先将文件下载到本地
-            String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/")+1);
-            downloadFile(remoteFilePath, httpFyunConfig.getLocalTempPath() + fileName);
-            String content =FileUtils.readFile(nasBasePath + httpFyunConfig.getLocalTempPath() + fileName);
-            FileUtils.deleteFile(nasBasePath + httpFyunConfig.getLocalTempPath() + fileName);
+            String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/") + 1);
+            downloadFile(remoteFilePath, nasBasePath + httpFyunConfig.getLocalTempPath() + fileName);
+            String content = FileUtils.readFile(nasBasePath + httpFyunConfig.getLocalTempPath() + fileName);
+            // FileUtils.deleteFile(nasBasePath + httpFyunConfig.getLocalTempPath() + fileName);
             return content;
         } catch (Exception e) {
             log.error("获取文件内容失败:{}", remoteFilePath);
@@ -259,16 +368,26 @@ public class HttpFileService extends AbstractFYunFileService {
 
     @Override
     public boolean fileExist(String bucket, String objectName) {
+
+        log.info("file exist check ,bucket:{},remoteFilePath:{}", bucket, objectName);
+
         try {
             Map<String, Object> params = new HashMap<>();
             params.put("appName", fYunFileConfig.getKey());
             params.put("secret", fYunFileConfig.getSecret());
+
+            if (!objectName.startsWith(File.separator)) {
+                objectName = File.separator.concat(objectName);
+            }
+
             params.put("fileName", objectName);
             String url = fYunFileConfig.getEndPoint() + httpFyunConfig.getFileExist();
+            log.info("url:{},params:{}", url, JSONObject.toJSONString(params));
             ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
             log.info("Fyun Http Utils check file exist,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
             if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {
                 log.error("Fyun Http Utils check file exist failed!");
+                return false;
             }
             return Boolean.parseBoolean(responseEntity.getBody().getData().toString());
         } catch (Exception e) {
@@ -279,22 +398,36 @@ public class HttpFileService extends AbstractFYunFileService {
 
     @Override
     public void downloadFile(String bucket, String remoteFilePath, String localPath) {
+        log.info(" download file ,bucket:{},remoteFilePath:{},localPath:{}", bucket, remoteFilePath, localPath);
         try {
             File localFile = new File(localPath);
             if (!localFile.getParentFile().exists()) {
                 localFile.getParentFile().mkdirs();
             }
-            if(localFile.isDirectory()){
-                String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/")+1);
-                log.info("未配置文件名,使用默认文件名:{}",fileName);
+            if (localFile.isDirectory()) {
+                String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/") + 1);
+                log.info("未配置文件名,使用默认文件名:{}", fileName);
                 localPath = localPath.concat(File.separator).concat(fileName);
             }
             Map<String, Object> params = new HashMap<>();
             params.put("appName", fYunFileConfig.getKey());
             params.put("secret", fYunFileConfig.getSecret());
+
+            if (!remoteFilePath.startsWith(File.separator)) {
+                localPath = File.separator.concat(remoteFilePath);
+            }
+
             params.put("fileName", remoteFilePath);
+
+            if (localPath.startsWith(nasBasePath)) {
+                localPath = localPath.replace(nasBasePath, "");
+            }
+
             params.put("targetPath", localPath);
             String url = fYunFileConfig.getEndPoint() + httpFyunConfig.getDownloadFile();
+
+            log.info("url:{},params:{}", url, JSONObject.toJSONString(params));
+
             ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, params, Result.class);
             log.info("Fyun Http Utils download file,url:{},params:{},结果,{}", url, JSONObject.toJSONString(params), JSONObject.toJSONString(responseEntity.getBody()));
             if (responseEntity.getStatusCode() != HttpStatus.OK || responseEntity.getBody().getCode() != Result.CODE_SUCCESS) {

+ 5 - 5
4dkankan-utils-model/src/main/java/com/fdkankan/model/constants/ConstantCmd.java

@@ -3,7 +3,7 @@ package com.fdkankan.model.constants;
 public class ConstantCmd {
 
 	//生成模型的命令
-	public static final String BUILD_MODEL_COMMAND = "sudo bash /home/ubuntu/bin/Launcher.sh ";
+	public static final String BUILD_MODEL_COMMAND = "bash /home/ubuntu/bin/Launcher.sh ";
 
 	//生成模型的命令
 	public static final String BUILD_MODEL_COMMAND2 = "bash /opt/ossutil/sshoss.sh ";
@@ -11,7 +11,7 @@ public class ConstantCmd {
 	public static final String BUILD_MODEL_OLD_COMMAND = "bash /home/ubuntu/bin_old/Launcher.sh ";
 	public static final String BUILD_MODEL_SFM_COMMAND = "bash /home/ubuntu/run_sfm.sh ";
 
-	public static final String OBJ_TO_TXT = "sudo bash /home/ubuntu/bin_old/obj2txt.sh ";
+	public static final String OBJ_TO_TXT = "bash /home/ubuntu/bin_old/obj2txt.sh ";
 
 	public static final String REBUILD_MODEL_FLLOR = "bash /home/ubuntu/bin/Panoramix_Floorplan.sh ";
 	//切图命令
@@ -33,7 +33,7 @@ public class ConstantCmd {
 	public static final String CREATE_MUTE_VIDEO = "bash /monchickey/ffmpeg/bin/ff_mtue.sh ";
 
 	//将mp4文件转换成flv
-	public static final String MP4_TO_FLV = "sudo bash /monchickey/ffmpeg/bin/ff_mp4TOflv.sh ";
+	public static final String MP4_TO_FLV = "bash /monchickey/ffmpeg/bin/ff_mp4TOflv.sh ";
 
 	public static final String FORMAT_MP4 = "bash /monchickey/ffmpeg/bin/ff_formatMp4.sh ";
 
@@ -54,8 +54,8 @@ public class ConstantCmd {
 	// 修改户型图json文件
 	public static final String TRANSLATE_HOUST_FLOOR = "/opt/Robin/JsonRead.out ";
 
-	public static final String FYUN_DOWNLOAD = "sudo bash /opt/ossutil/fyun-download.sh %s /%s %s %s %s";
+	public static final String FYUN_DOWNLOAD = "bash /opt/ossutil/fyun-download.sh %s /%s %s %s %s";
 
-	public static final String FYUN_UPLOAD = "sudo bash /opt/ossutil/fyun-upload.sh %s %s /%s %s %s";
+	public static final String FYUN_UPLOAD = "bash /opt/ossutil/fyun-upload.sh %s %s /%s %s %s";
 
 }