66import com .admin4j .oss .UploadFileService ;
77import com .admin4j .oss .entity .vo .UploadFileVO ;
88import com .amazonaws .services .s3 .model .PutObjectResult ;
9- import lombok .RequiredArgsConstructor ;
109import org .apache .commons .codec .digest .DigestUtils ;
1110import org .apache .commons .lang3 .StringUtils ;
1211import org .springframework .web .multipart .MultipartFile ;
2120 * @author andanyang
2221 * @since 2023/4/14 9:27
2322 */
24- @ RequiredArgsConstructor
23+ // @RequiredArgsConstructor
2524public class SimpleOSSUploadFileService implements UploadFileService {
2625
2726 protected final static DateTimeFormatter FILEPATH_DATETIME_FORMATTER = DateTimeFormatter .ofPattern ("yyyyMMdd" );
@@ -30,6 +29,12 @@ public class SimpleOSSUploadFileService implements UploadFileService {
3029
3130 protected final OssProperties ossProperties ;
3231
32+ public SimpleOSSUploadFileService (OssTemplate ossTemplate , OssProperties ossProperties ) {
33+ this .ossTemplate = ossTemplate ;
34+ this .ossProperties = ossProperties ;
35+ }
36+
37+
3338 /**
3439 * 上传文件
3540 *
@@ -45,11 +50,11 @@ public UploadFileVO upload(String path, MultipartFile file) throws IOException {
4550 uploadFileVO .setContentType (file .getContentType ());
4651 uploadFileVO .setCreateTime (LocalDateTime .now ());
4752 uploadFileVO .setBucket (defaultBucketName ());
48- //计算文件md5
53+ // 计算文件md5
4954 String md5 = DigestUtils .md5Hex (file .getBytes ());
5055 uploadFileVO .setMd5 (md5 );
5156
52- //生成文件存储路径
57+ // 生成文件存储路径
5358 if (StringUtils .isNotBlank (path )) {
5459 uploadFileVO .setPrefix (path );
5560 }
@@ -111,7 +116,7 @@ public UploadFileVO upload(String key, String originalFilename, String contentTy
111116 uploadFileVO .setContentType (contentType );
112117 uploadFileVO .setCreateTime (LocalDateTime .now ());
113118 uploadFileVO .setBucket (defaultBucketName ());
114- //计算文件md5
119+ // 计算文件md5
115120 String md5 = DigestUtils .md5Hex (file .getBytes ());
116121 uploadFileVO .setMd5 (md5 );
117122
@@ -133,7 +138,7 @@ public String getPreviewUrl(String key) {
133138 if (ossProperties .getExpires () == -1 ) {
134139 return ossProperties .getPreviewUrl () + key ;
135140 } else {
136- return getPrivateUrl (key , ossProperties .getExpires ());
141+ return getPrivateUrl (ossProperties . getPreviewUrl (), key , ossProperties .getExpires ());
137142 }
138143 }
139144 return getPrivateUrl (key , ossProperties .getExpires () == -1 ? 300 : ossProperties .getExpires ());
@@ -153,6 +158,25 @@ public String getPrivateUrl(String key, Integer expires) {
153158 return ossTemplate .getObjectURL (defaultBucketName (), key , expires , TimeUnit .SECONDS );
154159 }
155160
161+ /**
162+ * 使用预览域名,通过OSS直接查看文件预览路径
163+ * 获取私有链接
164+ *
165+ * @param url 域名前缀
166+ * @param key oss key
167+ * @param expires 私有链接有效秒数
168+ * @return 文件阅览路径
169+ */
170+ // @Override
171+ public String getPrivateUrl (String url , String key , Integer expires ) {
172+ String objectURL = ossTemplate .getObjectURL (defaultBucketName (), key , expires , TimeUnit .SECONDS );
173+ if (StringUtils .isNotBlank (url )) {
174+
175+ return objectURL .replace (ossProperties .getEndpoint () + "/" + defaultBucketName () + "/" , url );
176+ }
177+ return objectURL ;
178+ }
179+
156180 /**
157181 * 文件内网阅览路径
158182 *
@@ -165,7 +189,7 @@ public String getPreviewIntranetUrl(String key) {
165189 if (ossProperties .getExpires () == -1 ) {
166190 return ossProperties .getIntranetUrl () + key ;
167191 } else {
168- return getPrivateUrl (key , ossProperties .getExpires ());
192+ return getPrivateUrl (ossProperties . getIntranetUrl (), key , ossProperties .getExpires ());
169193 }
170194 }
171195 return getPreviewUrl (key );
@@ -192,7 +216,7 @@ protected String generateFilePath(UploadFileVO uploadFileVO) {
192216 if (StringUtils .isNotBlank (uploadFileVO .getKey ())) {
193217 return uploadFileVO .getKey ();
194218 }
195- //后缀
219+ // 后缀
196220 String postfix = null ;
197221 if (StringUtils .isNotBlank (uploadFileVO .getOriginalFilename ()) && StringUtils .contains (uploadFileVO .getOriginalFilename (), "." )) {
198222
@@ -201,7 +225,7 @@ protected String generateFilePath(UploadFileVO uploadFileVO) {
201225
202226 StringBuilder filePathBuilder = new StringBuilder ();
203227
204- //文件前缀
228+ // 文件前缀
205229 if (StringUtils .isNotBlank (uploadFileVO .getPrefix ())) {
206230 filePathBuilder .append (uploadFileVO .getPrefix ());
207231 if (!StringUtils .endsWith (uploadFileVO .getPrefix (), "/" )) {
0 commit comments