Search in sources :

Example 1 with UpException

use of com.upyun.UpException 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 2 with UpException

use of com.upyun.UpException 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 3 with UpException

use of com.upyun.UpException in project oss-spring-boot-starter by ArtIsLong.

the class UpOssClient method downLoad.

@Override
public void downLoad(OutputStream os, String targetName) {
    try {
        Response response = restManager.readFile(getKey(targetName, true));
        IoUtil.copy(response.body().byteStream(), os);
    } catch (IOException | UpException e) {
        log.error("{}下载失败", targetName, e);
        throw new OssException(e);
    }
}
Also used : Response(okhttp3.Response) UpException(com.upyun.UpException) OssException(io.github.artislong.exception.OssException)

Example 4 with UpException

use of com.upyun.UpException in project halo by halo-dev.

the class UpOssFileHandler method delete.

@Override
public void delete(String key) {
    Assert.notNull(key, "File key must not be blank");
    // Get config
    String password = optionService.getByPropertyOfNonNull(UpOssProperties.OSS_PASSWORD).toString();
    String bucket = optionService.getByPropertyOfNonNull(UpOssProperties.OSS_BUCKET).toString();
    String operator = optionService.getByPropertyOfNonNull(UpOssProperties.OSS_OPERATOR).toString();
    RestManager manager = new RestManager(bucket, operator, password);
    manager.setTimeout(60 * 10);
    manager.setApiDomain(RestManager.ED_AUTO);
    try {
        Response result = manager.deleteFile(key, null);
        if (!result.isSuccessful()) {
            log.warn("附件 " + key + " 从又拍云删除失败");
            throw new FileOperationException("附件 " + key + " 从又拍云删除失败");
        }
    } catch (IOException | UpException e) {
        e.printStackTrace();
        throw new FileOperationException("附件 " + key + " 从又拍云删除失败", e);
    }
}
Also used : Response(okhttp3.Response) UpException(com.upyun.UpException) FileOperationException(run.halo.app.exception.FileOperationException) RestManager(com.upyun.RestManager) IOException(java.io.IOException)

Example 5 with UpException

use of com.upyun.UpException in project halo-plugin-experimental by guqing.

the class UpOssFileHandler method delete.

@Override
public void delete(String key) {
    Assert.notNull(key, "File key must not be blank");
    // Get config
    String password = optionService.getByPropertyOfNonNull(UpOssProperties.OSS_PASSWORD).toString();
    String bucket = optionService.getByPropertyOfNonNull(UpOssProperties.OSS_BUCKET).toString();
    String operator = optionService.getByPropertyOfNonNull(UpOssProperties.OSS_OPERATOR).toString();
    RestManager manager = new RestManager(bucket, operator, password);
    manager.setTimeout(60 * 10);
    manager.setApiDomain(RestManager.ED_AUTO);
    try {
        Response result = manager.deleteFile(key, null);
        if (!result.isSuccessful()) {
            log.warn("附件 " + key + " 从又拍云删除失败");
            throw new FileOperationException("附件 " + key + " 从又拍云删除失败");
        }
    } catch (IOException | UpException e) {
        e.printStackTrace();
        throw new FileOperationException("附件 " + key + " 从又拍云删除失败", e);
    }
}
Also used : Response(okhttp3.Response) UpException(com.upyun.UpException) FileOperationException(run.halo.app.exception.FileOperationException) RestManager(com.upyun.RestManager) IOException(java.io.IOException)

Aggregations

UpException (com.upyun.UpException)8 Response (okhttp3.Response)7 RestManager (com.upyun.RestManager)5 OssException (io.github.artislong.exception.OssException)4 DirectoryOssInfo (io.github.artislong.model.DirectoryOssInfo)3 IOException (java.io.IOException)3 FileOperationException (run.halo.app.exception.FileOperationException)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 ParallelUploader (com.upyun.ParallelUploader)2 StandardOssClient (io.github.artislong.core.StandardOssClient)2 UpConstant (io.github.artislong.core.up.constant.UpConstant)2 UpOssConfig (io.github.artislong.core.up.model.UpOssConfig)2