package com.goldenwater.web.controller.efinder; import com.goldenwater.slgc.entity.efinder.DocFile; import com.goldenwater.slgc.service.efinder.IDocFileService; import com.goldenwater.common.core.controller.BaseController; import com.goldenwater.common.core.domain.AjaxResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import com.goldenwater.common.annotation.Log; import com.goldenwater.common.enums.BusinessType; import jakarta.servlet.http.HttpServletResponse; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import java.io.IOException; import java.util.HashMap; import java.util.Map; @Tag(name = "01.03 文件管理-文档编辑", description = "文档在线编辑相关接口") @RestController @RequestMapping("/efinder/pageoffice") public class PageOfficeController extends BaseController { @Autowired private IDocFileService docFileService; /** * 获取文件信息(供前端打开编辑器) */ @Operation(summary = "获取文件信息") @GetMapping("/open/{fileId}") public AjaxResult open(@Parameter(description = "文件ID") @PathVariable Long fileId) { DocFile file = docFileService.selectById(fileId); if (file == null) { return error("文件不存在"); } Map result = new HashMap<>(); result.put("id", file.getId()); result.put("name", file.getName()); result.put("path", file.getPath()); result.put("type", file.getType()); result.put("size", file.getSize()); return success(result); } /** * 下载Word文档(供前端编辑前下载) */ @Operation(summary = "下载Word文档") @GetMapping("/download/{fileId}") public void download(@Parameter(description = "文件ID") @PathVariable Long fileId, HttpServletResponse response) { docFileService.download(fileId, response); } /** * 保存编辑后的Word文档(上传覆盖原文件) */ @Operation(summary = "保存编辑后的Word文档") @Log(title = "PageOffice文档保存", businessType = BusinessType.UPDATE) @PostMapping("/save/{fileId}") public AjaxResult save(@Parameter(description = "文件ID") @PathVariable Long fileId, @Parameter(description = "上传的文件") @RequestParam("file") MultipartFile file) { try { DocFile docFile = docFileService.selectById(fileId); if (docFile == null) { return error("文件不存在"); } String absPath = docFile.getPath(); java.io.File dest = new java.io.File(absPath); if (dest.getParentFile() != null && !dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } file.transferTo(dest); docFile.setSize(file.getSize()); docFileService.update(docFile); return success("保存成功"); } catch (IOException e) { return error("保存失败: " + e.getMessage()); } } /** * 返回在线编辑器HTML页面(iframe嵌入方式) * 前端可通过 iframe 加载此页面实现文档预览 */ @Operation(summary = "返回在线编辑器HTML页面") @GetMapping("/editor/{fileId}") public void editor(@Parameter(description = "文件ID") @PathVariable Long fileId, HttpServletResponse response) { DocFile file = docFileService.selectById(fileId); if (file == null) { return; } try { response.setContentType("text/html;charset=UTF-8"); StringBuilder html = new StringBuilder(); html.append(""); html.append("").append(file.getName()).append(" - 在线编辑"); html.append(""); html.append(""); html.append("
"); html.append("").append(file.getName()).append(""); html.append(""); html.append("
"); html.append("
"); html.append("
"); html.append("

文档预览不支持直接在线编辑

"); html.append("

请点击上方「下载文档」按钮下载到本地编辑

"); html.append("

编辑完成后可通过页面上传功能保存修改

"); html.append("
"); html.append("
"); html.append(""); response.getWriter().write(html.toString()); } catch (IOException e) { throw new RuntimeException(e); } } }