|
@@ -0,0 +1,96 @@
|
|
|
+/*
|
|
|
+ * Copyright (c) 2018-2025, lengleng All rights reserved.
|
|
|
+ *
|
|
|
+ * Redistribution and use in source and binary forms, with or without
|
|
|
+ * modification, are permitted provided that the following conditions are met:
|
|
|
+ *
|
|
|
+ * Redistributions of source code must retain the above copyright notice,
|
|
|
+ * this list of conditions and the following disclaimer.
|
|
|
+ * Redistributions in binary form must reproduce the above copyright
|
|
|
+ * notice, this list of conditions and the following disclaimer in the
|
|
|
+ * documentation and/or other materials provided with the distribution.
|
|
|
+ * Neither the name of the pig4cloud.com developer nor the names of its
|
|
|
+ * contributors may be used to endorse or promote products derived from
|
|
|
+ * this software without specific prior written permission.
|
|
|
+ * Author: lengleng (wangiegie@gmail.com)
|
|
|
+ */
|
|
|
+
|
|
|
+package com.pig4cloud.pigx.admin.controller;
|
|
|
+
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.pig4cloud.common.minio.service.MinioTemplate;
|
|
|
+import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
|
|
+import com.pig4cloud.pigx.common.core.util.R;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author lengleng
|
|
|
+ * @date 2018-12-30
|
|
|
+ * <p>
|
|
|
+ * 文件管理
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@AllArgsConstructor
|
|
|
+@RequestMapping("/file")
|
|
|
+public class FileController {
|
|
|
+ private final MinioTemplate minioTemplate;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件
|
|
|
+ *
|
|
|
+ * @param file 资源
|
|
|
+ * @return R(bucketName, filename)
|
|
|
+ */
|
|
|
+ @PostMapping("/upload")
|
|
|
+ public R upload(@RequestParam("file") MultipartFile file) {
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ String newName = RandomUtil.randomString(16) + originalFilename;
|
|
|
+
|
|
|
+ Map<String, String> resultMap = new HashMap<>(4);
|
|
|
+ resultMap.put("bucketName", CommonConstants.BUCKET_NAME);
|
|
|
+ resultMap.put("fileName", newName);
|
|
|
+
|
|
|
+ try {
|
|
|
+ minioTemplate.putObject(CommonConstants.BUCKET_NAME, newName, file.getInputStream());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("上传失败", e);
|
|
|
+ return R.builder().code(CommonConstants.FAIL)
|
|
|
+ .msg(e.getLocalizedMessage()).build();
|
|
|
+ }
|
|
|
+ return R.builder().data(resultMap).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件
|
|
|
+ *
|
|
|
+ * @param fileName 文件空间/名称
|
|
|
+ * @param response
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping("/{fileName}")
|
|
|
+ public R file(@PathVariable String fileName, HttpServletResponse response) {
|
|
|
+ String[] nameArray = StrUtil.split(fileName, StrUtil.DASHED);
|
|
|
+
|
|
|
+ try (InputStream inputStream = minioTemplate.getObject(nameArray[0], nameArray[1])) {
|
|
|
+ response.setContentType("application/octet-stream; charset=UTF-8");
|
|
|
+ IoUtil.copy(inputStream, response.getOutputStream());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("文件读取异常", e);
|
|
|
+ return R.builder().code(CommonConstants.FAIL)
|
|
|
+ .msg(e.getLocalizedMessage()).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ return R.builder().build();
|
|
|
+ }
|
|
|
+}
|