mirror of
https://github.com/chatopera/cosin.git
synced 2025-08-01 16:38:02 +08:00
Fix StreamingFileRepository related class
This commit is contained in:
parent
802efafc13
commit
f707132743
@ -25,13 +25,14 @@ import com.chatopera.cc.persistence.blob.JpaBlobHelper;
|
||||
import com.chatopera.cc.persistence.repository.AttachmentRepository;
|
||||
import com.chatopera.cc.persistence.repository.StreamingFileRepository;
|
||||
import com.chatopera.cc.util.Menu;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.tomcat.util.http.fileupload.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@ -48,34 +49,32 @@ import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/res")
|
||||
@RequiredArgsConstructor
|
||||
public class MediaController extends Handler {
|
||||
private final static Logger logger = LoggerFactory.getLogger(MediaController.class);
|
||||
|
||||
private static final String TEMPLATE_DATA_PATH = "WEB-INF/data/templates/";
|
||||
@NonNull
|
||||
private final StreamingFileRepository streamingFileRes;
|
||||
@NonNull
|
||||
private final JpaBlobHelper jpaBlobHelper;
|
||||
@NonNull
|
||||
private final AttachmentRepository attachementRes;
|
||||
@Value("${web.upload-path}")
|
||||
private String path;
|
||||
|
||||
@Autowired
|
||||
private StreamingFileRepository streamingFileRes;
|
||||
|
||||
@Autowired
|
||||
private JpaBlobHelper jpaBlobHelper;
|
||||
|
||||
private final String TEMPLATE_DATA_PATH = "WEB-INF/data/templates/";
|
||||
|
||||
@Autowired
|
||||
private AttachmentRepository attachementRes;
|
||||
|
||||
@RequestMapping("/image")
|
||||
@Menu(type = "resouce", subtype = "image", access = true)
|
||||
public void index(HttpServletResponse response,
|
||||
@Valid String id,
|
||||
@RequestParam(value = "original", required = false) boolean original,
|
||||
@RequestParam(value = "cooperation", required = false) boolean cooperation) throws IOException, SQLException {
|
||||
StreamingFile sf = streamingFileRes.findOne(id);
|
||||
if (sf != null) {
|
||||
Optional<StreamingFile> optional = streamingFileRes.findById(id);
|
||||
if (optional.isPresent()) {
|
||||
StreamingFile sf = optional.get();
|
||||
response.setHeader("Content-Type", sf.getMime());
|
||||
response.setContentType(sf.getMime());
|
||||
if (cooperation && (sf.getCooperation() != null)) { // 协作文件
|
||||
@ -105,7 +104,7 @@ public class MediaController extends Handler {
|
||||
@Menu(type = "resouce", subtype = "image", access = true)
|
||||
public void url(HttpServletResponse response, @Valid String url) throws IOException {
|
||||
byte[] data = new byte[1024];
|
||||
int length = 0;
|
||||
int length;
|
||||
OutputStream out = response.getOutputStream();
|
||||
if (StringUtils.isNotBlank(url)) {
|
||||
InputStream input = new URL(url).openStream();
|
||||
@ -119,13 +118,13 @@ public class MediaController extends Handler {
|
||||
@RequestMapping("/image/upload")
|
||||
@Menu(type = "resouce", subtype = "imageupload")
|
||||
public ModelAndView upload(ModelMap map,
|
||||
HttpServletRequest request,
|
||||
@RequestParam(value = "imgFile", required = false) MultipartFile multipart) throws IOException {
|
||||
ModelAndView view = request(super.createRequestPageTempletResponse("/public/upload"));
|
||||
UploadStatus notify = null;
|
||||
if (multipart != null && multipart.getOriginalFilename().lastIndexOf(".") > 0) {
|
||||
UploadStatus notify;
|
||||
if (multipart != null && multipart.getOriginalFilename() != null && multipart.getOriginalFilename().lastIndexOf(".") > 0) {
|
||||
File uploadDir = new File(path, "upload");
|
||||
if (!uploadDir.exists()) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
uploadDir.mkdirs();
|
||||
}
|
||||
String fileid = MainUtils.getUUID();
|
||||
@ -150,8 +149,9 @@ public class MediaController extends Handler {
|
||||
if (StringUtils.isNotBlank(id)) {
|
||||
AttachmentFile attachmentFile = attachementRes.findByIdAndOrgi(id, super.getOrgi(request));
|
||||
if (attachmentFile != null && attachmentFile.getFileid() != null) {
|
||||
StreamingFile sf = streamingFileRes.findOne(attachmentFile.getFileid());
|
||||
if (sf != null) {
|
||||
Optional<StreamingFile> optional = streamingFileRes.findById(attachmentFile.getFileid());
|
||||
if (optional.isPresent()) {
|
||||
StreamingFile sf = optional.get();
|
||||
response.setContentType(attachmentFile.getFiletype());
|
||||
response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(attachmentFile.getTitle(), "UTF-8"));
|
||||
IOUtils.copy(sf.getData().getBinaryStream(), response.getOutputStream());
|
||||
@ -166,7 +166,7 @@ public class MediaController extends Handler {
|
||||
|
||||
@RequestMapping("/template")
|
||||
@Menu(type = "resouce", subtype = "template")
|
||||
public void template(HttpServletResponse response, HttpServletRequest request, @Valid String filename) throws IOException {
|
||||
public void template(HttpServletResponse response, @Valid String filename) throws IOException {
|
||||
if (StringUtils.isNotBlank(filename)) {
|
||||
InputStream is = MediaController.class.getClassLoader().getResourceAsStream(TEMPLATE_DATA_PATH + filename);
|
||||
if (is != null) {
|
||||
@ -180,7 +180,6 @@ public class MediaController extends Handler {
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,12 +16,14 @@ import com.chatopera.cc.persistence.repository.StreamingFileRepository;
|
||||
import com.chatopera.cc.socketio.message.ChatMessage;
|
||||
import com.chatopera.cc.socketio.message.Message;
|
||||
import com.chatopera.cc.util.HashMapUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@ -30,45 +32,34 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Objects;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class AgentProxy {
|
||||
private final static Logger logger = LoggerFactory.getLogger(AgentProxy.class);
|
||||
|
||||
@NonNull
|
||||
private final ACDPolicyService acdPolicyService;
|
||||
@NonNull
|
||||
private final AttachmentRepository attachementRes;
|
||||
@NonNull
|
||||
private final JpaBlobHelper jpaBlobHelper;
|
||||
@NonNull
|
||||
private final StreamingFileRepository streamingFileRepository;
|
||||
@NonNull
|
||||
private final PeerSyncIM peerSyncIM;
|
||||
@NonNull
|
||||
private final SNSAccountRepository snsAccountRes;
|
||||
@NonNull
|
||||
private final Cache cache;
|
||||
@NonNull
|
||||
private final AgentStatusRepository agentStatusRes;
|
||||
@Value("${web.upload-path}")
|
||||
private String webUploadPath;
|
||||
|
||||
@Autowired
|
||||
private ACDPolicyService acdPolicyService;
|
||||
|
||||
@Autowired
|
||||
private AttachmentRepository attachementRes;
|
||||
|
||||
@Autowired
|
||||
private JpaBlobHelper jpaBlobHelper;
|
||||
|
||||
@Autowired
|
||||
private StreamingFileRepository streamingFileRepository;
|
||||
|
||||
@Autowired
|
||||
private PeerSyncIM peerSyncIM;
|
||||
|
||||
@Autowired
|
||||
private SNSAccountRepository snsAccountRes;
|
||||
|
||||
@Autowired
|
||||
private Cache cache;
|
||||
|
||||
@Autowired
|
||||
private AgentStatusRepository agentStatusRes;
|
||||
|
||||
|
||||
/**
|
||||
* 设置一个坐席为就绪状态
|
||||
* 不牵扯ACD
|
||||
*
|
||||
* @param user
|
||||
* @param agentStatus
|
||||
*/
|
||||
public void ready(final User user, final AgentStatus agentStatus, final boolean busy) {
|
||||
agentStatus.setOrgi(user.getOrgi());
|
||||
@ -85,7 +76,7 @@ public class AgentProxy {
|
||||
SessionConfig sessionConfig = acdPolicyService.initSessionConfig(agentStatus.getOrgi());
|
||||
agentStatus.setMaxusers(sessionConfig.getMaxuser());
|
||||
|
||||
/**
|
||||
/*
|
||||
* 更新当前用户状态
|
||||
*/
|
||||
agentStatus.setUsers(
|
||||
@ -103,9 +94,6 @@ public class AgentProxy {
|
||||
|
||||
/**
|
||||
* 将消息发布到接收端
|
||||
*
|
||||
* @param chatMessage
|
||||
* @param agentUser
|
||||
*/
|
||||
public void sendChatMessageByAgent(final ChatMessage chatMessage, final AgentUser agentUser) {
|
||||
Message outMessage = new Message();
|
||||
@ -117,7 +105,7 @@ public class AgentProxy {
|
||||
if (StringUtils.isNotBlank(agentUser.getAppid())) {
|
||||
snsAccountRes.findOneBySnsTypeAndSnsIdAndOrgi(
|
||||
agentUser.getChannel(), agentUser.getAppid(), agentUser.getOrgi()).ifPresent(
|
||||
p -> outMessage.setSnsAccount(p));
|
||||
outMessage::setSnsAccount);
|
||||
}
|
||||
|
||||
outMessage.setContextid(chatMessage.getContextid());
|
||||
@ -149,13 +137,8 @@ public class AgentProxy {
|
||||
|
||||
/**
|
||||
* 发送坐席的图片消息给访客和坐席自己
|
||||
*
|
||||
* @param creator
|
||||
* @param agentUser
|
||||
* @param multipart
|
||||
* @param sf
|
||||
*/
|
||||
public void sendFileMessageByAgent(final User creator, final AgentUser agentUser, final MultipartFile multipart, final StreamingFile sf) {
|
||||
public void sendFileMessageByAgent(final User creator, @Nullable final AgentUser agentUser, final MultipartFile multipart, final StreamingFile sf) {
|
||||
// 消息体
|
||||
ChatMessage chatMessage = new ChatMessage();
|
||||
chatMessage.setFilename(multipart.getOriginalFilename());
|
||||
@ -163,23 +146,24 @@ public class AgentProxy {
|
||||
chatMessage.setAttachmentid(sf.getId());
|
||||
chatMessage.setMessage(sf.getFileUrl());
|
||||
chatMessage.setId(MainUtils.getUUID());
|
||||
chatMessage.setContextid(agentUser.getContextid());
|
||||
chatMessage.setAgentserviceid(agentUser.getAgentserviceid());
|
||||
chatMessage.setChannel(agentUser.getChannel());
|
||||
chatMessage.setUsession(agentUser.getUserid());
|
||||
chatMessage.setAppid(agentUser.getAppid());
|
||||
if (agentUser != null) {
|
||||
chatMessage.setContextid(agentUser.getContextid());
|
||||
chatMessage.setAgentserviceid(agentUser.getAgentserviceid());
|
||||
chatMessage.setChannel(agentUser.getChannel());
|
||||
chatMessage.setUsession(agentUser.getUserid());
|
||||
chatMessage.setAppid(agentUser.getAppid());
|
||||
}
|
||||
chatMessage.setUserid(creator.getId());
|
||||
chatMessage.setOrgi(creator.getOrgi());
|
||||
chatMessage.setCreater(creator.getId());
|
||||
chatMessage.setUsername(creator.getUname());
|
||||
|
||||
chatMessage.setCalltype(MainContext.CallType.OUT.toString());
|
||||
if (StringUtils.isNotBlank(agentUser.getAgentno())) {
|
||||
if (agentUser != null && StringUtils.isNotBlank(agentUser.getAgentno())) {
|
||||
chatMessage.setTouser(agentUser.getUserid());
|
||||
}
|
||||
|
||||
if (multipart.getContentType() != null && multipart.getContentType().indexOf(
|
||||
Constants.ATTACHMENT_TYPE_IMAGE) >= 0) {
|
||||
if (multipart.getContentType() != null && multipart.getContentType().contains(Constants.ATTACHMENT_TYPE_IMAGE)) {
|
||||
chatMessage.setMsgtype(MainContext.MediaType.IMAGE.toString());
|
||||
} else {
|
||||
chatMessage.setMsgtype(MainContext.MediaType.FILE.toString());
|
||||
@ -199,23 +183,23 @@ public class AgentProxy {
|
||||
outMessage.setCreatetime(Constants.DISPLAY_DATE_FORMATTER.format(new Date()));
|
||||
outMessage.setMessageType(chatMessage.getMsgtype());
|
||||
|
||||
/**
|
||||
/*
|
||||
* 通知文件上传消息
|
||||
*/
|
||||
// 发送消息给访客
|
||||
peerSyncIM.send(MainContext.ReceiverType.VISITOR,
|
||||
MainContext.ChannelType.toValue(agentUser.getChannel()),
|
||||
agentUser.getAppid(), MainContext.MessageType.MESSAGE,
|
||||
agentUser.getUserid(),
|
||||
outMessage,
|
||||
true);
|
||||
MainContext.ChannelType.toValue(agentUser.getChannel()),
|
||||
agentUser.getAppid(), MainContext.MessageType.MESSAGE,
|
||||
agentUser.getUserid(),
|
||||
outMessage,
|
||||
true);
|
||||
|
||||
// 发送给坐席自己
|
||||
peerSyncIM.send(MainContext.ReceiverType.AGENT,
|
||||
MainContext.ChannelType.WEBIM,
|
||||
agentUser.getAppid(),
|
||||
MainContext.MessageType.MESSAGE,
|
||||
agentUser.getAgentno(), outMessage, true);
|
||||
MainContext.ChannelType.WEBIM,
|
||||
agentUser.getAppid(),
|
||||
MainContext.MessageType.MESSAGE,
|
||||
agentUser.getAgentno(), outMessage, true);
|
||||
|
||||
} else {
|
||||
logger.warn("[sendFileMessageByAgent] agent user chat is end, disable forward files.");
|
||||
@ -225,31 +209,25 @@ public class AgentProxy {
|
||||
|
||||
/**
|
||||
* 将http的multipart保存到数据库
|
||||
*
|
||||
* @param creator
|
||||
* @param multipart
|
||||
* @return
|
||||
* @throws IOException
|
||||
* @throws CSKefuException
|
||||
*/
|
||||
public StreamingFile saveFileIntoMySQLBlob(final User creator, final MultipartFile multipart) throws
|
||||
IOException, CSKefuException {
|
||||
/**
|
||||
/*
|
||||
* 准备文件夹
|
||||
*/
|
||||
File uploadDir = new File(webUploadPath, "upload");
|
||||
if (!uploadDir.exists()) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
uploadDir.mkdirs();
|
||||
}
|
||||
|
||||
String fileid = MainUtils.getUUID();
|
||||
StreamingFile sf = new StreamingFile();
|
||||
|
||||
/**
|
||||
/*
|
||||
* 保存到本地
|
||||
*/
|
||||
if (multipart.getContentType() != null && multipart.getContentType().indexOf(
|
||||
Constants.ATTACHMENT_TYPE_IMAGE) >= 0) {
|
||||
if (multipart.getContentType() != null && multipart.getContentType().contains(Constants.ATTACHMENT_TYPE_IMAGE)) {
|
||||
// 图片
|
||||
// process thumbnail
|
||||
File original = new File(webUploadPath, "upload/" + fileid + "_original");
|
||||
@ -264,7 +242,7 @@ public class AgentProxy {
|
||||
sf.setFileUrl("/res/file.html?id=" + attachmentFile.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
/*
|
||||
* 保存文件到MySQL数据库
|
||||
*/
|
||||
sf.setId(fileid);
|
||||
@ -280,13 +258,6 @@ public class AgentProxy {
|
||||
|
||||
/**
|
||||
* 处理multi part为本地文件
|
||||
*
|
||||
* @param owner
|
||||
* @param multipart
|
||||
* @param fileid
|
||||
* @return
|
||||
* @throws IOException
|
||||
* @throws CSKefuException
|
||||
*/
|
||||
public AttachmentFile processAttachmentFile(
|
||||
final User owner, final MultipartFile multipart,
|
||||
@ -306,14 +277,13 @@ public class AgentProxy {
|
||||
} else {
|
||||
attachmentFile.setFiletype(multipart.getContentType());
|
||||
}
|
||||
File uploadFile = new File(multipart.getOriginalFilename());
|
||||
if (uploadFile.getName() != null && uploadFile.getName().length() > 255) {
|
||||
File uploadFile = new File(Objects.requireNonNull(multipart.getOriginalFilename()));
|
||||
if (uploadFile.getName().length() > 255) {
|
||||
attachmentFile.setTitle(uploadFile.getName().substring(0, 255));
|
||||
} else {
|
||||
attachmentFile.setTitle(uploadFile.getName());
|
||||
}
|
||||
if (StringUtils.isNotBlank(attachmentFile.getFiletype()) && attachmentFile.getFiletype().indexOf(
|
||||
Constants.ATTACHMENT_TYPE_IMAGE) >= 0) {
|
||||
if (StringUtils.isNotBlank(attachmentFile.getFiletype()) && attachmentFile.getFiletype().contains(Constants.ATTACHMENT_TYPE_IMAGE)) {
|
||||
attachmentFile.setImage(true);
|
||||
}
|
||||
attachmentFile.setFileid(fileid);
|
||||
@ -325,10 +295,6 @@ public class AgentProxy {
|
||||
/**
|
||||
* 获得一个User的AgentStatus
|
||||
* 先从缓存读取,再从数据库,还没有就新建
|
||||
*
|
||||
* @param agentno
|
||||
* @param orgi
|
||||
* @return
|
||||
*/
|
||||
public AgentStatus resolveAgentStatusByAgentnoAndOrgi(final String agentno, final String orgi, final HashMap<String, String> skills) {
|
||||
logger.info(
|
||||
@ -340,9 +306,7 @@ public class AgentProxy {
|
||||
agentStatus = agentStatusRes.findOneByAgentnoAndOrgi(agentno, orgi).orElseGet(AgentStatus::new);
|
||||
}
|
||||
|
||||
if (skills != null) {
|
||||
agentStatus.setSkills(skills);
|
||||
}
|
||||
agentStatus.setSkills(skills);
|
||||
|
||||
return agentStatus;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user