mirror of
https://github.com/bbernhard/signal-cli-rest-api.git
synced 2026-05-30 15:24:15 +00:00
add support for multiple attachments
This commit is contained in:
parent
e3fb01a2b3
commit
40d856fbef
147
src/main.go
147
src/main.go
@ -15,6 +15,76 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func cleanupTmpFiles(paths []string) {
|
||||||
|
for _, path := range paths {
|
||||||
|
os.Remove(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func send(c *gin.Context, attachmentTmpDir string, signalCliConfig string, number string, message string, recipients []string, base64Attachments []string) {
|
||||||
|
cmd := []string{"--config", signalCliConfig, "-u", number, "send", "-m", message}
|
||||||
|
cmd = append(cmd, recipients...)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
attachmentTmpPaths := []string{}
|
||||||
|
for _, base64Attachment := range base64Attachments {
|
||||||
|
u, err := uuid.NewV4()
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dec, err := base64.StdEncoding.DecodeString(base64Attachment)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fType, err := filetype.Get(dec)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
attachmentTmpPath := attachmentTmpDir + u.String() + "." + fType.Extension
|
||||||
|
attachmentTmpPaths = append(attachmentTmpPaths, attachmentTmpPath)
|
||||||
|
|
||||||
|
f, err := os.Create(attachmentTmpPath)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
if _, err := f.Write(dec); err != nil {
|
||||||
|
cleanupTmpFiles(attachmentTmpPaths)
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := f.Sync(); err != nil {
|
||||||
|
cleanupTmpFiles(attachmentTmpPaths)
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(attachmentTmpPaths) > 0 {
|
||||||
|
cmd = append(cmd, "-a")
|
||||||
|
cmd = append(cmd , attachmentTmpPaths...)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := runSignalCli(cmd)
|
||||||
|
if err != nil {
|
||||||
|
cleanupTmpFiles(attachmentTmpPaths)
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(201, nil)
|
||||||
|
}
|
||||||
|
|
||||||
func runSignalCli(args []string) error {
|
func runSignalCli(args []string) error {
|
||||||
cmd := exec.Command("signal-cli", args...)
|
cmd := exec.Command("signal-cli", args...)
|
||||||
var errBuffer bytes.Buffer
|
var errBuffer bytes.Buffer
|
||||||
@ -52,6 +122,15 @@ func main() {
|
|||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
log.Info("Started Signal Messenger REST API")
|
log.Info("Started Signal Messenger REST API")
|
||||||
|
|
||||||
|
router.GET("/v1/about", func(c *gin.Context) {
|
||||||
|
type About struct {
|
||||||
|
SupportedApiVersions []string `json:"versions"`
|
||||||
|
}
|
||||||
|
|
||||||
|
about := About{SupportedApiVersions: []string{"v1", "v2"}}
|
||||||
|
c.JSON(200, about)
|
||||||
|
})
|
||||||
|
|
||||||
router.POST("/v1/register/:number", func(c *gin.Context) {
|
router.POST("/v1/register/:number", func(c *gin.Context) {
|
||||||
number := c.Param("number")
|
number := c.Param("number")
|
||||||
|
|
||||||
@ -130,60 +209,30 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := []string{"--config", *signalCliConfig, "-u", req.Number, "send", "-m", req.Message}
|
base64Attachments := []string{}
|
||||||
cmd = append(cmd, req.Recipients...)
|
if req.Base64Attachment != "" {
|
||||||
|
base64Attachments = append(base64Attachments, req.Base64Attachment)
|
||||||
attachmentTmpPath := ""
|
|
||||||
if(req.Base64Attachment != "") {
|
|
||||||
u, err := uuid.NewV4()
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
dec, err := base64.StdEncoding.DecodeString(req.Base64Attachment)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
fType, err := filetype.Get(dec)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
attachmentTmpPath := *attachmentTmpDir + u.String() + "." + fType.Extension
|
|
||||||
|
|
||||||
f, err := os.Create(attachmentTmpPath)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
if _, err := f.Write(dec); err != nil {
|
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := f.Sync(); err != nil {
|
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd = append(cmd, "-a")
|
|
||||||
cmd = append(cmd , attachmentTmpPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = runSignalCli(cmd)
|
send(c, *signalCliConfig, *signalCliConfig, req.Number, req.Message, req.Recipients, base64Attachments)
|
||||||
|
})
|
||||||
|
|
||||||
|
router.POST("/v2/send", func(c *gin.Context) {
|
||||||
|
type Request struct{
|
||||||
|
Number string `json:"number"`
|
||||||
|
Recipients []string `json:"recipients"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Base64Attachments []string `json:"base64_attachments"`
|
||||||
|
}
|
||||||
|
var req Request
|
||||||
|
err := c.BindJSON(&req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if attachmentTmpPath != "" {
|
c.JSON(400, "Couldn't process request - invalid request")
|
||||||
os.Remove(attachmentTmpPath)
|
log.Error(err.Error())
|
||||||
}
|
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(201, nil)
|
|
||||||
|
send(c, *attachmentTmpDir, *signalCliConfig, req.Number, req.Message, req.Recipients, req.Base64Attachments)
|
||||||
})
|
})
|
||||||
|
|
||||||
router.Run()
|
router.Run()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user