Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
go-gitlab
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
go-gitlab
Commits
1a2af71a
Commit
1a2af71a
authored
Sep 22, 2017
by
Sander van Harmelen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor the `UploadFile` function
parent
7d5edaf7
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
33 deletions
+26
-33
projects.go
projects.go
+26
-33
No files found.
projects.go
View file @
1a2af71a
...
...
@@ -22,7 +22,6 @@ import (
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"time"
...
...
@@ -807,61 +806,55 @@ func (s *ProjectsService) DeleteProjectForkRelation(pid int, options ...OptionFu
return
s
.
client
.
Do
(
req
,
nil
)
}
// ProjectFile
upload a
file
// ProjectFile
represents an uploaded project
file
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/projects.html#upload-a-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
// 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
)
(
*
ProjectFile
,
*
Response
,
error
)
{
// 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
))
options
:=
[]
OptionFunc
{
func
(
req
*
http
.
Request
)
error
{
req
.
Method
=
http
.
MethodPost
b
:=
&
bytes
.
Buffer
{}
w
:=
multipart
.
NewWriter
(
b
)
f
,
err
:=
os
.
Open
(
file
)
if
err
!=
nil
{
return
err
}
defer
f
.
Close
()
fw
,
err
:=
w
.
CreateFormFile
(
"file"
,
file
)
if
err
!=
nil
{
return
err
}
_
,
err
=
io
.
Copy
(
fw
,
f
)
if
err
!=
nil
{
return
err
}
w
.
Close
()
f
,
err
:=
os
.
Open
(
file
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
defer
f
.
Close
()
req
.
Body
=
ioutil
.
NopCloser
(
b
)
req
.
ContentLength
=
int64
(
b
.
Len
()
)
b
:=
&
bytes
.
Buffer
{}
w
:=
multipart
.
NewWriter
(
b
)
req
.
Header
.
Set
(
"Content-Type"
,
w
.
FormDataContentType
())
fw
,
err
:=
w
.
CreateFormFile
(
"file"
,
file
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
return
nil
}}
_
,
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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment