Search in sources :

Example 1 with OssInfo

use of io.github.artislong.model.OssInfo in project oss-spring-boot-starter by ArtIsLong.

the class UCloudOssClient method getInfo.

@Override
public OssInfo getInfo(String targetName, Boolean isRecursion) {
    String key = getKey(targetName, false);
    OssInfo ossInfo = getBaseInfo(key);
    ossInfo.setName(StrUtil.equals(targetName, StrUtil.SLASH) ? targetName : FileNameUtil.getName(targetName));
    ossInfo.setPath(OssPathUtil.replaceKey(targetName, ossInfo.getName(), true));
    if (isRecursion && isDirectory(key)) {
        String prefix = OssPathUtil.convertPath(key, false);
        ObjectListBean objectListBean;
        try {
            objectListBean = objectApiBuilder.objectList(getBucket()).withPrefix(prefix.endsWith("/") ? prefix : prefix + CharPool.SLASH).execute();
        } catch (Exception e) {
            throw new OssException(e);
        }
        List<ObjectInfoBean> objectList = objectListBean.getObjectList();
        List<OssInfo> fileOssInfos = new ArrayList<>();
        List<OssInfo> directoryInfos = new ArrayList<>();
        for (ObjectInfoBean objectInfoBean : objectList) {
            String fileName = objectInfoBean.getFileName();
            if (FileNameUtil.getName(fileName).equals(FileNameUtil.getName(key))) {
                ossInfo.setLastUpdateTime(DateUtil.date(objectInfoBean.getModifyTime()).toString(DatePattern.NORM_DATETIME_PATTERN));
                ossInfo.setCreateTime(DateUtil.date(objectInfoBean.getCreateTime()).toString(DatePattern.NORM_DATETIME_PATTERN));
                ossInfo.setLength(Convert.toStr(objectInfoBean.getSize()));
            } else if (isDirectory(fileName)) {
                directoryInfos.add(getInfo(OssPathUtil.replaceKey(fileName, getBasePath(), false), true));
            } else {
                fileOssInfos.add(getInfo(OssPathUtil.replaceKey(fileName, getBasePath(), false), false));
            }
        }
        if (ObjectUtil.isNotEmpty(fileOssInfos) && fileOssInfos.get(0) instanceof FileOssInfo) {
            ReflectUtil.setFieldValue(ossInfo, "fileInfos", fileOssInfos);
        }
        if (ObjectUtil.isNotEmpty(directoryInfos) && directoryInfos.get(0) instanceof DirectoryOssInfo) {
            ReflectUtil.setFieldValue(ossInfo, "directoryInfos", directoryInfos);
        }
    }
    return ossInfo;
}
Also used : ObjectInfoBean(cn.ucloud.ufile.bean.ObjectInfoBean) DirectoryOssInfo(io.github.artislong.model.DirectoryOssInfo) FileOssInfo(io.github.artislong.model.FileOssInfo) OssInfo(io.github.artislong.model.OssInfo) DirectoryOssInfo(io.github.artislong.model.DirectoryOssInfo) OssException(io.github.artislong.exception.OssException) UfileClientException(cn.ucloud.ufile.exception.UfileClientException) OssException(io.github.artislong.exception.OssException) ObjectListBean(cn.ucloud.ufile.bean.ObjectListBean) FileOssInfo(io.github.artislong.model.FileOssInfo)

Example 2 with OssInfo

use of io.github.artislong.model.OssInfo in project oss-spring-boot-starter by ArtIsLong.

the class UpOssClient method getDirectoryOssInfo.

private DirectoryOssInfo getDirectoryOssInfo(String key) throws UpException, IOException {
    String name = FileNameUtil.getName(key);
    String newKey = OssPathUtil.replaceKey(key, name, true);
    Response response = restManager.readDirIter(newKey, null);
    DirectoryOssInfo ossInfo = new DirectoryOssInfo();
    IoUtil.readUtf8Lines(response.body().byteStream(), (LineHandler) line -> {
        List<String> fields = StrUtil.split(line, "\t");
        if (name.equals(fields.get(0))) {
            ossInfo.setName(fields.get(0));
            ossInfo.setPath(OssPathUtil.replaceKey(newKey, getBasePath(), true));
            ossInfo.setLength(fields.get(2));
            ossInfo.setCreateTime(DateUtil.date(Convert.toLong(fields.get(3)) * 1000).toString(DatePattern.NORM_DATETIME_PATTERN));
            ossInfo.setLastUpdateTime(DateUtil.date(Convert.toLong(fields.get(3)) * 1000).toString(DatePattern.NORM_DATETIME_PATTERN));
        }
    });
    return ossInfo;
}
Also used : Response(okhttp3.Response) DateUtil(cn.hutool.core.date.DateUtil) RestManager(com.upyun.RestManager) ObjectUtil(cn.hutool.core.util.ObjectUtil) StandardOssClient(io.github.artislong.core.StandardOssClient) HashMap(java.util.HashMap) Headers(okhttp3.Headers) ArrayList(java.util.ArrayList) FileOssInfo(io.github.artislong.model.FileOssInfo) OssInfo(io.github.artislong.model.OssInfo) OssException(io.github.artislong.exception.OssException) Map(java.util.Map) Response(okhttp3.Response) ParallelUploader(com.upyun.ParallelUploader) IoUtil(cn.hutool.core.io.IoUtil) LineHandler(cn.hutool.core.io.LineHandler) OssPathUtil(io.github.artislong.utils.OssPathUtil) FileNameUtil(cn.hutool.core.io.file.FileNameUtil) DirectoryOssInfo(io.github.artislong.model.DirectoryOssInfo) UpOssConfig(io.github.artislong.core.up.model.UpOssConfig) StrUtil(cn.hutool.core.util.StrUtil) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) java.io(java.io) UpException(com.upyun.UpException) ReflectUtil(cn.hutool.core.util.ReflectUtil) Data(lombok.Data) Convert(cn.hutool.core.convert.Convert) UpConstant(io.github.artislong.core.up.constant.UpConstant) AllArgsConstructor(lombok.AllArgsConstructor) DatePattern(cn.hutool.core.date.DatePattern) NoArgsConstructor(lombok.NoArgsConstructor) DirectoryOssInfo(io.github.artislong.model.DirectoryOssInfo) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with OssInfo

use of io.github.artislong.model.OssInfo in project oss-spring-boot-starter by ArtIsLong.

the class UpOssClient method getInfo.

@Override
public OssInfo getInfo(String targetName, Boolean isRecursion) {
    String key = getKey(targetName, true);
    try {
        OssInfo ossInfo = getBaseInfo(key);
        ossInfo.setName(StrUtil.equals(targetName, StrUtil.SLASH) ? targetName : FileNameUtil.getName(targetName));
        ossInfo.setPath(OssPathUtil.replaceKey(targetName, ossInfo.getName(), true));
        if (isRecursion && isDirectory(key)) {
            List<OssInfo> fileOssInfos = new ArrayList<>();
            List<OssInfo> directoryInfos = new ArrayList<>();
            Response response = restManager.readDirIter(key, null);
            IoUtil.readUtf8Lines(response.body().byteStream(), (LineHandler) line -> {
                List<String> fields = StrUtil.split(line, "\t");
                if (UpConstant.FILE_TYPE.equals(fields.get(1))) {
                    fileOssInfos.add(getInfo(OssPathUtil.replaceKey(key + StrUtil.SLASH + fields.get(0), getBasePath(), true), true));
                } else {
                    directoryInfos.add(getInfo(OssPathUtil.replaceKey(key + StrUtil.SLASH + fields.get(0), getBasePath(), true), true));
                }
            });
            if (ObjectUtil.isNotEmpty(fileOssInfos) && fileOssInfos.get(0) instanceof FileOssInfo) {
                ReflectUtil.setFieldValue(ossInfo, "fileInfos", fileOssInfos);
            }
            if (ObjectUtil.isNotEmpty(directoryInfos) && directoryInfos.get(0) instanceof DirectoryOssInfo) {
                ReflectUtil.setFieldValue(ossInfo, "directoryInfos", directoryInfos);
            }
        }
        return ossInfo;
    } catch (IOException | UpException e) {
        log.error("获取{}基本信息失败", targetName, e);
        throw new OssException(e);
    }
}
Also used : DateUtil(cn.hutool.core.date.DateUtil) RestManager(com.upyun.RestManager) ObjectUtil(cn.hutool.core.util.ObjectUtil) StandardOssClient(io.github.artislong.core.StandardOssClient) HashMap(java.util.HashMap) Headers(okhttp3.Headers) ArrayList(java.util.ArrayList) FileOssInfo(io.github.artislong.model.FileOssInfo) OssInfo(io.github.artislong.model.OssInfo) OssException(io.github.artislong.exception.OssException) Map(java.util.Map) Response(okhttp3.Response) ParallelUploader(com.upyun.ParallelUploader) IoUtil(cn.hutool.core.io.IoUtil) LineHandler(cn.hutool.core.io.LineHandler) OssPathUtil(io.github.artislong.utils.OssPathUtil) FileNameUtil(cn.hutool.core.io.file.FileNameUtil) DirectoryOssInfo(io.github.artislong.model.DirectoryOssInfo) UpOssConfig(io.github.artislong.core.up.model.UpOssConfig) StrUtil(cn.hutool.core.util.StrUtil) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) java.io(java.io) UpException(com.upyun.UpException) ReflectUtil(cn.hutool.core.util.ReflectUtil) Data(lombok.Data) Convert(cn.hutool.core.convert.Convert) UpConstant(io.github.artislong.core.up.constant.UpConstant) AllArgsConstructor(lombok.AllArgsConstructor) DatePattern(cn.hutool.core.date.DatePattern) NoArgsConstructor(lombok.NoArgsConstructor) UpException(com.upyun.UpException) ArrayList(java.util.ArrayList) OssException(io.github.artislong.exception.OssException) Response(okhttp3.Response) DirectoryOssInfo(io.github.artislong.model.DirectoryOssInfo) ArrayList(java.util.ArrayList) List(java.util.List) FileOssInfo(io.github.artislong.model.FileOssInfo) OssInfo(io.github.artislong.model.OssInfo) DirectoryOssInfo(io.github.artislong.model.DirectoryOssInfo) FileOssInfo(io.github.artislong.model.FileOssInfo)

Example 4 with OssInfo

use of io.github.artislong.model.OssInfo in project oss-spring-boot-starter by ArtIsLong.

the class JinShanOssClient method getInfo.

@Override
public OssInfo getInfo(String targetName, Boolean isRecursion) {
    String key = getKey(targetName, false);
    OssInfo ossInfo = getBaseInfo(key);
    ossInfo.setName(StrUtil.equals(targetName, StrUtil.SLASH) ? targetName : FileNameUtil.getName(targetName));
    ossInfo.setPath(OssPathUtil.replaceKey(targetName, ossInfo.getName(), true));
    if (isRecursion && isDirectory(key)) {
        String prefix = OssPathUtil.convertPath(key, false);
        ObjectListing listObjects = ks3.listObjects(getBucket(), prefix.endsWith("/") ? prefix : prefix + CharPool.SLASH);
        List<OssInfo> fileOssInfos = new ArrayList<>();
        List<OssInfo> directoryInfos = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(listObjects.getObjectSummaries())) {
            for (Ks3ObjectSummary ks3ObjectSummary : listObjects.getObjectSummaries()) {
                if (FileNameUtil.getName(ks3ObjectSummary.getKey()).equals(FileNameUtil.getName(key))) {
                    ossInfo.setLastUpdateTime(DateUtil.date(ks3ObjectSummary.getLastModified()).toString(DatePattern.NORM_DATETIME_PATTERN));
                    ossInfo.setCreateTime(DateUtil.date(ks3ObjectSummary.getLastModified()).toString(DatePattern.NORM_DATETIME_PATTERN));
                    ossInfo.setLength(Convert.toStr(ks3ObjectSummary.getSize()));
                } else {
                    fileOssInfos.add(getInfo(OssPathUtil.replaceKey(ks3ObjectSummary.getKey(), getBasePath(), false), false));
                }
            }
        }
        if (ObjectUtil.isNotEmpty(listObjects.getCommonPrefixes())) {
            for (String commonPrefix : listObjects.getCommonPrefixes()) {
                String target = OssPathUtil.replaceKey(commonPrefix, getBasePath(), false);
                if (isDirectory(commonPrefix)) {
                    directoryInfos.add(getInfo(target, true));
                } else {
                    fileOssInfos.add(getInfo(target, false));
                }
            }
        }
        if (ObjectUtil.isNotEmpty(fileOssInfos) && fileOssInfos.get(0) instanceof FileOssInfo) {
            ReflectUtil.setFieldValue(ossInfo, "fileInfos", fileOssInfos);
        }
        if (ObjectUtil.isNotEmpty(directoryInfos) && directoryInfos.get(0) instanceof DirectoryOssInfo) {
            ReflectUtil.setFieldValue(ossInfo, "directoryInfos", directoryInfos);
        }
    }
    return ossInfo;
}
Also used : DirectoryOssInfo(io.github.artislong.model.DirectoryOssInfo) FileOssInfo(io.github.artislong.model.FileOssInfo) OssInfo(io.github.artislong.model.OssInfo) DirectoryOssInfo(io.github.artislong.model.DirectoryOssInfo) FileOssInfo(io.github.artislong.model.FileOssInfo)

Example 5 with OssInfo

use of io.github.artislong.model.OssInfo in project oss-spring-boot-starter by ArtIsLong.

the class JinShanOssClient method getBaseInfo.

public OssInfo getBaseInfo(String key) {
    OssInfo ossInfo;
    if (isFile(key)) {
        ossInfo = new FileOssInfo();
        try {
            GetObjectResult objectResult = ks3.getObject(getBucket(), key);
            ObjectMetadata objectMetadata = objectResult.getObject().getObjectMetadata();
            ossInfo.setLastUpdateTime(DateUtil.date(objectMetadata.getLastModified()).toString(DatePattern.NORM_DATETIME_PATTERN));
            ossInfo.setCreateTime(DateUtil.date(objectMetadata.getLastModified()).toString(DatePattern.NORM_DATETIME_PATTERN));
            ossInfo.setLength(Convert.toStr(objectMetadata.getContentLength()));
        } catch (Exception e) {
            log.error("获取{}文件属性失败", key, e);
        }
    } else {
        ossInfo = new DirectoryOssInfo();
    }
    return ossInfo;
}
Also used : DirectoryOssInfo(io.github.artislong.model.DirectoryOssInfo) FileOssInfo(io.github.artislong.model.FileOssInfo) OssInfo(io.github.artislong.model.OssInfo) DirectoryOssInfo(io.github.artislong.model.DirectoryOssInfo) FileOssInfo(io.github.artislong.model.FileOssInfo)

Aggregations

OssInfo (io.github.artislong.model.OssInfo)44 DirectoryOssInfo (io.github.artislong.model.DirectoryOssInfo)41 FileOssInfo (io.github.artislong.model.FileOssInfo)41 OssException (io.github.artislong.exception.OssException)16 ArrayList (java.util.ArrayList)7 Headers (okhttp3.Headers)4 ChannelSftp (com.jcraft.jsch.ChannelSftp)3 Response (okhttp3.Response)3 AmazonClientException (repkg.com.amazonaws.AmazonClientException)3 Convert (cn.hutool.core.convert.Convert)2 DatePattern (cn.hutool.core.date.DatePattern)2 DateUtil (cn.hutool.core.date.DateUtil)2 IoUtil (cn.hutool.core.io.IoUtil)2 LineHandler (cn.hutool.core.io.LineHandler)2 FileNameUtil (cn.hutool.core.io.file.FileNameUtil)2 ObjectUtil (cn.hutool.core.util.ObjectUtil)2 ReflectUtil (cn.hutool.core.util.ReflectUtil)2 StrUtil (cn.hutool.core.util.StrUtil)2 UfileClientException (cn.ucloud.ufile.exception.UfileClientException)2 SftpATTRS (com.jcraft.jsch.SftpATTRS)2