mirror of
https://github.com/KevinMidboe/planetposen-images.git
synced 2025-10-28 21:00:12 +00:00
Also return google bucket url in response
This commit is contained in:
@@ -9,9 +9,9 @@ import (
|
|||||||
|
|
||||||
// Config contains environment variables.
|
// Config contains environment variables.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Hostname string `envconfig:"HOSTNAME"`
|
Hostname string `envconfig:"HOSTNAME"`
|
||||||
GCSBucket string `envconfig:"GCS_BUCKET" default:"p"`
|
GCSBucket string `envconfig:"GCS_BUCKET" default:"p"`
|
||||||
Port string `envconfig:"PORT" default:"8000"`
|
Port string `envconfig:"PORT" default:"8000"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadConfig reads environment variables, populates and returns Config.
|
// LoadConfig reads environment variables, populates and returns Config.
|
||||||
@@ -25,4 +25,4 @@ func LoadConfig() (*Config, error) {
|
|||||||
err := envconfig.Process("", &c)
|
err := envconfig.Process("", &c)
|
||||||
|
|
||||||
return &c, err
|
return &c, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,9 @@ import (
|
|||||||
|
|
||||||
// MessageImage is a representation of a single image in the database
|
// MessageImage is a representation of a single image in the database
|
||||||
type Image struct {
|
type Image struct {
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
URL string `json:"url,omitempty"`
|
URL string `json:"url,omitempty"`
|
||||||
|
RemoteURL string `json:"remote_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetURL gets URL of the image, also in cases where MessageImage.URL is not defined.
|
// GetURL gets URL of the image, also in cases where MessageImage.URL is not defined.
|
||||||
|
|||||||
@@ -2,26 +2,19 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/kevinmidboe/planetposen-images/util"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
// "github.com/sirupsen/logrus"
|
|
||||||
// "encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
"github.com/kevinmidboe/planetposen-images/clients/gcs"
|
"github.com/kevinmidboe/planetposen-images/clients/gcs"
|
||||||
"github.com/kevinmidboe/planetposen-images/image"
|
"github.com/kevinmidboe/planetposen-images/image"
|
||||||
// "github.com/dbmedialab/dearheart/event"
|
"github.com/kevinmidboe/planetposen-images/util"
|
||||||
// "github.com/dbmedialab/dearheart/server/internal/serverutils"
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
// "strconv"
|
"strings"
|
||||||
// "strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// UploadImages takes a request with file form and uploads the content to GCS
|
// UploadImages takes a request with file form and uploads the content to GCS
|
||||||
func UploadImages(hostname string, gcsClient gcs.Client) http.HandlerFunc {
|
func UploadImages(hostname string, bucketname string, gcsClient gcs.Client) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Get initial protocol data
|
// Get initial protocol data
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
@@ -54,9 +47,12 @@ func UploadImages(hostname string, gcsClient gcs.Client) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
finalURL := util.ImageURL(hostname, string(path))
|
finalURL := util.ImageURL(hostname, string(path))
|
||||||
|
decodedPath, err := path.Decode()
|
||||||
|
remoteURL := util.ImageRemoteURL(bucketname, string(decodedPath))
|
||||||
responseStruct := image.Image{
|
responseStruct := image.Image{
|
||||||
Path: string(path),
|
Path: string(path),
|
||||||
URL: finalURL,
|
URL: finalURL,
|
||||||
|
RemoteURL: remoteURL,
|
||||||
}
|
}
|
||||||
logger.UploadSuccessMessage(string(path), finalURL)
|
logger.UploadSuccessMessage(string(path), finalURL)
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ func (s *Server) setupRoutes() {
|
|||||||
s.Router.HandleFunc("/_healthz", handler.Healthz).Methods("GET").Name("Health")
|
s.Router.HandleFunc("/_healthz", handler.Healthz).Methods("GET").Name("Health")
|
||||||
|
|
||||||
api := s.Router.PathPrefix(v1API).Subrouter()
|
api := s.Router.PathPrefix(v1API).Subrouter()
|
||||||
api.HandleFunc("/images", handler.UploadImages(s.Config.Hostname, s.GCSClient)).Methods("POST").Name("UploadImages")
|
api.HandleFunc("/images", handler.UploadImages(s.Config.Hostname, s.Config.GCSBucket, s.GCSClient)).Methods("POST").Name("UploadImages")
|
||||||
api.HandleFunc("/images", handler.ListImages(s.GCSClient)).Methods("GET").Name("ListImages")
|
api.HandleFunc("/images", handler.ListImages(s.GCSClient)).Methods("GET").Name("ListImages")
|
||||||
|
|
||||||
// Raw image fetcher
|
// Raw image fetcher
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
// ImageURL creates imageURL from hostname and image name
|
// ImageURL creates imageURL from hostname and image name
|
||||||
func ImageURL(hostname, name string) string {
|
func ImageURL(hostname, name string) string {
|
||||||
return fmt.Sprintf("https://%s/api/v1/images/%s", hostname, name)
|
return fmt.Sprintf("https://%s/api/v1/images/%s", hostname, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ImageRemoteURL creates imageURL to bucket file
|
||||||
|
func ImageRemoteURL(bucketname string, path string) string {
|
||||||
|
return fmt.Sprintf("https://storage.googleapis.com/%s/%s", bucketname, path)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user