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 (
...
@@ -22,7 +22,6 @@ import (
"io"
"io"
"io/ioutil"
"io/ioutil"
"mime/multipart"
"mime/multipart"
"net/http"
"net/url"
"net/url"
"os"
"os"
"time"
"time"
...
@@ -807,61 +806,55 @@ func (s *ProjectsService) DeleteProjectForkRelation(pid int, options ...OptionFu
...
@@ -807,61 +806,55 @@ func (s *ProjectsService) DeleteProjectForkRelation(pid int, options ...OptionFu
return
s
.
client
.
Do
(
req
,
nil
)
return
s
.
client
.
Do
(
req
,
nil
)
}
}
// ProjectFile
upload a
file
// ProjectFile
represents an uploaded project
file
//
//
// GitLab API docs:
// GitLab API docs: https://docs.gitlab.com/ce/api/projects.html#upload-a-file
// https://docs.gitlab.com/ce/api/projects.html#upload-a-file
type
ProjectFile
struct
{
type
ProjectFile
struct
{
Alt
string
`json:"alt"`
Alt
string
`json:"alt"`
URL
string
`json:"url"`
URL
string
`json:"url"`
Markdown
string
`json:"markdown"`
Markdown
string
`json:"markdown"`
}
}
// UploadFile upload a file
// UploadFile upload a file
from disk
//
//
// GitLab API docs:
// GitLab API docs: https://docs.gitlab.com/ce/api/projects.html#upload-a-file
// https://docs.gitlab.com/ce/api/projects.html#upload-a-file
func
(
s
*
ProjectsService
)
UploadFile
(
pid
interface
{},
file
string
,
options
...
OptionFunc
)
(
*
ProjectFile
,
*
Response
,
error
)
{
func
(
s
*
ProjectsService
)
UploadFile
(
pid
interface
{},
file
string
)
(
*
ProjectFile
,
*
Response
,
error
)
{
project
,
err
:=
parseID
(
pid
)
project
,
err
:=
parseID
(
pid
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
nil
,
err
return
nil
,
nil
,
err
}
}
u
:=
fmt
.
Sprintf
(
"projects/%s/uploads"
,
url
.
QueryEscape
(
project
))
u
:=
fmt
.
Sprintf
(
"projects/%s/uploads"
,
url
.
QueryEscape
(
project
))
options
:=
[]
OptionFunc
{
func
(
req
*
http
.
Request
)
error
{
f
,
err
:=
os
.
Open
(
file
)
req
.
Method
=
http
.
MethodPost
if
err
!=
nil
{
return
nil
,
nil
,
err
b
:=
&
bytes
.
Buffer
{}
}
w
:=
multipart
.
NewWriter
(
b
)
defer
f
.
Close
()
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
()
req
.
Body
=
ioutil
.
NopCloser
(
b
)
b
:=
&
bytes
.
Buffer
{}
req
.
ContentLength
=
int64
(
b
.
Len
()
)
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
)
req
,
err
:=
s
.
client
.
NewRequest
(
""
,
u
,
nil
,
options
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
nil
,
err
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
{}
uf
:=
&
ProjectFile
{}
resp
,
err
:=
s
.
client
.
Do
(
req
,
uf
)
resp
,
err
:=
s
.
client
.
Do
(
req
,
uf
)
if
err
!=
nil
{
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