Skip to content

Commit

Permalink
updating to run without goroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
awmpietro committed Oct 19, 2023
1 parent f3325e8 commit 4dd68a3
Showing 1 changed file with 33 additions and 35 deletions.
68 changes: 33 additions & 35 deletions gcpfileupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,45 @@ func NewFileUploader(r *http.Request, formFile string, fileName string, bucketNa
// Upload a file to a bucket in Google Cloud Storage
// Make sure you have defined the env var GOOGLE_STORAGE_BUCKET_NAME in your env file
// The env should point to the path of your google user account keys
func (fu *FileUploader) Upload(errCh chan error) {
go func(errCh chan<- error) {
file, header, err := fu.Request.FormFile(fu.FormFile)
if err != nil {
errCh <- err
return
func (fu *FileUploader) Upload() (bool, error) {
file, header, err := fu.Request.FormFile(fu.FormFile)
if err != nil {
if err == http.ErrMissingFile {
return false, nil
}
defer file.Close()
if file != nil {
return false, err
}
defer file.Close()
if file != nil {

if fu.FileName == "" {
fu.FileName = header.Filename
} else {
ext := filepath.Ext(header.Filename)
fu.FileName = fu.FileName + ext
}
if fu.FileName == "" {
fu.FileName = header.Filename
} else {
ext := filepath.Ext(header.Filename)
fu.FileName = fu.FileName + ext
}

ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Second*50)
defer cancel()
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Second*50)
defer cancel()

client, err := storage.NewClient(ctx)
if err != nil {
client, err := storage.NewClient(ctx)
if err != nil {

errCh <- err
return
}
bucket := client.Bucket(fu.BucketName)
obj := bucket.Object(fu.FileName)
wc := obj.NewWriter(ctx)
if _, err := io.Copy(wc, file); err != nil {
return false, err
}
bucket := client.Bucket(fu.BucketName)
obj := bucket.Object(fu.FileName)
wc := obj.NewWriter(ctx)
if _, err := io.Copy(wc, file); err != nil {

errCh <- err
return
}
if err := wc.Close(); err != nil {
errCh <- err
return
}
close(errCh)
return false, err
}
if err := wc.Close(); err != nil {
return false, err
}
}(errCh)
return true, nil
}
return false, nil

}

0 comments on commit 4dd68a3

Please sign in to comment.