Skip to content

Commit

Permalink
whatsapp attachments to use extension of detected file type
Browse files Browse the repository at this point in the history
  • Loading branch information
d99kris committed Oct 20, 2024
1 parent 71be0b8 commit ad187ab
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lib/common/src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

#pragma once

#define NCHAT_VERSION "5.3.2"
#define NCHAT_VERSION "5.3.3"
60 changes: 35 additions & 25 deletions lib/wmchat/go/gowm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ import (
"fmt"
"io"
"io/ioutil"
"math"
"mime"
"os"
"os/exec"
"path/filepath"
"runtime"
"slices"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -467,6 +470,29 @@ func ParseWebMessageInfo(selfJid types.JID, chatJid types.JID, webMsg *waWeb.Web
return &info
}

func SliceIndex(list []string, value string, defaultValue int) int {
index := slices.Index(list, value)
if index == -1 {
index = defaultValue
}
return index
}

func ExtensionByType(mimeType string, defaultExt string) string {
ext := defaultExt
exts, extErr := mime.ExtensionsByType(mimeType)
if extErr == nil && len(exts) > 0 {
// prefer common extensions over less common (.jpe, etc) returned by mime library
preferredExts := []string{".jpg", ".jpeg"}
sort.Slice(exts, func(i, j int) bool {
return SliceIndex(preferredExts, exts[i], math.MaxInt32) < SliceIndex(preferredExts, exts[j], math.MaxInt32)
})
ext = exts[0]
}

return ext
}

// logger
type ncLogger struct{}

Expand Down Expand Up @@ -1084,11 +1110,7 @@ func (handler *WmEventHandler) HandleImageMessage(messageInfo types.MessageInfo,
}

// get extension
ext := "jpg"
exts, extErr := mime.ExtensionsByType(img.GetMimetype())
if extErr != nil && len(exts) > 0 {
ext = exts[0]
}
ext := ExtensionByType(img.GetMimetype(), ".jpg")

// text
text := img.GetCaption()
Expand All @@ -1102,7 +1124,7 @@ func (handler *WmEventHandler) HandleImageMessage(messageInfo types.MessageInfo,

// file id, path and status
var tmpPath string = GetPath(connId) + "/tmp"
filePath := fmt.Sprintf("%s/%s.%s", tmpPath, messageInfo.ID, ext)
filePath := fmt.Sprintf("%s/%s%s", tmpPath, messageInfo.ID, ext)
fileId := DownloadableMessageToFileId(client, img, filePath)
fileStatus := FileStatusNotDownloaded

Expand Down Expand Up @@ -1137,11 +1159,7 @@ func (handler *WmEventHandler) HandleVideoMessage(messageInfo types.MessageInfo,
}

// get extension
ext := "mp4"
exts, extErr := mime.ExtensionsByType(vid.GetMimetype())
if extErr != nil && len(exts) > 0 {
ext = exts[0]
}
ext := ExtensionByType(vid.GetMimetype(), ".mp4")

// text
text := vid.GetCaption()
Expand All @@ -1155,7 +1173,7 @@ func (handler *WmEventHandler) HandleVideoMessage(messageInfo types.MessageInfo,

// file id, path and status
var tmpPath string = GetPath(connId) + "/tmp"
filePath := fmt.Sprintf("%s/%s.%s", tmpPath, messageInfo.ID, ext)
filePath := fmt.Sprintf("%s/%s%s", tmpPath, messageInfo.ID, ext)
fileId := DownloadableMessageToFileId(client, vid, filePath)
fileStatus := FileStatusNotDownloaded

Expand Down Expand Up @@ -1190,11 +1208,7 @@ func (handler *WmEventHandler) HandleAudioMessage(messageInfo types.MessageInfo,
}

// get extension
ext := "ogg"
exts, extErr := mime.ExtensionsByType(aud.GetMimetype())
if extErr != nil && len(exts) > 0 {
ext = exts[0]
}
ext := ExtensionByType(aud.GetMimetype(), ".ogg")

// text
text := ""
Expand All @@ -1208,7 +1222,7 @@ func (handler *WmEventHandler) HandleAudioMessage(messageInfo types.MessageInfo,

// file id, path and status
var tmpPath string = GetPath(connId) + "/tmp"
filePath := fmt.Sprintf("%s/%s.%s", tmpPath, messageInfo.ID, ext)
filePath := fmt.Sprintf("%s/%s%s", tmpPath, messageInfo.ID, ext)
fileId := DownloadableMessageToFileId(client, aud, filePath)
fileStatus := FileStatusNotDownloaded

Expand Down Expand Up @@ -1289,14 +1303,10 @@ func (handler *WmEventHandler) HandleStickerMessage(messageInfo types.MessageInf
}

// get extension
ext := "webp"
exts, extErr := mime.ExtensionsByType(sticker.GetMimetype())
if extErr != nil && len(exts) > 0 {
ext = exts[0]
}
ext := ExtensionByType(sticker.GetMimetype(), ".webp")

// text
text := "[Sticker]"
text := ""

// context
quotedId := ""
Expand All @@ -1307,7 +1317,7 @@ func (handler *WmEventHandler) HandleStickerMessage(messageInfo types.MessageInf

// file id, path and status
var tmpPath string = GetPath(connId) + "/tmp"
filePath := fmt.Sprintf("%s/%s.%s", tmpPath, messageInfo.ID, ext)
filePath := fmt.Sprintf("%s/%s%s", tmpPath, messageInfo.ID, ext)
fileId := DownloadableMessageToFileId(client, sticker, filePath)
fileStatus := FileStatusNotDownloaded

Expand Down
3 changes: 2 additions & 1 deletion lib/wmchat/src/wmchat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,8 @@ void WmNewContactsNotify(int p_ConnId, char* p_ChatId, char* p_Name, char* p_Pho
free(p_Name);
}

void WmNewChatsNotify(int p_ConnId, char* p_ChatId, int p_IsUnread, int p_IsMuted, int p_IsPinned, int p_LastMessageTime)
void WmNewChatsNotify(int p_ConnId, char* p_ChatId, int p_IsUnread, int p_IsMuted, int p_IsPinned,
int p_LastMessageTime)
{
WmChat* instance = WmChat::GetInstance(p_ConnId);
if (instance == nullptr) return;
Expand Down
3 changes: 2 additions & 1 deletion lib/wmchat/src/wmchat.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ class WmChat : public Protocol

extern "C" {
void WmNewContactsNotify(int p_ConnId, char* p_ChatId, char* p_Name, char* p_Phone, int p_IsSelf);
void WmNewChatsNotify(int p_ConnId, char* p_ChatId, int p_IsUnread, int p_IsMuted, int p_IsPinned, int p_LastMessageTime);
void WmNewChatsNotify(int p_ConnId, char* p_ChatId, int p_IsUnread, int p_IsMuted, int p_IsPinned,
int p_LastMessageTime);
void WmNewMessagesNotify(int p_ConnId, char* p_ChatId, char* p_MsgId, char* p_SenderId, char* p_Text, int p_FromMe,
char* p_ReplyId, char* p_FileId, char* p_FilePath, int p_FileStatus, int p_TimeSent,
int p_IsRead);
Expand Down
2 changes: 1 addition & 1 deletion src/nchat.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\" DO NOT MODIFY THIS FILE! It was generated by help2man.
.TH NCHAT "1" "September 2024" "nchat 5.3.2" "User Commands"
.TH NCHAT "1" "October 2024" "nchat 5.3.3" "User Commands"
.SH NAME
nchat \- ncurses chat
.SH SYNOPSIS
Expand Down
3 changes: 2 additions & 1 deletion src/uistatusview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ void UiStatusView::Draw()
else
{
static bool isMultipleProfiles = m_Model->IsMultipleProfiles();
std::string profileDisplayName = isMultipleProfiles ? " @ " + m_Model->GetProfileDisplayName(currentChat.first) : "";
std::string profileDisplayName = isMultipleProfiles ? " @ " +
m_Model->GetProfileDisplayName(currentChat.first) : "";

std::string chatStatus = m_Model->GetChatStatus(currentChat.first, currentChat.second);
wstatus = std::wstring(statusVPad, ' ') +
Expand Down

0 comments on commit ad187ab

Please sign in to comment.