| | |
| | | package com.ruoyi.other.controller; |
| | | |
| | | |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.framework.web.domain.AjaxResult; |
| | | import com.ruoyi.other.service.TempFileService; |
| | | import com.ruoyi.purchase.dto.ProductRecordDto; |
| | | import com.ruoyi.purchase.dto.TicketRegistrationDto; |
| | | import com.ruoyi.purchase.service.ITicketRegistrationService; |
| | | import com.ruoyi.purchase.service.impl.TicketRegistrationServiceImpl; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | |
| | | |
| | | @RestController |
| | |
| | | |
| | | private TempFileService tempFileService; |
| | | |
| | | private TicketRegistrationServiceImpl ticketRegistrationServiceImpl; |
| | | |
| | | @PostMapping("/upload") |
| | | public AjaxResult uploadFile(MultipartFile file, String type) { |
| | | public AjaxResult uploadFile(MultipartFile file, Integer type) { |
| | | try { |
| | | return AjaxResult.success(tempFileService.uploadFile(file, type)); |
| | | } catch (Exception e) { |
| | |
| | | } |
| | | } |
| | | |
| | | @PostMapping("uploadFile") |
| | | public AjaxResult uploadFile(@RequestBody ProductRecordDto productRecordDto) { |
| | | try { |
| | | if (!productRecordDto.getTempFileIds().isEmpty()&&productRecordDto.getTicketRegistrationId() != null) { |
| | | ticketRegistrationServiceImpl.migrateTempFilesToFormal(productRecordDto.getTicketRegistrationId(), productRecordDto.getTempFileIds()); |
| | | } |
| | | } catch (Exception e) { |
| | | return AjaxResult.error(e.getMessage()); |
| | | } |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | /** |
| | | * 图片预览(根据磁盘路径) |
| | | */ |
| | | @GetMapping("/preview") |
| | | @ApiOperation(value = "图片预览") |
| | | public void previewImage(String url, HttpServletResponse response) { |
| | | if (!StringUtils.hasText(url)) { |
| | | response.setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| | | return; |
| | | } |
| | | try { |
| | | Path filePath = Paths.get(url); |
| | | if (!Files.exists(filePath) || !Files.isRegularFile(filePath)) { |
| | | response.setStatus(HttpServletResponse.SC_NOT_FOUND); |
| | | return; |
| | | } |
| | | String filename = filePath.getFileName().toString(); |
| | | String ext = filename.contains(".") ? filename.substring(filename.lastIndexOf('.') + 1).toLowerCase() : ""; |
| | | MediaType mediaType; |
| | | switch (ext) { |
| | | case "png": |
| | | mediaType = MediaType.IMAGE_PNG; |
| | | break; |
| | | case "gif": |
| | | mediaType = MediaType.IMAGE_GIF; |
| | | break; |
| | | case "bmp": |
| | | mediaType = MediaType.parseMediaType("image/bmp"); |
| | | break; |
| | | case "webp": |
| | | mediaType = MediaType.parseMediaType("image/webp"); |
| | | break; |
| | | case "jpg": |
| | | case "jpeg": |
| | | mediaType = MediaType.IMAGE_JPEG; |
| | | break; |
| | | default: |
| | | mediaType = MediaType.APPLICATION_OCTET_STREAM; |
| | | } |
| | | response.setContentType(mediaType.toString()); |
| | | response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=3600"); |
| | | Files.copy(filePath, response.getOutputStream()); |
| | | } catch (IOException e) { |
| | | response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
| | | } |
| | | } |
| | | |
| | | } |