mirror of
https://github.com/aljazceru/signal-cli-rest-api.git
synced 2025-12-19 23:54:22 +01:00
Support attachment base64 and custom filename.
This commit is contained in:
@@ -4,12 +4,12 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
securejoin "github.com/cyphar/filepath-securejoin"
|
securejoin "github.com/cyphar/filepath-securejoin"
|
||||||
"github.com/gabriel-vasile/mimetype"
|
"github.com/gabriel-vasile/mimetype"
|
||||||
@@ -134,6 +134,16 @@ func cleanupTmpFiles(paths []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cleanupAttachmentEntries(attachmentEntries []AttachmentEntry) {
|
||||||
|
for _, attachmentEntry := range attachmentEntries {
|
||||||
|
if len(attachmentEntry.FilePath) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(attachmentEntry.FilePath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func convertInternalGroupIdToGroupId(internalId string) string {
|
func convertInternalGroupIdToGroupId(internalId string) string {
|
||||||
return groupPrefix + base64.StdEncoding.EncodeToString([]byte(internalId))
|
return groupPrefix + base64.StdEncoding.EncodeToString([]byte(internalId))
|
||||||
}
|
}
|
||||||
@@ -194,7 +204,6 @@ func getContainerId() (string, error) {
|
|||||||
return containerId, nil
|
return containerId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func ConvertGroupIdToInternalGroupId(id string) (string, error) {
|
func ConvertGroupIdToInternalGroupId(id string) (string, error) {
|
||||||
|
|
||||||
groupIdWithoutPrefix := strings.TrimPrefix(id, groupPrefix)
|
groupIdWithoutPrefix := strings.TrimPrefix(id, groupPrefix)
|
||||||
@@ -300,35 +309,41 @@ func (s *SignalClient) send(number string, message string,
|
|||||||
groupId = string(grpId)
|
groupId = string(grpId)
|
||||||
}
|
}
|
||||||
|
|
||||||
attachmentTmpPaths := []string{}
|
attachmentEntries := []AttachmentEntry{}
|
||||||
for _, base64Attachment := range base64Attachments {
|
for _, base64Attachment := range base64Attachments {
|
||||||
|
attachmentEntry := NewAttachmentEntry(base64Attachment)
|
||||||
|
|
||||||
u, err := uuid.NewV4()
|
u, err := uuid.NewV4()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
dec, err := base64.StdEncoding.DecodeString(base64Attachment)
|
dec, err := base64.StdEncoding.DecodeString(attachmentEntry.Base64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
mimeType := mimetype.Detect(dec)
|
mimeType := mimetype.Detect(dec)
|
||||||
|
|
||||||
attachmentTmpPath := s.attachmentTmpDir + u.String() + mimeType.Extension()
|
if attachmentEntry.isWithMetaData() {
|
||||||
attachmentTmpPaths = append(attachmentTmpPaths, attachmentTmpPath)
|
attachmentEntries = append(attachmentEntries, *attachmentEntry)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
attachmentEntry.FilePath = s.attachmentTmpDir + u.String() + mimeType.Extension()
|
||||||
|
attachmentEntries = append(attachmentEntries, *attachmentEntry)
|
||||||
|
|
||||||
f, err := os.Create(attachmentTmpPath)
|
f, err := os.Create(attachmentEntry.FilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
if _, err := f.Write(dec); err != nil {
|
if _, err := f.Write(dec); err != nil {
|
||||||
cleanupTmpFiles(attachmentTmpPaths)
|
cleanupAttachmentEntries(attachmentEntries)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := f.Sync(); err != nil {
|
if err := f.Sync(); err != nil {
|
||||||
cleanupTmpFiles(attachmentTmpPaths)
|
cleanupAttachmentEntries(attachmentEntries)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -354,13 +369,13 @@ func (s *SignalClient) send(number string, message string,
|
|||||||
} else {
|
} else {
|
||||||
request.Recipients = recipients
|
request.Recipients = recipients
|
||||||
}
|
}
|
||||||
if len(attachmentTmpPaths) > 0 {
|
for _, attachmentEntry := range attachmentEntries {
|
||||||
request.Attachments = append(request.Attachments, attachmentTmpPaths...)
|
request.Attachments = append(request.Attachments, attachmentEntry.toDataForSignal())
|
||||||
}
|
}
|
||||||
|
|
||||||
rawData, err := jsonRpc2Client.getRaw("send", request)
|
rawData, err := jsonRpc2Client.getRaw("send", request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanupTmpFiles(attachmentTmpPaths)
|
cleanupAttachmentEntries(attachmentEntries)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -379,14 +394,16 @@ func (s *SignalClient) send(number string, message string,
|
|||||||
cmd = append(cmd, []string{"-g", groupId}...)
|
cmd = append(cmd, []string{"-g", groupId}...)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(attachmentTmpPaths) > 0 {
|
if len(attachmentEntries) > 0 {
|
||||||
cmd = append(cmd, "-a")
|
cmd = append(cmd, "-a")
|
||||||
cmd = append(cmd, attachmentTmpPaths...)
|
for _, attachmentEntry := range attachmentEntries {
|
||||||
|
cmd = append(cmd, attachmentEntry.toDataForSignal())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rawData, err := s.cliClient.Execute(true, cmd, message)
|
rawData, err := s.cliClient.Execute(true, cmd, message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanupTmpFiles(attachmentTmpPaths)
|
cleanupAttachmentEntries(attachmentEntries)
|
||||||
if strings.Contains(err.Error(), signalCliV2GroupError) {
|
if strings.Contains(err.Error(), signalCliV2GroupError) {
|
||||||
return nil, errors.New("Cannot send message to group - please first update your profile.")
|
return nil, errors.New("Cannot send message to group - please first update your profile.")
|
||||||
}
|
}
|
||||||
@@ -394,12 +411,12 @@ func (s *SignalClient) send(number string, message string,
|
|||||||
}
|
}
|
||||||
resp.Timestamp, err = strconv.ParseInt(strings.TrimSuffix(rawData, "\n"), 10, 64)
|
resp.Timestamp, err = strconv.ParseInt(strings.TrimSuffix(rawData, "\n"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanupTmpFiles(attachmentTmpPaths)
|
cleanupAttachmentEntries(attachmentEntries)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanupTmpFiles(attachmentTmpPaths)
|
cleanupAttachmentEntries(attachmentEntries)
|
||||||
|
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1686,7 +1686,8 @@ var doc = `{
|
|||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"base64_attachment": {
|
"base64_attachment": {
|
||||||
"type": "string"
|
"type": "string",
|
||||||
|
"example": "<BASE64 ENCODED DATA> or 'data:<MIME-TYPE>;base64,<BASE64 ENCODED DATA>' or 'data:<MIME-TYPE>;filename=<FILENAME>;base64,<BASE64 ENCODED DATA>'"
|
||||||
},
|
},
|
||||||
"is_group": {
|
"is_group": {
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
@@ -1711,7 +1712,8 @@ var doc = `{
|
|||||||
"base64_attachments": {
|
"base64_attachments": {
|
||||||
"type": "array",
|
"type": "array",
|
||||||
"items": {
|
"items": {
|
||||||
"type": "string"
|
"type": "string",
|
||||||
|
"example": "<BASE64 ENCODED DATA> or 'data:<MIME-TYPE>;base64,<BASE64 ENCODED DATA>' or 'data:<MIME-TYPE>;filename=<FILENAME>;base64,<BASE64 ENCODED DATA>'"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"message": {
|
"message": {
|
||||||
|
|||||||
@@ -1670,7 +1670,8 @@
|
|||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"base64_attachment": {
|
"base64_attachment": {
|
||||||
"type": "string"
|
"type": "string",
|
||||||
|
"example": "<BASE64 ENCODED DATA> or 'data:<MIME-TYPE>;base64,<BASE64 ENCODED DATA>' or 'data:<MIME-TYPE>;filename=<FILENAME>;base64,<BASE64 ENCODED DATA>'"
|
||||||
},
|
},
|
||||||
"is_group": {
|
"is_group": {
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
@@ -1695,7 +1696,8 @@
|
|||||||
"base64_attachments": {
|
"base64_attachments": {
|
||||||
"type": "array",
|
"type": "array",
|
||||||
"items": {
|
"items": {
|
||||||
"type": "string"
|
"type": "string",
|
||||||
|
"example": "<BASE64 ENCODED DATA> or 'data:<MIME-TYPE>;base64,<BASE64 ENCODED DATA>' or 'data:<MIME-TYPE>;filename=<FILENAME>;base64,<BASE64 ENCODED DATA>'"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"message": {
|
"message": {
|
||||||
|
|||||||
@@ -107,6 +107,7 @@ definitions:
|
|||||||
properties:
|
properties:
|
||||||
base64_attachment:
|
base64_attachment:
|
||||||
type: string
|
type: string
|
||||||
|
example: "<BASE64 ENCODED DATA> or 'data:<MIME-TYPE>;base64,<BASE64 ENCODED DATA>' or 'data:<MIME-TYPE>;filename=<FILENAME>;base64,<BASE64 ENCODED DATA>'"
|
||||||
is_group:
|
is_group:
|
||||||
type: boolean
|
type: boolean
|
||||||
message:
|
message:
|
||||||
@@ -123,6 +124,7 @@ definitions:
|
|||||||
base64_attachments:
|
base64_attachments:
|
||||||
items:
|
items:
|
||||||
type: string
|
type: string
|
||||||
|
example: "<BASE64 ENCODED DATA> or 'data:<MIME-TYPE>;base64,<BASE64 ENCODED DATA>' or 'data:<MIME-TYPE>;filename=<FILENAME>;base64,<BASE64 ENCODED DATA>'"
|
||||||
type: array
|
type: array
|
||||||
message:
|
message:
|
||||||
type: string
|
type: string
|
||||||
|
|||||||
Reference in New Issue
Block a user