grim/youtrack-import

Parents f19a2cb91197
Children e9fca42fc97f
Use the built in mime stuff to figure out the content type for attachments
--- a/youtrack/attachment.go Tue Jan 14 04:41:41 2020 -0600
+++ b/youtrack/attachment.go Tue Jan 14 04:59:25 2020 -0600
@@ -3,9 +3,12 @@
import (
"fmt"
"io"
+ "mime"
"mime/multipart"
"net/http"
+ "net/textproto"
"net/url"
+ "path/filepath"
"strings"
"time"
)
@@ -43,8 +46,34 @@
return a.payload.String(), nil
}
+var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
+
+func escapeQuotes(s string) string {
+ return quoteEscaper.Replace(s)
+}
+
func (a *Attachment) AddFile(r io.ReadCloser, filename string) error {
- w, err := a.writer.CreateFormFile(filename, filename)
+ header := textproto.MIMEHeader{}
+
+ header.Set(
+ "Content-Disposition",
+ fmt.Sprintf("form-data; name=%q; filename=%q",
+ escapeQuotes(filename),
+ escapeQuotes(filename),
+ ),
+ )
+
+ contentType := ""
+ ext := filepath.Ext(filename)
+ if ext != "" {
+ contentType = mime.TypeByExtension(ext)
+ }
+ if contentType == "" {
+ contentType = "application/octet-stream"
+ }
+ header.Set("Content-Type", contentType)
+
+ w, err := a.writer.CreatePart(header)
if err != nil {
return err
}