|
@@ -5,16 +5,14 @@ import com.etotem.cfc.entity.Article;
|
|
|
import com.etotem.cfc.entity.ArticleCategory;
|
|
import com.etotem.cfc.entity.ArticleCategory;
|
|
|
import com.etotem.cfc.service.ArticleCategoryService;
|
|
import com.etotem.cfc.service.ArticleCategoryService;
|
|
|
import com.etotem.cfc.service.ArticleService;
|
|
import com.etotem.cfc.service.ArticleService;
|
|
|
|
|
+import com.etotem.cfc.service.FileStorageService;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
-import java.io.File;
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
-import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
@RestController
|
|
|
@RequestMapping("/api/admin/articles")
|
|
@RequestMapping("/api/admin/articles")
|
|
@@ -26,6 +24,9 @@ public class AdminArticleController {
|
|
|
@Resource
|
|
@Resource
|
|
|
private ArticleCategoryService articleCategoryService;
|
|
private ArticleCategoryService articleCategoryService;
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private FileStorageService fileStorageService;
|
|
|
|
|
+
|
|
|
@PostMapping("/list")
|
|
@PostMapping("/list")
|
|
|
public Result<Page<Article>> list(@RequestBody Map<String, Object> body) {
|
|
public Result<Page<Article>> list(@RequestBody Map<String, Object> body) {
|
|
|
String status = (String) body.get("status");
|
|
String status = (String) body.get("status");
|
|
@@ -250,19 +251,11 @@ public class AdminArticleController {
|
|
|
return Result.error("文件为空");
|
|
return Result.error("文件为空");
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
- String uploadDir = System.getProperty("user.dir") + "/uploads/articles/";
|
|
|
|
|
- File dir = new File(uploadDir);
|
|
|
|
|
- if (!dir.exists()) dir.mkdirs();
|
|
|
|
|
-
|
|
|
|
|
- String ext = file.getOriginalFilename();
|
|
|
|
|
- ext = ext != null && ext.contains(".") ? ext.substring(ext.lastIndexOf(".")) : ".jpg";
|
|
|
|
|
- String filename = UUID.randomUUID().toString() + ext;
|
|
|
|
|
- File dest = new File(uploadDir + filename);
|
|
|
|
|
- file.transferTo(dest);
|
|
|
|
|
-
|
|
|
|
|
- String url = "/uploads/articles/" + filename;
|
|
|
|
|
|
|
+ String url = fileStorageService.store(file, "articles");
|
|
|
return Result.success(url);
|
|
return Result.success(url);
|
|
|
- } catch (IOException e) {
|
|
|
|
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
|
+ return Result.error(e.getMessage());
|
|
|
|
|
+ } catch (RuntimeException e) {
|
|
|
return Result.error("上传失败: " + e.getMessage());
|
|
return Result.error("上传失败: " + e.getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|