-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_ticket_data.go
66 lines (54 loc) · 1.61 KB
/
upload_ticket_data.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
61
62
63
64
65
66
package gcs
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/google/martian/v3/log"
)
func (a *StorageConnection) UploadTicketData(r *http.Request, domain string) (TicketFileUploadResponse, error) {
pid := os.Getenv("GOOGLE_PROJECT_ID")
department := "Ticket"
ticket_id := r.FormValue("ticket_id")
documentType := r.FormValue("dtype")
var ticketPathArr []string
var fNameArr [] string
err := r.ParseMultipartForm(50 << 20) // Max Size Limit is 50 MB
if err != nil {
fmt.Println("Error ", err.Error())
return TicketFileUploadResponse{}, errors.New("size is > 50 mb")
}
for _, fh := range r.MultipartForm.File["ticket_images"] {
f, err := fh.Open()
if err != nil {
fmt.Println("Error ", err.Error())
return TicketFileUploadResponse{}, errors.New("file read error")
}
defer f.Close()
fname := ticket_id + "_" + fh.Filename
fNameArr = append(fNameArr, fname)
nd := strings.Replace(domain, ".", "_", -1)
filepath := fmt.Sprintf("%s/%s/%s/%s", department, ticket_id, documentType, fname)
ticketPathArr = append(ticketPathArr, filepath)
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 TicketFileUploadResponse{}, err
}
if err := wc.Close(); err != nil {
return TicketFileUploadResponse{}, err
}
}
info := TicketFileUploadResponse{
Domain: domain,
TicketId: ticket_id,
Department: department,
DocName: fNameArr,
DocType: documentType,
TicketPathArr: ticketPathArr,
}
return info, nil
}