slack-go / slack

Slack API in Go, originally by @nlopes; Maintainers needed, contact @parsley42
https://pkg.go.dev/github.com/slack-go/slack
BSD 2-Clause "Simplified" License
4.66k stars 1.14k forks source link

UploadFileV2 uploads incorrectly when using text `Content` parameter #1290

Closed calebmckay closed 3 months ago

calebmckay commented 4 months ago

What happened

When calling UploadFileV2() with a string passed to the Content parameter, the file is incorrectly uploaded. This seems to be because old files.upload endpoint allowed text content to be passed as a POST parameter, but the new upload URL interprets the unexpected content= parameter as the start of the file content, and doesn't correctly URL-decode it.

Screenshot 2024-05-23 at 8 23 05 PM

Expected behavior

Screenshot 2024-05-23 at 8 25 09 PM

Steps to reproduce

reproducible code

package main

import (
    "os"

    "github.com/slack-go/slack"
)

func main() {
    token := os.Getenv("SLACK_AUTH_TOKEN")
    channelId := os.Getenv("SLACK_CHANNEL_ID")
    client := slack.New(token, slack.OptionDebug(true))

    fileContent := "This is test file content"
    fileSize := len(fileContent)

    fileParams := slack.UploadFileV2Parameters{
        Channel:        channelId,
        Content:        fileContent,
        Filename:       "file.txt",
        FileSize:       fileSize,
        InitialComment: "Test file",
        Title:          "Test file",
    }
    client.UploadFileV2(fileParams)
}

This seems to only be an issue with the Content parameter, as converting the string to a Reader fixes the problem:

reader := strings.NewReader(fileContent)

fileParams := slack.UploadFileV2Parameters{
    Channel:        channelId,
    Reader:         reader,
    Filename:       "file.txt",
    FileSize:       fileSize,
    InitialComment: "Test file",
    Title:          "Test file",
}
client.UploadFileV2(fileParams)

Versions