Commit b3ac6922 authored by Sander van Harmelen's avatar Sander van Harmelen Committed by GitHub

Merge pull request #215 from xanzy/longkey1

Refactor PR #204
parents d6ed0f17 1a2af71a
......@@ -17,8 +17,13 @@
package gitlab
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/url"
"os"
"time"
)
......@@ -800,3 +805,61 @@ func (s *ProjectsService) DeleteProjectForkRelation(pid int, options ...OptionFu
return s.client.Do(req, nil)
}
// ProjectFile represents an uploaded project file
//
// GitLab API docs: https://docs.gitlab.com/ce/api/projects.html#upload-a-file
type ProjectFile struct {
Alt string `json:"alt"`
URL string `json:"url"`
Markdown string `json:"markdown"`
}
// UploadFile upload a file from disk
//
// GitLab API docs: https://docs.gitlab.com/ce/api/projects.html#upload-a-file
func (s *ProjectsService) UploadFile(pid interface{}, file string, options ...OptionFunc) (*ProjectFile, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/uploads", url.QueryEscape(project))
f, err := os.Open(file)
if err != nil {
return nil, nil, err
}
defer f.Close()
b := &bytes.Buffer{}
w := multipart.NewWriter(b)
fw, err := w.CreateFormFile("file", file)
if err != nil {
return nil, nil, err
}
_, err = io.Copy(fw, f)
if err != nil {
return nil, nil, err
}
w.Close()
req, err := s.client.NewRequest("", u, nil, options)
if err != nil {
return nil, nil, err
}
req.Body = ioutil.NopCloser(b)
req.ContentLength = int64(b.Len())
req.Header.Set("Content-Type", w.FormDataContentType())
req.Method = "POST"
uf := &ProjectFile{}
resp, err := s.client.Do(req, uf)
if err != nil {
return nil, resp, err
}
return uf, resp, nil
}
......@@ -2,8 +2,11 @@ package gitlab
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"reflect"
"strings"
"testing"
)
......@@ -161,3 +164,42 @@ func TestCreateProject(t *testing.T) {
t.Errorf("Projects.CreateProject returned %+v, want %+v", project, want)
}
}
func TestUploadFile(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
tf, _ := ioutil.TempFile(os.TempDir(), "test")
defer os.Remove(tf.Name())
mux.HandleFunc("/projects/1/uploads", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
if false == strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data;") {
t.Fatalf("Prokects.UploadFile request content-type %+v want multipart/form-data;", r.Header.Get("Content-Type"))
}
if r.ContentLength == -1 {
t.Fatalf("Prokects.UploadFile request content-length is -1")
}
fmt.Fprint(w, `{
"alt": "dk",
"url": "/uploads/66dbcd21ec5d24ed6ea225176098d52b/dk.md",
"markdown": "![dk](/uploads/66dbcd21ec5d24ed6ea225176098d52b/dk.png)"
}`)
})
want := &ProjectFile{
Alt: "dk",
URL: "/uploads/66dbcd21ec5d24ed6ea225176098d52b/dk.md",
Markdown: "![dk](/uploads/66dbcd21ec5d24ed6ea225176098d52b/dk.png)",
}
file, _, err := client.Projects.UploadFile(1, tf.Name())
if err != nil {
t.Fatalf("Prokects.UploadFile returns an error: %v", err)
}
if !reflect.DeepEqual(want, file) {
t.Errorf("Prokects.UploadFile returned %+v, want %+v", file, want)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment