package com.farriver.bwf.service.common; import com.farriver.bwf.common.model.ApiData; import com.farriver.bwf.common.model.WangEditResponse; import com.farriver.bwf.common.model.WangEditResponseData; import com.farriver.bwf.common.statics.LangConstants; import com.farriver.bwf.common.utilities.FileUtil; import com.farriver.bwf.common.utilities.HttpUtil; import jakarta.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; @Service public class FileService { private static final Logger logger = LoggerFactory.getLogger(FileService.class); @Value("${file.upload.path}") public String uploadPath; @Value("${file.upload.alias}") public String uploadAlias; @Value("${file.nginx.host}") public String nginxHost; @Value("${file.nginx.port}") public String nginxPort; public String getAccessBase() { if (nginxHost.toLowerCase().startsWith("https://")) { return String.format("%s/%s", nginxHost, uploadAlias); } else { return String.format("%s:%s/%s", nginxHost, nginxPort, uploadAlias); } } public String Upload(MultipartFile file, HttpServletRequest request) { String httpaccesspath = null; try { httpaccesspath = getAccessBase() + FileUtil.writeFile(uploadPath, file); } catch (IOException e) { logger.error(e.getMessage(), e); } return httpaccesspath; } public ApiData UploadDocument(MultipartFile file, HttpServletRequest request) { try { if (file == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } String module = HttpUtil.getUrlQueryMap(request).get("module"); if (module == null || module.isEmpty()) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } var re = FileUtil.writeDocumentFile(module, uploadPath, file); var httpaccesspath = String.format("%s%s", getAccessBase(), re.getRelativepath()); re.setLinkurl(httpaccesspath); return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, re); } catch (IOException e) { logger.error(e.getMessage(), e); } return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } public ApiData UploadImportTemplate(MultipartFile file, HttpServletRequest request) { try { if (file == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } String module = HttpUtil.getUrlQueryMap(request).get("module"); if (module == null || module.isEmpty()) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } var re = FileUtil.writeImportTemplateFile(module, uploadPath, file); var httpaccesspath = String.format("%s%s", getAccessBase(), re.getRelativepath()); re.setLinkurl(httpaccesspath); return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, re); } catch (IOException e) { logger.error(e.getMessage(), e); } return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } public ApiData UploadExportTemplate(MultipartFile file, HttpServletRequest request) { try { if (file == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } String module = HttpUtil.getUrlQueryMap(request).get("module"); if (module == null || module.isEmpty()) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } var re = FileUtil.writeExportTemplateFile(module, uploadPath, file); var httpaccesspath = String.format("%s%s", getAccessBase(), re.getRelativepath()); re.setLinkurl(httpaccesspath); return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, re); } catch (IOException e) { logger.error(e.getMessage(), e); } return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } public ApiData UploadReport(MultipartFile file, HttpServletRequest request) { try { if (file == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } String module = HttpUtil.getUrlQueryMap(request).get("module"); if (module == null || module.isEmpty()) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } var re = FileUtil.writeReportFile(module, uploadPath, file); var httpaccesspath = String.format("%s%s", getAccessBase(), re.getRelativepath()); re.setLinkurl(httpaccesspath); return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, re); } catch (IOException e) { logger.error(e.getMessage(), e); } return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } public ApiData UploadBOL(MultipartFile file, HttpServletRequest request) { try { if (file == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } String module = HttpUtil.getUrlQueryMap(request).get("module"); if (module == null || module.isEmpty()) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } var re = FileUtil.writeBOLFile(module, uploadPath, file); var httpaccesspath = String.format("%s%s", getAccessBase(), re.getRelativepath()); re.setLinkurl(httpaccesspath); return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, re); } catch (IOException e) { logger.error(e.getMessage(), e); } return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } public WangEditResponse WangEditUpload(MultipartFile file, HttpServletRequest request) { String httpaccesspath = null; try { httpaccesspath = getAccessBase() + FileUtil.writeFile(uploadPath, file); } catch (IOException e) { logger.error(e.getMessage(), e); } WangEditResponse res = new WangEditResponse(); WangEditResponseData redata = new WangEditResponseData(); redata.setUrl(httpaccesspath); res.setData(redata); return res; } }