package org.mvnsearch.ali.oss.spring.services;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * OSS URI, format as oss://bucket/path/file.txt
 *
 * @author linux_china
 */
public class OSSUri {
    /**
     * oss protocol
     */
    public static String PROTOCOL = "oss://";
    /**
     * bucket name
     */
    private String bucket;
    /**
     * file path
     */
    private String filePath;

    /**
     * construct method
     */
    public OSSUri() {

    }

    /**
     * construct method
     *
     * @param bucket   bucket name
     * @param filePath file path
     */
    public OSSUri(String bucket, String filePath) {
        setBucket(bucket);
        setFilePath(filePath);
    }

    /**
     * construct method
     *
     * @param uri oss uri
     */
    public OSSUri(String uri) {
        if (uri.startsWith("oss://")) {
            String temp = uri.replace(PROTOCOL, "");
            String[] parts = temp.split("/", 2);
            if (parts.length > 0) {
                this.bucket = parts[0];
            }
            if (parts.length > 1) {
                this.filePath = parts[1];
            }
        }
    }

    public String getBucket() {
        return bucket;
    }

    public void setBucket(@NotNull String bucket) {
        this.bucket = bucket;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(@Nullable String filePath) {
        this.filePath = filePath == null ? "" : filePath;
        if (this.filePath.startsWith("/")) {
            this.filePath = this.filePath.substring(1, this.filePath.length());
        }
    }

    /**
     * get file path
     *
     * @return file path, if absent, empty string will be returned
     */
    @NotNull
    public String getPath() {
        if (filePath == null || filePath.isEmpty() || !filePath.contains("/")) {
            return "";
        } else {
            return filePath.substring(0, filePath.lastIndexOf("/"));
        }
    }

    /**
     * get file name
     *
     * @return file name
     */
    @Nullable
    public String getFileName() {
        if (isDirectory()) {
            return null;
        }
        if (filePath.contains("/")) {
            return filePath.substring(filePath.lastIndexOf("/") + 1);
        } else {
            return filePath;
        }
    }

    /**
     * 判断是否为目录
     *
     * @return 目录标识
     */
    public boolean isDirectory() {
        return filePath == null || filePath.isEmpty() || filePath.endsWith("/");
    }

    /**
     * 是否是合法的URI
     *
     * @return 合法标识
     */
    public boolean isValid() {
        return bucket != null;
    }

    /**
     * oss uri
     *
     * @return oss uri
     */
    public String toString() {
        if (filePath != null && !filePath.isEmpty()) {
            return PROTOCOL + bucket + "/" + filePath;
        } else {
            return PROTOCOL + bucket;
        }
    }

    /**
     * 获取HTTP URL
     *
     * @return http url
     */
    public String getHttpUrl() {
        return toString().replaceAll("oss://", "http://storage.aliyun.com/");
    }
}

@@ -797,33 +797,56 @@
 aram uri oss uri
+%EF%BC%8C%E7%9B%AE%E5%89%8D%E6%94%AF%E6%8C%81oss://%E5%92%8Chttp://%E4%B8%A4%E7%A7%8D%E6%96%B9%E5%BC%8F
 %0A
-
      */%0A    publ
@@ -865,24 +865,59 @@
 ring uri) %7B%0A
+        String relativePath = uri;%0A
         if (
@@ -931,24 +931,24 @@
 rtsWith(
-%22oss://%22
+PROTOCOL
 )) %7B%0A   
@@ -960,19 +960,20 @@
     
-String temp
+relativePath
  = u
@@ -998,24 +998,279 @@
 L, %22%22);%0A    
+    %7D else if (uri.startsWith(%22http://storage.aliyun.com/%22)) %7B%0A            relativePath = uri.replace(%22http://storage.aliyun.com/%22, %22%22);%0A        %7D%0A        if (relativePath.startsWith(%22/%22)) %7B%0A            relativePath = relativePath.substring(1);%0A        %7D%0A
         Stri
@@ -1286,12 +1286,20 @@
 s = 
-temp
+relativePath
 .spl
@@ -1310,36 +1310,32 @@
 /%22, 2);%0A        
-    
 if (parts.length
@@ -1338,28 +1338,24 @@
 ngth %3E 0) %7B%0A
-    
             
@@ -1382,34 +1382,26 @@
 0%5D;%0A        
-   
- 
 %7D%0A
-    
         if (
@@ -1424,36 +1424,32 @@
 ) %7B%0A            
-    
 this.filePath = 
@@ -1462,22 +1462,8 @@
 1%5D;%0A
-            %7D%0A
     

添加对http url解析的支持