Skip to content

Commit

Permalink
Media message file downloads can be configured to be tried only once.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoehermann committed Dec 12, 2019
1 parent 133dddc commit b26d85a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions constants.h
Expand Up @@ -22,6 +22,7 @@ CHARCONSTANT(GOWHATSAPP_MESSAGE_ID_STORE_SIZE_OPTION, "message-id-store-size");
CHARCONSTANT(GOWHATSAPP_TIMESTAMP_FILTERING_OPTION, "message-timestamp-filter");
CHARCONSTANT(GOWHATSAPP_SYSTEM_MESSAGES_ARE_ORDINARY_MESSAGES_OPTION, "system-messages-are-ordinary-messages");
CHARCONSTANT(GOWHATSAPP_DOWNLOAD_ATTACHMENTS_OPTION, "download-attachments");
CHARCONSTANT(GOWHATSAPP_DOWNLOAD_TRY_ONLY_ONCE_OPTION, "download-try-only-once");
CHARCONSTANT(GOWHATSAPP_INLINE_IMAGES_OPTION, "inline-images");

/*
Expand Down
7 changes: 7 additions & 0 deletions libgowhatsapp.c
Expand Up @@ -413,6 +413,13 @@ gowhatsapp_add_account_options(GList *account_options)
);
account_options = g_list_append(account_options, option);

option = purple_account_option_bool_new(
_("Try to download once only"),
GOWHATSAPP_DOWNLOAD_TRY_ONLY_ONCE_OPTION,
TRUE
);
account_options = g_list_append(account_options, option);

option = purple_account_option_bool_new(
_("Display images in conversation window after download"),
GOWHATSAPP_INLINE_IMAGES_OPTION,
Expand Down
21 changes: 17 additions & 4 deletions purplegwa-media.go
Expand Up @@ -89,11 +89,11 @@ func (handler *waHandler) storeDownloadedData(filename string, data []byte) erro
file, err := os.Create(filename)
defer file.Close()
if err != nil {
return fmt.Errorf("Data was downloaded, but file %s creation failed due to %v", filename, err)
return fmt.Errorf("File %s creation failed due to %v.", filename, err)
} else {
_, err := file.Write(data)
if err != nil {
return fmt.Errorf("Data was downloaded, but could not be written to file %s due to %v", filename, err)
return fmt.Errorf("Data could not be written to file %s due to %v.", filename, err)
} else {
return nil
}
Expand All @@ -104,15 +104,28 @@ type downloadable interface {
Download() ([]byte, error)
}

func (handler *waHandler) presentDownloadableMessage(message downloadable, info whatsapp.MessageInfo, downloadsEnabled bool, inline bool) []byte {
func (handler *waHandler) presentDownloadableMessage(message downloadable, info whatsapp.MessageInfo, downloadsEnabled bool, storeFailedDownload bool, inline bool) []byte {
filename, wtd := handler.wantToDownload(info)
if wtd {
if downloadsEnabled {
if isSaneId(info.Id) {
data, err := message.Download()
if err != nil {
retryComment := ""
if storeFailedDownload {
errStore := handler.storeDownloadedData(filename, make([]byte, 0))
if errStore != nil {
retryComment = "Will not try to download again."
} else {
// for some odd reason, errStore is always set, even on success, yet empty
// TODO: find out how to handle this properly. os.Truncate does not create files
//retryComment = fmt.Sprintf("Unable to mark download as failed (%v). Will try to download again.", errStore)
}
} else {
retryComment = "Retrying on next occasion is enabled."
}
handler.presentMessage(makeConversationErrorMessage(info,
fmt.Sprintf("A media message (ID %s) was received, but the download failed: %v", info.Id, err)))
fmt.Sprintf("A media message (ID %s) was received, but the download failed: %v. %s", info.Id, err, retryComment)))
} else {
if inline {
return data
Expand Down
3 changes: 2 additions & 1 deletion purplegwa.go
Expand Up @@ -239,7 +239,8 @@ func makeConversationErrorMessage(originalInfo whatsapp.MessageInfo, errorMessag

func (handler *waHandler) handleDownloadableMessage(message downloadable, info whatsapp.MessageInfo, inline bool) []byte {
downloadsEnabled := Cint_to_bool(C.gowhatsapp_account_get_bool(C.gowhatsapp_get_account(handler.connID), C.GOWHATSAPP_DOWNLOAD_ATTACHMENTS_OPTION, 0))
return handler.presentDownloadableMessage(message, info, downloadsEnabled, inline)
storeFailedDownload := Cint_to_bool(C.gowhatsapp_account_get_bool(C.gowhatsapp_get_account(handler.connID), C.GOWHATSAPP_DOWNLOAD_TRY_ONLY_ONCE_OPTION, 1))
return handler.presentDownloadableMessage(message, info, downloadsEnabled, storeFailedDownload, inline)
}

func (handler *waHandler) HandleTextMessage(message whatsapp.TextMessage) {
Expand Down

0 comments on commit b26d85a

Please sign in to comment.