package com.qianwen.core.oss; import com.qiniu.common.QiniuException; import com.qiniu.common.Zone; import com.qiniu.http.Response; import com.qiniu.storage.BucketManager; import com.qiniu.storage.UploadManager; import com.qiniu.storage.model.FileInfo; import com.qiniu.util.Auth; import com.qiniu.util.StringMap; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.util.List; import com.qianwen.core.oss.model.BladeFile; import com.qianwen.core.oss.model.OssFile; import com.qianwen.core.oss.props.OssProperties; import com.qianwen.core.oss.rule.OssRule; import com.qianwen.core.tool.utils.CollectionUtil; import com.qianwen.core.tool.utils.Func; import org.springframework.web.multipart.MultipartFile; /* loaded from: blade-starter-oss-9.3.0.0-SNAPSHOT.jar:org/springblade/core/oss/QiniuTemplate.class */ public class QiniuTemplate implements OssTemplate { private final Auth auth; private final UploadManager uploadManager; private final BucketManager bucketManager; private final OssProperties ossProperties; private final OssRule ossRule; public QiniuTemplate(final Auth auth, final UploadManager uploadManager, final BucketManager bucketManager, final OssProperties ossProperties, final OssRule ossRule) { this.auth = auth; this.uploadManager = uploadManager; this.bucketManager = bucketManager; this.ossProperties = ossProperties; this.ossRule = ossRule; } @Override // com.qianwen.core.oss.OssTemplate public void makeBucket(String bucketName) { try { if (!CollectionUtil.contains(this.bucketManager.buckets(), getBucketName(bucketName))) { this.bucketManager.createBucket(getBucketName(bucketName), Zone.autoZone().getRegion()); } }catch(QiniuException ex) { throw new RuntimeException(ex); } } @Override // com.qianwen.core.oss.OssTemplate public void removeBucket(String bucketName) { } @Override // com.qianwen.core.oss.OssTemplate public boolean bucketExists(String bucketName) { try { return CollectionUtil.contains(this.bucketManager.buckets(), getBucketName(bucketName)); }catch(QiniuException ex) { throw new RuntimeException(ex); } } @Override // com.qianwen.core.oss.OssTemplate public void copyFile(String bucketName, String fileName, String destBucketName) { try { this.bucketManager.copy(getBucketName(bucketName), fileName, getBucketName(destBucketName), fileName); }catch(QiniuException ex) { throw new RuntimeException(ex); } } @Override // com.qianwen.core.oss.OssTemplate public void copyFile(String bucketName, String fileName, String destBucketName, String destFileName) { try { this.bucketManager.copy(getBucketName(bucketName), fileName, getBucketName(destBucketName), destFileName); }catch(QiniuException ex) { throw new RuntimeException(ex); } } @Override // com.qianwen.core.oss.OssTemplate public OssFile statFile(String fileName) { return statFile(this.ossProperties.getBucketName(), fileName); } @Override // com.qianwen.core.oss.OssTemplate public OssFile statFile(String bucketName, String fileName) { try { FileInfo stat = this.bucketManager.stat(getBucketName(bucketName), fileName); OssFile ossFile = new OssFile(); ossFile.setName(Func.isEmpty(stat.key) ? fileName : stat.key); ossFile.setLink(fileLink(ossFile.getName())); ossFile.setHash(stat.hash); ossFile.setLength(stat.fsize); ossFile.setPutTime(new Date(stat.putTime / 10000)); ossFile.setContentType(stat.mimeType); return ossFile; }catch(QiniuException ex) { throw new RuntimeException(ex); } } @Override // com.qianwen.core.oss.OssTemplate public String filePath(String fileName) { return getBucketName().concat("/").concat(fileName); } @Override // com.qianwen.core.oss.OssTemplate public String filePath(String bucketName, String fileName) { return getBucketName(bucketName).concat("/").concat(fileName); } @Override // com.qianwen.core.oss.OssTemplate public String fileLink(String fileName) { return this.ossProperties.getEndpoint().concat("/").concat(fileName); } @Override // com.qianwen.core.oss.OssTemplate public String fileLink(String bucketName, String fileName) { return this.ossProperties.getEndpoint().concat("/").concat(fileName); } @Override // com.qianwen.core.oss.OssTemplate public InputStream getObject(String bucketName, String fileName, Long offset, Long length) { return get(bucketName, fileName, offset, length); } @Override // com.qianwen.core.oss.OssTemplate public InputStream getObject(String fileName, Long offset, Long length) { return get(this.ossProperties.getBucketName(), fileName, offset, length); } @Override // com.qianwen.core.oss.OssTemplate public InputStream getObject(String bucketName, String fileName) { return get(bucketName, fileName); } @Override // com.qianwen.core.oss.OssTemplate public InputStream getObject(String fileName) { return get(this.ossProperties.getBucketName(), fileName); } @Override // com.qianwen.core.oss.OssTemplate public BladeFile putFile(MultipartFile file) { return putFile(this.ossProperties.getBucketName(), file.getOriginalFilename(), file); } @Override // com.qianwen.core.oss.OssTemplate public BladeFile putFile(String fileName, MultipartFile file) { return putFile(this.ossProperties.getBucketName(), fileName, file); } @Override // com.qianwen.core.oss.OssTemplate public BladeFile putFile(String bucketName, String fileName, MultipartFile file) { try { return putFile(bucketName, fileName, file.getInputStream()); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override // com.qianwen.core.oss.OssTemplate public BladeFile putFile(String fileName, InputStream stream) { return putFile(this.ossProperties.getBucketName(), fileName, stream); } @Override // com.qianwen.core.oss.OssTemplate public BladeFile putFile(String bucketName, String fileName, InputStream stream) { return put(bucketName, stream, fileName, false); } public InputStream get(String bucketName, String fileName) { throw new UnsupportedOperationException(); } public InputStream get(String bucketName, String fileName, Long offset, Long length) { throw new UnsupportedOperationException(); } public BladeFile put(String bucketName, InputStream stream, String key, boolean cover) { makeBucket(bucketName); String key2 = getFileName(key); try { if (cover) { this.uploadManager.put(stream, key2, getUploadToken(bucketName, key2), (StringMap) null, (String) null); } else { Response response = this.uploadManager.put(stream, key2, getUploadToken(bucketName), (StringMap) null, (String) null); for (int retry = 0; response.needRetry() && retry < 5; retry++) { response = this.uploadManager.put(stream, key2, getUploadToken(bucketName), (StringMap) null, (String) null); } } BladeFile file = new BladeFile(); file.setOriginalName(key); file.setName(key2); file.setDomain(getOssHost()); file.setLink(fileLink(bucketName, key2)); return file; }catch(QiniuException ex) { throw new RuntimeException(ex); } } @Override // com.qianwen.core.oss.OssTemplate public void removeFile(String fileName) { try { this.bucketManager.delete(getBucketName(), fileName); }catch(QiniuException ex) { throw new RuntimeException(ex); } } @Override // com.qianwen.core.oss.OssTemplate public void removeFile(String bucketName, String fileName) { try { this.bucketManager.delete(getBucketName(bucketName), fileName); }catch(QiniuException ex) { throw new RuntimeException(ex); } } @Override // com.qianwen.core.oss.OssTemplate public void removeFiles(List fileNames) { fileNames.forEach(this::removeFile); } @Override // com.qianwen.core.oss.OssTemplate public void removeFiles(String bucketName, List fileNames) { fileNames.forEach(fileName -> { removeFile(getBucketName(bucketName), fileName); }); } private String getBucketName() { return getBucketName(this.ossProperties.getBucketName()); } private String getBucketName(String bucketName) { return this.ossRule.bucketName(bucketName); } private String getFileName(String originalFilename) { return this.ossRule.fileName(originalFilename); } public String getUploadToken(String bucketName) { return this.auth.uploadToken(getBucketName(bucketName)); } private String getUploadToken(String bucketName, String key) { return this.auth.uploadToken(getBucketName(bucketName), key); } public String getOssHost() { return this.ossProperties.getEndpoint(); } }