From 88d85ff4127bd711889bce0ca55b92765bac585f Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Thu, 29 Aug 2024 09:32:19 +0200 Subject: [PATCH] file upload --- pkg/list/list.go | 61 ++++++++++++++++-------------- web/index/src/Uploader/Uploader.js | 50 +++++++++++++++++++++++- 2 files changed, 81 insertions(+), 30 deletions(-) diff --git a/pkg/list/list.go b/pkg/list/list.go index 37c90ce..9161fc5 100644 --- a/pkg/list/list.go +++ b/pkg/list/list.go @@ -10,8 +10,7 @@ import ( "fmt" "io" "net/http" - "os" - "path/filepath" + "strings" "cloud.google.com/go/storage" "go.uber.org/zap" @@ -61,38 +60,42 @@ func (s *ListService) UploadMatch(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Credentials", "true") } - n := r.Form.Get("name") - // Retrieve the file from form data - f, h, err := r.FormFile("fileupload") - if err != nil { - log.L().Error("something wrong", zap.Error(err)) + if r.Method != "POST" { + log.L().Info("unexpected upload request method", zap.Any("request", *r)) + return } - defer f.Close() - path := filepath.Join(".", "files") - _ = os.MkdirAll(path, os.ModePerm) - fullPath := path + "/" + n - file, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE, os.ModePerm) + + // r.ParseMultipartForm(100 << 20) + file, handler, err := r.FormFile("demoFile") if err != nil { - log.L().Error("something else wrong", zap.Error(err)) + // fmt.Println("Error Retrieving the File") + fmt.Println(err) + return } defer file.Close() - // Copy the file to the destination path - _, err = io.Copy(file, f) - if err != nil { - log.L().Error("something else even more wrongwrong", zap.Error(err)) + if !strings.HasSuffix(handler.Filename, ".dem.gz") { + http.Error(w, fmt.Sprintf("unexpected file type [%s]", handler.Filename), http.StatusBadRequest) + return + } + log.L().Info("uploading file", zap.String("filename", handler.Filename), zap.Int64("filesize", handler.Size), zap.Any("mime", handler.Header)) + objHandle := s.gcpBucket.Object(handler.Filename) + log.L().Info("1") + objWriter := objHandle.NewWriter(r.Context()) + log.L().Info("2") + // Copy the uploaded file to the created file on the filesystem + if _, err := io.Copy(objWriter, file); err != nil { + + log.L().Info("3") + http.Error(w, err.Error(), http.StatusInternalServerError) + return } - log.L().Info("finally uploadd", zap.String("file", n+filepath.Ext(h.Filename))) - - // obj := s.gcpBucket.Object("test") - // writer := obj.NewWriter(r.Context()) - // if _, writeErr := fmt.Fprintf(writer, "This is test upload file 2"); writeErr != nil { - // http.Error(w, writeErr.Error(), http.StatusInternalServerError) - // log.L().Error("failed to write to gcp file", zap.Error(writeErr)) - // } - // if err := writer.Close(); err != nil { - // http.Error(w, err.Error(), http.StatusInternalServerError) - // log.L().Error("failed to close gcp filewriter", zap.Error(err)) - // } + if err := objWriter.Close(); err != nil { + log.L().Error("fail to close gcp object writer", zap.Error(err)) + } + + log.L().Info("4") + + fmt.Fprintf(w, "Successfully Uploaded File\n") } func (s *ListService) gcpStorage(ctx context.Context) ([]match.MatchInfo, error) { diff --git a/web/index/src/Uploader/Uploader.js b/web/index/src/Uploader/Uploader.js index 3a187d6..bf1ecd0 100644 --- a/web/index/src/Uploader/Uploader.js +++ b/web/index/src/Uploader/Uploader.js @@ -7,9 +7,57 @@ const Uploader = (props) => { const onUpload = function (event) { console.log("file upload", event) } + + const uploadHandler = function ({ files }) { + const [file] = files; + let formData = new FormData(); + formData.append('demoFile', file); + + fetch(serverHost + "/match/upload", + { + method: 'POST', + body: formData + }, + ).catch(err => { + console.log("failed to upload") + }) + + // const fileReader = new FileReader(); + // fileReader.onload = (e) => { + // uploadDemo(e.target.result); + // }; + // fileReader.readAsDataURL(file); + } + + const uploadDemo = async (demoFile) => { + let formData = new FormData(); + formData.append('demoFile', demoFile); + + const response = await fetch(serverHost + "/match/upload", + { + method: 'POST', + body: formData + }, + ); + console.log(response) + }; + const onProgress = (event) => { + console.log("progress", event) + } + return (
- +
) }