@@ -10,8 +10,7 @@ import (
10
10
"fmt"
11
11
"io"
12
12
"net/http"
13
- "os"
14
- "path/filepath"
13
+ "strings"
15
14
16
15
"cloud.google.com/go/storage"
17
16
"go.uber.org/zap"
@@ -61,38 +60,42 @@ func (s *ListService) UploadMatch(w http.ResponseWriter, r *http.Request) {
61
60
w .Header ().Set ("Access-Control-Allow-Credentials" , "true" )
62
61
}
63
62
64
- n := r .Form .Get ("name" )
65
- // Retrieve the file from form data
66
- f , h , err := r .FormFile ("fileupload" )
67
- if err != nil {
68
- log .L ().Error ("something wrong" , zap .Error (err ))
63
+ if r .Method != "POST" {
64
+ log .L ().Info ("unexpected upload request method" , zap .Any ("request" , * r ))
65
+ return
69
66
}
70
- defer f .Close ()
71
- path := filepath .Join ("." , "files" )
72
- _ = os .MkdirAll (path , os .ModePerm )
73
- fullPath := path + "/" + n
74
- file , err := os .OpenFile (fullPath , os .O_WRONLY | os .O_CREATE , os .ModePerm )
67
+
68
+ // r.ParseMultipartForm(100 << 20)
69
+ file , handler , err := r .FormFile ("demoFile" )
75
70
if err != nil {
76
- log .L ().Error ("something else wrong" , zap .Error (err ))
71
+ // fmt.Println("Error Retrieving the File")
72
+ fmt .Println (err )
73
+ return
77
74
}
78
75
defer file .Close ()
79
- // Copy the file to the destination path
80
- _ , err = io .Copy (file , f )
81
- if err != nil {
82
- log .L ().Error ("something else even more wrongwrong" , zap .Error (err ))
76
+ if ! strings .HasSuffix (handler .Filename , ".dem.gz" ) {
77
+ http .Error (w , fmt .Sprintf ("unexpected file type [%s]" , handler .Filename ), http .StatusBadRequest )
78
+ return
79
+ }
80
+ log .L ().Info ("uploading file" , zap .String ("filename" , handler .Filename ), zap .Int64 ("filesize" , handler .Size ), zap .Any ("mime" , handler .Header ))
81
+ objHandle := s .gcpBucket .Object (handler .Filename )
82
+ log .L ().Info ("1" )
83
+ objWriter := objHandle .NewWriter (r .Context ())
84
+ log .L ().Info ("2" )
85
+ // Copy the uploaded file to the created file on the filesystem
86
+ if _ , err := io .Copy (objWriter , file ); err != nil {
87
+
88
+ log .L ().Info ("3" )
89
+ http .Error (w , err .Error (), http .StatusInternalServerError )
90
+ return
83
91
}
84
- log .L ().Info ("finally uploadd" , zap .String ("file" , n + filepath .Ext (h .Filename )))
85
-
86
- // obj := s.gcpBucket.Object("test")
87
- // writer := obj.NewWriter(r.Context())
88
- // if _, writeErr := fmt.Fprintf(writer, "This is test upload file 2"); writeErr != nil {
89
- // http.Error(w, writeErr.Error(), http.StatusInternalServerError)
90
- // log.L().Error("failed to write to gcp file", zap.Error(writeErr))
91
- // }
92
- // if err := writer.Close(); err != nil {
93
- // http.Error(w, err.Error(), http.StatusInternalServerError)
94
- // log.L().Error("failed to close gcp filewriter", zap.Error(err))
95
- // }
92
+ if err := objWriter .Close (); err != nil {
93
+ log .L ().Error ("fail to close gcp object writer" , zap .Error (err ))
94
+ }
95
+
96
+ log .L ().Info ("4" )
97
+
98
+ fmt .Fprintf (w , "Successfully Uploaded File\n " )
96
99
}
97
100
98
101
func (s * ListService ) gcpStorage (ctx context.Context ) ([]match.MatchInfo , error ) {
0 commit comments