mirror of
https://gitee.com/likeadmin/likeadmin_java.git
synced 2026-05-21 22:25:49 +08:00
优化代码
This commit is contained in:
@@ -8,6 +8,7 @@ import com.mdd.common.core.AjaxResult;
|
||||
import com.mdd.common.enums.AlbumEnum;
|
||||
import com.mdd.common.exception.OperateException;
|
||||
import com.mdd.common.plugin.storage.StorageDriver;
|
||||
import com.mdd.common.plugin.storage.UploadFilesVo;
|
||||
import com.mdd.common.utils.StringUtil;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -24,7 +25,7 @@ import java.util.Map;
|
||||
* 上传管理
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("api/common/upload")
|
||||
@RequestMapping("api/upload")
|
||||
public class UploadController {
|
||||
|
||||
@Resource
|
||||
@@ -35,43 +36,38 @@ public class UploadController {
|
||||
*
|
||||
* @author fzr
|
||||
* @param request 请求对象
|
||||
* @return AjaxResult<Object>
|
||||
* @return AjaxResult<UploadFilesVo>
|
||||
*/
|
||||
@Log(title = "上传图片", requestType = RequestType.File)
|
||||
@PostMapping("/image")
|
||||
public AjaxResult<Object> image(HttpServletRequest request) {
|
||||
public AjaxResult<UploadFilesVo> image(HttpServletRequest request) {
|
||||
MultipartFile multipartFile;
|
||||
try {
|
||||
multipartFile = ((MultipartRequest) request).getFile("file");
|
||||
} catch (Exception e) {
|
||||
return AjaxResult.failed("请正确选择上传图片");
|
||||
throw new OperateException("请正确选择上传图片!");
|
||||
}
|
||||
|
||||
if (multipartFile == null) {
|
||||
return AjaxResult.failed("请选择上传图片");
|
||||
throw new OperateException("请选择上传图片!");
|
||||
}
|
||||
|
||||
try {
|
||||
StorageDriver storageDriver = new StorageDriver();
|
||||
Map<String, Object> map = storageDriver.upload(multipartFile, "image", AlbumEnum.IMAGE.getCode());
|
||||
String cid = StringUtil.isNotEmpty(request.getParameter("cid")) ? request.getParameter("cid") : "0";
|
||||
StorageDriver storageDriver = new StorageDriver();
|
||||
UploadFilesVo vo = storageDriver.upload(multipartFile, "image", AlbumEnum.IMAGE.getCode());
|
||||
String cid = StringUtil.isNotEmpty(request.getParameter("cid")) ? request.getParameter("cid") : "0";
|
||||
|
||||
Map<String, String> album = new LinkedHashMap<>();
|
||||
album.put("aid", String.valueOf(LikeAdminThreadLocal.getAdminId()));
|
||||
album.put("cid", cid);
|
||||
album.put("type", String.valueOf(AlbumEnum.IMAGE.getCode()));
|
||||
album.put("size", map.get("size").toString());
|
||||
album.put("ext", map.get("ext").toString());
|
||||
album.put("url", map.get("url").toString());
|
||||
album.put("name", map.get("name").toString());
|
||||
Integer id = iAlbumsService.albumAdd(album);
|
||||
Map<String, String> album = new LinkedHashMap<>();
|
||||
album.put("aid", String.valueOf(LikeAdminThreadLocal.getAdminId()));
|
||||
album.put("cid", cid);
|
||||
album.put("type", String.valueOf(AlbumEnum.IMAGE.getCode()));
|
||||
album.put("size", vo.getSize().toString());
|
||||
album.put("ext", vo.getExt());
|
||||
album.put("url", vo.getUrl());
|
||||
album.put("name", vo.getName());
|
||||
Integer id = iAlbumsService.albumAdd(album);
|
||||
|
||||
map.put("id", id);
|
||||
|
||||
return AjaxResult.success(map);
|
||||
} catch (OperateException e) {
|
||||
return AjaxResult.failed(e.getMsg());
|
||||
}
|
||||
vo.setId(id);
|
||||
return AjaxResult.success(vo);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,43 +75,38 @@ public class UploadController {
|
||||
*
|
||||
* @author fzr
|
||||
* @param request 请求对象
|
||||
* @return AjaxResult<Object>
|
||||
* @return AjaxResult<UploadFilesVo>
|
||||
*/
|
||||
@Log(title = "上传视频", requestType = RequestType.File)
|
||||
@PostMapping("/video")
|
||||
public AjaxResult<Object> video(HttpServletRequest request) {
|
||||
public AjaxResult<UploadFilesVo> video(HttpServletRequest request) {
|
||||
MultipartFile multipartFile;
|
||||
try {
|
||||
multipartFile = ((MultipartRequest) request).getFile("file");
|
||||
} catch (Exception e) {
|
||||
return AjaxResult.failed("请正确选择上传视频");
|
||||
throw new OperateException("请选择上传视频!");
|
||||
}
|
||||
|
||||
if (multipartFile == null) {
|
||||
return AjaxResult.failed("请选择上传视频");
|
||||
throw new OperateException("请选择上传视频!");
|
||||
}
|
||||
|
||||
try {
|
||||
StorageDriver storageDriver = new StorageDriver();
|
||||
Map<String, Object> map = storageDriver.upload(multipartFile, "video", AlbumEnum.Video.getCode());
|
||||
String cid = StringUtil.isNotEmpty(request.getParameter("cid")) ? request.getParameter("cid") : "0";
|
||||
StorageDriver storageDriver = new StorageDriver();
|
||||
UploadFilesVo vo = storageDriver.upload(multipartFile, "video", AlbumEnum.Video.getCode());
|
||||
String cid = StringUtil.isNotEmpty(request.getParameter("cid")) ? request.getParameter("cid") : "0";
|
||||
|
||||
Map<String, String> album = new LinkedHashMap<>();
|
||||
album.put("cid", cid);
|
||||
album.put("aid", String.valueOf(LikeAdminThreadLocal.getAdminId()));
|
||||
album.put("type", String.valueOf(AlbumEnum.Video.getCode()));
|
||||
album.put("ext", map.get("ext").toString());
|
||||
album.put("size", map.get("size").toString());
|
||||
album.put("url", map.get("url").toString());
|
||||
album.put("name", map.get("name").toString());
|
||||
Integer id = iAlbumsService.albumAdd(album);
|
||||
Map<String, String> album = new LinkedHashMap<>();
|
||||
album.put("cid", cid);
|
||||
album.put("aid", String.valueOf(LikeAdminThreadLocal.getAdminId()));
|
||||
album.put("type", String.valueOf(AlbumEnum.Video.getCode()));
|
||||
album.put("ext", vo.getExt());
|
||||
album.put("size", vo.getSize().toString());
|
||||
album.put("url", vo.getUrl());
|
||||
album.put("name", vo.getName());
|
||||
Integer id = iAlbumsService.albumAdd(album);
|
||||
|
||||
map.put("id", id);
|
||||
|
||||
return AjaxResult.success(map);
|
||||
} catch (OperateException e) {
|
||||
return AjaxResult.failed(e.getMsg());
|
||||
}
|
||||
vo.setId(id);
|
||||
return AjaxResult.success(vo);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user