update 优化 OSS 模块与上传组件 异常处理
| | |
| | | import com.ruoyi.oss.exception.OssException; |
| | | import com.ruoyi.oss.properties.CloudStorageProperties; |
| | | import com.ruoyi.oss.service.ICloudStorageStrategy; |
| | | import com.ruoyi.oss.service.abstractd.AbstractCloudStorageStrategy; |
| | | |
| | | import java.util.Date; |
| | | import java.util.Map; |
| | |
| | | return service; |
| | | } |
| | | // 获取redis配置信息 创建对象 并缓存 |
| | | service = (ICloudStorageStrategy) ReflectUtils.newInstance(CloudServiceEnumd.getServiceClass(type), properties); |
| | | service = (ICloudStorageStrategy) ReflectUtils.newInstance(CloudServiceEnumd.getServiceClass(type)); |
| | | ((AbstractCloudStorageStrategy)service).init(properties); |
| | | SERVICES.put(type, service); |
| | | SERVICES_UPDATE_TIME.put(type, nowDate); |
| | | return service; |
| | |
| | | |
| | | protected CloudStorageProperties properties; |
| | | |
| | | public abstract void init(CloudStorageProperties properties); |
| | | |
| | | @Override |
| | | public abstract void createBucket(); |
| | | |
| | |
| | | */ |
| | | public class AliyunCloudStorageStrategy extends AbstractCloudStorageStrategy { |
| | | |
| | | private final OSSClient client; |
| | | private OSSClient client; |
| | | |
| | | public AliyunCloudStorageStrategy(CloudStorageProperties cloudStorageProperties) { |
| | | @Override |
| | | public void init(CloudStorageProperties cloudStorageProperties) { |
| | | properties = cloudStorageProperties; |
| | | try { |
| | | ClientConfiguration configuration = new ClientConfiguration(); |
| | |
| | | client = new OSSClient(properties.getEndpoint(), credentialProvider, configuration); |
| | | createBucket(); |
| | | } catch (Exception e) { |
| | | throw new IllegalArgumentException("阿里云存储配置错误! 请检查系统配置!"); |
| | | throw new OssException("阿里云存储配置错误! 请检查系统配置!"); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | public class MinioCloudStorageStrategy extends AbstractCloudStorageStrategy { |
| | | |
| | | private final MinioClient minioClient; |
| | | private MinioClient minioClient; |
| | | |
| | | public MinioCloudStorageStrategy(CloudStorageProperties cloudStorageProperties) { |
| | | @Override |
| | | public void init(CloudStorageProperties cloudStorageProperties) { |
| | | properties = cloudStorageProperties; |
| | | try { |
| | | minioClient = MinioClient.builder() |
| | |
| | | .build(); |
| | | createBucket(); |
| | | } catch (Exception e) { |
| | | throw new IllegalArgumentException("Minio存储配置错误! 请检查系统配置!"); |
| | | throw new OssException("Minio存储配置错误! 请检查系统配置!"); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | public class QcloudCloudStorageStrategy extends AbstractCloudStorageStrategy { |
| | | |
| | | private final COSClient client; |
| | | private COSClient client; |
| | | |
| | | public QcloudCloudStorageStrategy(CloudStorageProperties cloudStorageProperties) { |
| | | @Override |
| | | public void init(CloudStorageProperties cloudStorageProperties) { |
| | | properties = cloudStorageProperties; |
| | | try { |
| | | COSCredentials credentials = new BasicCOSCredentials( |
| | |
| | | client = new COSClient(credentials, clientConfig); |
| | | createBucket(); |
| | | } catch (Exception e) { |
| | | throw new IllegalArgumentException("腾讯云存储配置错误! 请检查系统配置!"); |
| | | throw new OssException("腾讯云存储配置错误! 请检查系统配置!"); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | public class QiniuCloudStorageStrategy extends AbstractCloudStorageStrategy { |
| | | |
| | | private final UploadManager uploadManager; |
| | | private final BucketManager bucketManager; |
| | | private final String token; |
| | | private UploadManager uploadManager; |
| | | private BucketManager bucketManager; |
| | | private String token; |
| | | |
| | | public QiniuCloudStorageStrategy(CloudStorageProperties cloudStorageProperties) { |
| | | @Override |
| | | public void init(CloudStorageProperties cloudStorageProperties) { |
| | | properties = cloudStorageProperties; |
| | | try { |
| | | Configuration config = new Configuration(getRegion(properties.getRegion())); |
| | |
| | | bucketManager.createBucket(bucketName, properties.getRegion()); |
| | | } |
| | | } catch (Exception e) { |
| | | throw new IllegalArgumentException("七牛云存储配置错误! 请检查系统配置!"); |
| | | throw new OssException("七牛云存储配置错误! 请检查系统配置!"); |
| | | } |
| | | } |
| | | |
| | |
| | | // 调整光标到最后 |
| | | quill.setSelection(length + 1); |
| | | } else { |
| | | this.$message.error("图片插入失败"); |
| | | this.$message.error(res.msg); |
| | | } |
| | | }, |
| | | handleUploadError() { |
| | |
| | | }, |
| | | // 上传成功回调 |
| | | handleUploadSuccess(res, file) { |
| | | this.$message.success("上传成功"); |
| | | this.fileList.push({ name: res.data.fileName, url: res.data.fileName }); |
| | | this.$emit("input", this.listToString(this.fileList)); |
| | | if (res.code === 200) { |
| | | this.$message.success("上传成功"); |
| | | this.fileList.push({ name: res.data.fileName, url: res.data.fileName }); |
| | | this.$emit("input", this.listToString(this.fileList)); |
| | | } else { |
| | | this.$message.error(res.msg); |
| | | this.loading.close(); |
| | | } |
| | | }, |
| | | // 删除文件 |
| | | handleDelete(index) { |
| | |
| | | for (let i in list) { |
| | | strs += list[i].url + separator; |
| | | } |
| | | return strs != '' ? strs.substr(0, strs.length - 1) : ''; |
| | | } |
| | | } |
| | | return strs != "" ? strs.substr(0, strs.length - 1) : ""; |
| | | }, |
| | | }, |
| | | }; |
| | | </script> |
| | | |
| | |
| | | }, |
| | | // 上传成功回调 |
| | | handleUploadSuccess(res) { |
| | | this.fileList.push({ name: res.data.fileName, url: res.data.url }); |
| | | this.$emit("input", this.listToString(this.fileList)); |
| | | this.loading.close(); |
| | | if (res.code == 200) { |
| | | this.fileList.push({ name: res.data.fileName, url: res.data.url }); |
| | | this.$emit("input", this.listToString(this.fileList)); |
| | | this.loading.close(); |
| | | } else { |
| | | this.$message.error(res.msg); |
| | | this.loading.close(); |
| | | } |
| | | }, |
| | | // 上传前loading加载 |
| | | handleBeforeUpload(file) { |
| | |
| | | if (file.name.lastIndexOf(".") > -1) { |
| | | fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1); |
| | | } |
| | | isImg = this.fileType.some(type => { |
| | | isImg = this.fileType.some((type) => { |
| | | if (file.type.indexOf(type) > -1) return true; |
| | | if (fileExtension && fileExtension.indexOf(type) > -1) return true; |
| | | return false; |
| | |
| | | this.$message.error(`上传文件数量不能超过 ${this.limit} 个!`); |
| | | }, |
| | | // 上传失败 |
| | | handleUploadError() { |
| | | handleUploadError(res) { |
| | | this.$message({ |
| | | type: "error", |
| | | message: "上传失败", |
| | |
| | | for (let i in list) { |
| | | strs += list[i].url + separator; |
| | | } |
| | | return strs != '' ? strs.substr(0, strs.length - 1) : ''; |
| | | } |
| | | } |
| | | return strs != "" ? strs.substr(0, strs.length - 1) : ""; |
| | | }, |
| | | }, |
| | | }; |
| | | </script> |
| | | <style scoped lang="scss"> |
| | | // .el-upload--picture-card 控制加号部分 |
| | | ::v-deep.hide .el-upload--picture-card { |
| | | display: none; |
| | | display: none; |
| | | } |
| | | // 去掉动画效果 |
| | | ::v-deep .el-list-enter-active, |
| | | ::v-deep .el-list-leave-active { |
| | | transition: all 0s; |
| | | transition: all 0s; |
| | | } |
| | | |
| | | ::v-deep .el-list-enter, .el-list-leave-active { |
| | | opacity: 0; |
| | | transform: translateY(0); |
| | | ::v-deep .el-list-enter, |
| | | .el-list-leave-active { |
| | | opacity: 0; |
| | | transform: translateY(0); |
| | | } |
| | | </style> |
| | | |
| | |
| | | <el-input v-model="form.accessKey" placeholder="请输入accessKey" /> |
| | | </el-form-item> |
| | | <el-form-item label="secretKey" prop="secretKey"> |
| | | <el-input v-model="form.secretKey" placeholder="请输入秘钥" /> |
| | | <el-input v-model="form.secretKey" placeholder="请输入秘钥" show-password /> |
| | | </el-form-item> |
| | | <el-form-item label="桶名称" prop="bucketName"> |
| | | <el-input v-model="form.bucketName" placeholder="请输入桶名称" /> |