Skip to content

Commit

Permalink
file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkoo committed Aug 29, 2024
1 parent d1523f5 commit 88d85ff
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 30 deletions.
61 changes: 32 additions & 29 deletions pkg/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"

"cloud.google.com/go/storage"
"go.uber.org/zap"
Expand Down Expand Up @@ -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) {
Expand Down
50 changes: 49 additions & 1 deletion web/index/src/Uploader/Uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div>
<FileUpload mode="basic" name="demo[]" url={serverHost + "/match/upload"} accept="application/*" maxFileSize={10000000} onUpload={onUpload} auto />
<FileUpload
mode="basic"
name="demoFile"
url={serverHost + "/match/upload"}
accept="application/*"
maxFileSize={200_000_000}
onUpload={onUpload}
onProgress={onProgress}
// customUpload={true}
// uploadHandler={uploadHandler}
auto />
</div>
)
}
Expand Down

0 comments on commit 88d85ff

Please sign in to comment.