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

9
godo/localchat/image.go

@ -119,7 +119,7 @@ func ReceiveImg(msg UdpMessage) ([]string, error) {
if !ok {
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)
if err != nil {
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)
continue
}
savedPaths = append(savedPaths, filePath)
receviePath := filepath.Join(resPath, fileName)
savedPaths = append(savedPaths, receviePath)
}
if len(savedPaths) > 0 {
@ -158,13 +159,13 @@ func ReceiveImg(msg UdpMessage) ([]string, error) {
// 生成随机文件名并保留扩展名
func generateRandomFileNameWithExtension(originalFileName string) (string, error) {
fileNameWithoutExt, fileExt := filepath.Split(originalFileName)
_, fileExt := filepath.Split(originalFileName)
fileExt = strings.TrimPrefix(fileExt, ".")
if fileExt == "" {
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
}

Loading…
Cancel
Save