-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_file.go
60 lines (49 loc) · 1.39 KB
/
upload_file.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
package gcs
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"github.com/google/martian/v3/log"
)
func (a *StorageConnection) UploadFile(r *http.Request, domain string) (FileUploadResponse, error) {
pid := os.Getenv("GOOGLE_PROJECT_ID")
department := r.FormValue("department")
eid := r.FormValue("eid")
category := r.FormValue("category")
documentType := r.FormValue("dtype")
err := r.ParseMultipartForm(50 << 20) // Max Size Limit is 50 MB
if err != nil {
fmt.Println("Error ", err.Error())
return FileUploadResponse{}, errors.New("size is > 50 mb")
}
f, fh, err := r.FormFile("file")
if err != nil {
fmt.Println("Error ", err.Error())
return FileUploadResponse{}, errors.New("file read error")
}
defer f.Close()
fname := eid + "_" + fh.Filename
nd := GetUpdatedDomain(domain)
filepath := fmt.Sprintf("%s/%s/%s/%s/%s", department, eid, category, documentType, fname)
wc := a.Client.Bucket(nd).UserProject(pid).Object(filepath).NewWriter(context.Background())
if _, err = io.Copy(wc, f); err != nil {
log.Errorf("file upload error: %v", err)
return FileUploadResponse{}, err
}
if err := wc.Close(); err != nil {
return FileUploadResponse{}, err
}
info := FileUploadResponse{
EID: eid,
Domain: domain,
Department: department,
DocName: fname,
DocCategory: category,
DocType: documentType,
DocPath: filepath,
}
return info, nil
}