-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (104 loc) · 2.74 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"cloud.google.com/go/storage"
"github.com/google/uuid"
"github.com/gorilla/mux"
)
func enableCors(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
}
func uploadFile(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
pictureURLChannel := make(chan string)
go func() {
// create gcloud client
ctx := context.Background()
client, err := storage.NewClient(ctx)
bucketName := "api-da-test-bucket"
if err != nil {
fmt.Println(err)
}
// parse file from post requests.
r.ParseMultipartForm(10 << 20)
file, handler, err := r.FormFile("fileUpload")
if err != nil {
fmt.Println(err)
}
defer file.Close()
// fileBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println(err)
}
// name uuid generation
fileExtension := strings.Split(handler.Filename, ".")[1]
uniqueID, err := uuid.NewRandom()
if err != nil {
fmt.Println(err)
}
uniqueIDString := uniqueID.String()
// Writer Object Change
objectattrs := storage.ObjectAttrs{
ContentType: handler.Header.Get("Content-Type"),
Name: uniqueIDString + "." + fileExtension,
}
// handler.Filename
wc := client.Bucket(bucketName).Object(uniqueIDString + "." + fileExtension).NewWriter(ctx)
wc.ObjectAttrs = objectattrs
if _, err = io.Copy(wc, file); err != nil {
fmt.Println(err)
}
if err := wc.Close(); err != nil {
fmt.Println(err)
}
// JSON FOR URL
jsonFile, err := os.Open("./auth.json")
defer jsonFile.Close()
jsonByteValue, _ := ioutil.ReadAll(jsonFile)
var result map[string]interface{}
json.Unmarshal([]byte(jsonByteValue), &result)
if err != nil {
fmt.Println(err)
}
pictureURLChannel <- "https://storage.googleapis.com/" + bucketName + "/" + uniqueIDString + "." + fileExtension
}()
fmt.Fprintf(w, <-pictureURLChannel)
}
func deleteFile(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
fileName := r.URL.Query()["name"][0]
completionChannel := make(chan string)
go func() {
ctx := context.Background()
client, err := storage.NewClient(ctx)
bucketName := "api-da-test-bucket"
if err != nil {
fmt.Println(err)
}
o := client.Bucket(bucketName).Object(fileName)
if err := o.Delete(ctx); err != nil {
fmt.Println(err)
}
completionChannel <- fileName + " deleted!"
}()
fmt.Fprintf(w, <-completionChannel)
}
func setupRoutes() {
r := mux.NewRouter()
r.HandleFunc("/upload", uploadFile).Methods("POST")
r.HandleFunc("/delete", deleteFile).Methods("DELETE")
http.Handle("/", r)
http.ListenAndServe(":8080", nil)
}
func main() {
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", "./auth.json")
fmt.Println("Server running on PORT:8080")
setupRoutes()
}