Browse Source

change sendfile

master
godo 9 months ago
parent
commit
afc393b9e6
  1. 7
      frontend/src/components/localchat/ChatContent.vue
  2. 24
      frontend/src/stores/localchat.ts
  3. 9
      godo/localchat/image.go

7
frontend/src/components/localchat/ChatContent.vue

@ -88,6 +88,13 @@ async function scroll({ scrollTop }: { scrollTop: number }) {
</div> </div>
</div> </div>
</div> </div>
<div v-if="item.type === 'applyfile'">
<div class="file-bubble">
<div class="file-content" v-for="el in item.content" @click="sys.openFile(el)">
<div class="file-name">{{ el }}</div>
</div>
</div>
</div>
<div v-if="item.type === 'image'"> <div v-if="item.type === 'image'">
<div class="file-bubble"> <div class="file-bubble">
<div class="message-content" v-for="el in item.content"> <div class="message-content" v-for="el in item.content">

24
frontend/src/stores/localchat.ts

@ -39,6 +39,7 @@ export const useLocalChatStore = defineStore('localChatStore', () => {
setUserList(ips); setUserList(ips);
} }
if(data.messages){ if(data.messages){
const apiUrl = `${config.apiUrl}/localchat/viewimage?img=`
for(let ip in data.messages){ for(let ip in data.messages){
const msgList:any = data.messages[ip] const msgList:any = data.messages[ip]
if(!msgList || msgList.length < 1)return; if(!msgList || msgList.length < 1)return;
@ -49,11 +50,13 @@ export const useLocalChatStore = defineStore('localChatStore', () => {
//console.log(msg) //console.log(msg)
addText(msg) addText(msg)
} }
if (msg.type === "image"){ else if (msg.type === "image"){
console.log(msg) msg.message = msg.message.map((d: any) => `${apiUrl}${encodeURIComponent(d)}`)
//console.log(msg)
addText(msg)
} }
if (msg.type === "fileSending"){ else if (msg.type === "fileSending"){
addText(msg)
} }
}) })
} }
@ -313,7 +316,7 @@ export const useLocalChatStore = defineStore('localChatStore', () => {
const targetUser: any = await getTargetUser(data) const targetUser: any = await getTargetUser(data)
const saveMsg: any = { const saveMsg: any = {
type: 'text', type: data.type,
targetId: targetUser.id, targetId: targetUser.id,
targetIp: targetUser.ip, targetIp: targetUser.ip,
content: data.message, content: data.message,
@ -326,8 +329,8 @@ export const useLocalChatStore = defineStore('localChatStore', () => {
isRead: false, isRead: false,
status: 'reciped' status: 'reciped'
} }
console.log(saveMsg) // console.log(saveMsg)
console.log(chatTargetId.value) // console.log(chatTargetId.value)
if (targetUser.ip === chatTargetIp.value) { if (targetUser.ip === chatTargetIp.value) {
saveMsg.readAt = Date.now() saveMsg.readAt = Date.now()
saveMsg.isRead = true saveMsg.isRead = true
@ -362,6 +365,7 @@ export const useLocalChatStore = defineStore('localChatStore', () => {
isRead: false, isRead: false,
status: 'sending' status: 'sending'
} }
console.log(saveMsg)
const msgId = await db.addOne('chatmsg', saveMsg) const msgId = await db.addOne('chatmsg', saveMsg)
//await getMsgList() //await getMsgList()
msgList.value.push(saveMsg) msgList.value.push(saveMsg)
@ -393,9 +397,9 @@ export const useLocalChatStore = defineStore('localChatStore', () => {
saveMsg.status = 'sended' saveMsg.status = 'sended'
saveMsg.readAt = Date.now() saveMsg.readAt = Date.now()
await db.update('chatmsg', msgId, saveMsg) await db.update('chatmsg', msgId, saveMsg)
if(type === 'applyfile'){ // if(type === 'applyfile'){
notifySuccess("发送成功!") // notifySuccess("发送成功!")
} // }
} }
} }

9
godo/localchat/image.go

@ -119,7 +119,7 @@ func ReceiveImg(msg UdpMessage) ([]string, error) {
if !ok { if !ok {
continue continue
} }
imgUrl := fmt.Sprintf("http://%s/viewimg?img=%s", msg.IP, url.QueryEscape(p)) imgUrl := fmt.Sprintf("http://%s:56780/localchat/viewimage?img=%s", msg.IP, url.QueryEscape(p))
resp, err := http.Get(imgUrl) resp, err := http.Get(imgUrl)
if err != nil { if err != nil {
log.Printf("Failed to download image from URL %s: %v", imgUrl, err) log.Printf("Failed to download image from URL %s: %v", imgUrl, err)
@ -145,7 +145,8 @@ func ReceiveImg(msg UdpMessage) ([]string, error) {
log.Printf("Failed to save image to %s: %v", filePath, err) log.Printf("Failed to save image to %s: %v", filePath, err)
continue continue
} }
savedPaths = append(savedPaths, filePath) receviePath := filepath.Join(resPath, fileName)
savedPaths = append(savedPaths, receviePath)
} }
if len(savedPaths) > 0 { if len(savedPaths) > 0 {
@ -158,13 +159,13 @@ func ReceiveImg(msg UdpMessage) ([]string, error) {
// 生成随机文件名并保留扩展名 // 生成随机文件名并保留扩展名
func generateRandomFileNameWithExtension(originalFileName string) (string, error) { func generateRandomFileNameWithExtension(originalFileName string) (string, error) {
fileNameWithoutExt, fileExt := filepath.Split(originalFileName) _, fileExt := filepath.Split(originalFileName)
fileExt = strings.TrimPrefix(fileExt, ".") fileExt = strings.TrimPrefix(fileExt, ".")
if fileExt == "" { if fileExt == "" {
fileExt = "png" fileExt = "png"
} }
randomFileName := fmt.Sprintf("%s_%s.%s", fileNameWithoutExt, strconv.FormatInt(time.Now().UnixNano(), 10), fileExt) randomFileName := fmt.Sprintf("%s.%s", strconv.FormatInt(time.Now().UnixNano(), 10), fileExt)
return randomFileName, nil return randomFileName, nil
} }

Loading…
Cancel
Save