Skip to content

Commit 2e11aec

Browse files
author
yqz
committed
fix:修复文章图片无法保存的bug
1 parent f8a9210 commit 2e11aec

4 files changed

Lines changed: 33 additions & 11 deletions

File tree

OpenBlog-business/src/main/java/com/yqz/openblog/media/controller/MediaController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ public MediaController(MediaService mediaService) {
3939
@PostMapping(value = "/upload", produces = MediaType.APPLICATION_JSON_VALUE)
4040
public ApiResponse<MediaUploadResponse> upload(
4141
@RequestParam("file") MultipartFile file,
42-
@RequestParam(required = false) Long folderId) throws IOException {
43-
return ApiResponse.ok(mediaService.upload(file, folderId));
42+
@RequestParam(required = false) Long folderId,
43+
@RequestParam(required = false) String category) throws IOException {
44+
return ApiResponse.ok(mediaService.upload(file, folderId, category));
4445
}
4546

4647
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)

OpenBlog-business/src/main/java/com/yqz/openblog/media/service/MediaService.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@
3131
import java.util.Locale;
3232
import java.util.Map;
3333
import java.util.UUID;
34+
import java.util.regex.Pattern;
3435
import java.util.stream.Collectors;
3536

3637
@Service
3738
public class MediaService {
3839

40+
private static final Pattern CATEGORY_PATTERN = Pattern.compile("^[a-z0-9][a-z0-9-]{0,63}$");
41+
3942
private final MediaMapper mediaMapper;
4043
private final MediaProperties properties;
4144
private final CurrentUser currentUser;
@@ -57,7 +60,7 @@ public MediaService(MediaMapper mediaMapper,
5760
this.minioMediaStorage = minioMediaStorage;
5861
}
5962

60-
public MediaUploadResponse upload(MultipartFile file, Long folderId) throws IOException {
63+
public MediaUploadResponse upload(MultipartFile file, Long folderId, String category) throws IOException {
6164
if (file == null || file.isEmpty()) {
6265
throw new BizException(4002, "文件不能为空");
6366
}
@@ -75,7 +78,7 @@ public MediaUploadResponse upload(MultipartFile file, Long folderId) throws IOEx
7578
throw new BizException(4002, "无法识别图片后缀");
7679
}
7780

78-
String cat = resolveFolderPrefix(folderId);
81+
String cat = resolveStorageCategory(folderId, category);
7982
String key = cat + "/" + UUID.randomUUID().toString() + "." + ext;
8083
MediaStorage storage = activeStorage();
8184

@@ -227,11 +230,18 @@ public List<MediaCategoryResponse> listCategories() {
227230
.collect(Collectors.toList());
228231
}
229232

230-
private String resolveFolderPrefix(Long folderId) {
231-
if (folderId == null) {
232-
return "general";
233+
private String resolveStorageCategory(Long folderId, String category) {
234+
if (folderId != null) {
235+
return "folder-" + folderId;
236+
}
237+
if (StringUtils.hasText(category)) {
238+
String c = category.trim().toLowerCase(Locale.ROOT);
239+
if (!CATEGORY_PATTERN.matcher(c).matches()) {
240+
throw new BizException(4002, "无效的分类标识");
241+
}
242+
return c;
233243
}
234-
return "folder-" + folderId;
244+
return "general";
235245
}
236246

237247
public void deleteMyMedia(String key) {

vue/src/api/media.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@ function getAuthHeader() {
77
return { Authorization: `Bearer ${token}` }
88
}
99

10-
export async function uploadMedia(file, folderId) {
10+
export async function uploadMedia(file, folderIdOrOptions) {
1111
const fd = new FormData()
1212
fd.append('file', file)
13+
let folderId = null
14+
let category = null
15+
if (folderIdOrOptions != null) {
16+
if (typeof folderIdOrOptions === 'object') {
17+
folderId = folderIdOrOptions.folderId ?? null
18+
category = folderIdOrOptions.category ?? null
19+
} else {
20+
folderId = folderIdOrOptions
21+
}
22+
}
1323
if (folderId != null) fd.append('folderId', folderId)
24+
if (category != null) fd.append('category', category)
1425

1526
const res = await fetch(`${API_BASE}/api/v1/media/upload`, {
1627
method: 'POST',

vue/src/views/ConsoleArticleComposeView.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ async function uploadAndInsert(file) {
598598
599599
try {
600600
601-
const resp = await uploadMedia(file, 'article-body')
601+
const resp = await uploadMedia(file, { category: 'article-body' })
602602
603603
const md = `![${file.name || 'image'}](${resp.url})`
604604
@@ -902,7 +902,7 @@ async function onPickCover(e) {
902902
903903
articleError.value = ''
904904
905-
const resp = await uploadMedia(file, 'article-cover')
905+
const resp = await uploadMedia(file, { category: 'article-cover' })
906906
907907
form.value.coverMediaKey = resp.key
908908

0 commit comments

Comments
 (0)