Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
16b1c0e6
Commit
16b1c0e6
authored
Sep 15, 2017
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
minor code adjusetments
parent
c7698a09
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
22 deletions
+14
-22
pkg/components/imguploader/gcsuploader.go
+14
-22
No files found.
pkg/components/imguploader/gcsuploader.go
View file @
16b1c0e6
...
@@ -2,7 +2,6 @@ package imguploader
...
@@ -2,7 +2,6 @@ package imguploader
import
(
import
(
"context"
"context"
"errors"
"fmt"
"fmt"
"io/ioutil"
"io/ioutil"
"net/http"
"net/http"
...
@@ -13,6 +12,11 @@ import (
...
@@ -13,6 +12,11 @@ import (
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/google"
)
)
const
(
tokenUrl
string
=
"https://www.googleapis.com/auth/devstorage.read_write"
uploadUrl
string
=
"https://www.googleapis.com/upload/storage/v1/b/%s/o?uploadType=media&name=%s&predefinedAcl=publicRead"
)
type
GCSUploader
struct
{
type
GCSUploader
struct
{
keyFile
string
keyFile
string
bucket
string
bucket
string
...
@@ -30,24 +34,20 @@ func NewGCSUploader(keyFile, bucket string) *GCSUploader {
...
@@ -30,24 +34,20 @@ func NewGCSUploader(keyFile, bucket string) *GCSUploader {
func
(
u
*
GCSUploader
)
Upload
(
ctx
context
.
Context
,
imageDiskPath
string
)
(
string
,
error
)
{
func
(
u
*
GCSUploader
)
Upload
(
ctx
context
.
Context
,
imageDiskPath
string
)
(
string
,
error
)
{
key
:=
util
.
GetRandomString
(
20
)
+
".png"
key
:=
util
.
GetRandomString
(
20
)
+
".png"
log
.
Debug
(
"Opening key file "
,
u
.
keyFile
)
u
.
log
.
Debug
(
"Opening key file "
,
u
.
keyFile
)
data
,
err
:=
ioutil
.
ReadFile
(
u
.
keyFile
)
data
,
err
:=
ioutil
.
ReadFile
(
u
.
keyFile
)
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
err
return
""
,
err
}
}
log
.
Debug
(
"Creating JWT conf"
)
u
.
log
.
Debug
(
"Creating JWT conf"
)
conf
,
err
:=
google
.
JWTConfigFromJSON
(
data
,
tokenUrl
)
conf
,
err
:=
google
.
JWTConfigFromJSON
(
data
,
"https://www.googleapis.com/auth/devstorage.read_write"
)
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
err
return
""
,
err
}
}
log
.
Debug
(
"Creating HTTP client"
)
u
.
log
.
Debug
(
"Creating HTTP client"
)
client
:=
conf
.
Client
(
ctx
)
client
:=
conf
.
Client
(
ctx
)
err
=
u
.
uploadFile
(
client
,
imageDiskPath
,
key
)
err
=
u
.
uploadFile
(
client
,
imageDiskPath
,
key
)
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
err
return
""
,
err
...
@@ -57,20 +57,15 @@ func (u *GCSUploader) Upload(ctx context.Context, imageDiskPath string) (string,
...
@@ -57,20 +57,15 @@ func (u *GCSUploader) Upload(ctx context.Context, imageDiskPath string) (string,
}
}
func
(
u
*
GCSUploader
)
uploadFile
(
client
*
http
.
Client
,
imageDiskPath
,
key
string
)
error
{
func
(
u
*
GCSUploader
)
uploadFile
(
client
*
http
.
Client
,
imageDiskPath
,
key
string
)
error
{
log
.
Debug
(
"Opening image file "
,
imageDiskPath
)
u
.
log
.
Debug
(
"Opening image file "
,
imageDiskPath
)
fileReader
,
err
:=
os
.
Open
(
imageDiskPath
)
fileReader
,
err
:=
os
.
Open
(
imageDiskPath
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
reqUrl
:=
fmt
.
Sprintf
(
reqUrl
:=
fmt
.
Sprintf
(
uploadUrl
,
u
.
bucket
,
key
)
"https://www.googleapis.com/upload/storage/v1/b/%s/o?uploadType=media&name=%s&predefinedAcl=publicRead"
,
u
.
log
.
Debug
(
"Request URL: "
,
reqUrl
)
u
.
bucket
,
key
,
)
log
.
Debug
(
"Request URL: "
,
reqUrl
)
req
,
err
:=
http
.
NewRequest
(
"POST"
,
reqUrl
,
fileReader
)
req
,
err
:=
http
.
NewRequest
(
"POST"
,
reqUrl
,
fileReader
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -78,18 +73,15 @@ func (u *GCSUploader) uploadFile(client *http.Client, imageDiskPath, key string)
...
@@ -78,18 +73,15 @@ func (u *GCSUploader) uploadFile(client *http.Client, imageDiskPath, key string)
}
}
req
.
Header
.
Add
(
"Content-Type"
,
"image/png"
)
req
.
Header
.
Add
(
"Content-Type"
,
"image/png"
)
u
.
log
.
Debug
(
"Sending POST request to GCS"
)
log
.
Debug
(
"Sending POST request to GCS"
)
resp
,
err
:=
client
.
Do
(
req
)
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
log
.
Debug
(
"GCS API response header"
,
resp
.
Header
)
if
resp
.
StatusCode
!=
200
{
if
resp
.
StatusCode
!=
200
{
return
errors
.
New
(
fmt
.
Sprintf
(
"GCS response status code %d"
,
resp
.
StatusCode
)
)
return
fmt
.
Errorf
(
"GCS response status code %d"
,
resp
.
StatusCode
)
}
}
return
nil
return
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