/
fileTable.go
191 lines (157 loc) · 5.49 KB
/
fileTable.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package dbTable
import (
"fmt"
"github.com/google/uuid"
"github.com/lib/pq"
"github.com/pennsieve/pennsieve-go-api/pkg/core"
"github.com/pennsieve/pennsieve-go-api/pkg/models/fileInfo/fileType"
"github.com/pennsieve/pennsieve-go-api/pkg/models/fileInfo/objectType"
"github.com/pennsieve/pennsieve-go-api/pkg/models/fileInfo/processingState"
"github.com/pennsieve/pennsieve-go-api/pkg/models/fileInfo/uploadState"
"log"
"strings"
"time"
)
type File struct {
Id string `json:"id"`
PackageId int `json:"package_id"`
Name string `json:"name"`
FileType fileType.Type `json:"file_type"`
S3Bucket string `json:"s3_bucket"`
S3Key string `json:"s3_key"`
ObjectType objectType.ObjectType `json:"object_type"`
Size int64 `json:"size"`
CheckSum string `json:"checksum"`
UUID uuid.UUID `json:"uuid"`
ProcessingState processingState.ProcessingState `json:"processing_state"`
UploadedState uploadState.UploadedState `json:"uploaded_state"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type FileParams struct {
PackageId int `json:"package_id"`
Name string `json:"name"`
FileType fileType.Type `json:"file_type"`
S3Bucket string `json:"s3_bucket"`
S3Key string `json:"s3_key"`
ObjectType objectType.ObjectType `json:"object_type"`
Size int64 `json:"size"`
CheckSum string `json:"checksum"`
Sha256 string `json:"sha256"`
UUID uuid.UUID `json:"uuid"`
}
type ErrFileNotFound struct{}
func (m *ErrFileNotFound) Error() string {
return "File does not exist in postgres"
}
type ErrMultipleRowsAffected struct{}
func (m *ErrMultipleRowsAffected) Error() string {
return "Multiple files in files table were updated"
}
func (p *File) Add(db core.PostgresAPI, files []FileParams) ([]File, error) {
currentTime := time.Now()
var vals []interface{}
var inserts []string
for index, row := range files {
inserts = append(inserts, fmt.Sprintf("($%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d)",
index*13+1,
index*13+2,
index*13+3,
index*13+4,
index*13+5,
index*13+6,
index*13+7,
index*13+8,
index*13+9,
index*13+10,
index*13+11,
index*13+12,
index*13+13,
))
etag := fmt.Sprintf("{\"checksum\": \"%s\", \"chunkSize\": \"%s\", \"sha256\": \"%s\"}", row.CheckSum, "32", row.Sha256)
vals = append(vals, row.PackageId, row.Name, row.FileType.String(), row.S3Bucket, row.S3Key,
row.ObjectType.String(), row.Size, etag, row.UUID.String(), processingState.Unprocessed.String(),
uploadState.Uploaded.String(), currentTime, currentTime)
}
sqlInsert := "INSERT INTO files(package_id, name, file_type, s3_bucket, s3_key, " +
"object_type, size, checksum, uuid, processing_state, uploaded_state, created_at, updated_at) VALUES "
returnRows := "id, package_id, name, file_type, s3_bucket, s3_key, " +
"object_type, size, checksum, uuid, processing_state, uploaded_state, created_at, updated_at"
sqlInsert = sqlInsert + strings.Join(inserts, ",") + fmt.Sprintf("RETURNING %s;", returnRows)
//prepare the statement
stmt, err := db.Prepare(sqlInsert)
if err != nil {
log.Fatalln("ERROR: ", err)
}
defer stmt.Close()
// format all vals at once
var allInsertedFiles []File
rows, err := stmt.Query(vals...)
if err != nil {
if pqErr, ok := err.(*pq.Error); ok {
log.Println(pqErr)
}
return nil, err
}
for rows.Next() {
var currentRecord File
var fType string
var oType string
var pState string
var uState string
err = rows.Scan(
¤tRecord.Id,
¤tRecord.PackageId,
¤tRecord.Name,
&fType,
¤tRecord.S3Bucket,
¤tRecord.S3Key,
&oType,
¤tRecord.Size,
¤tRecord.CheckSum,
¤tRecord.UUID,
&pState,
&uState,
¤tRecord.CreatedAt,
¤tRecord.UpdatedAt,
)
currentRecord.FileType = fileType.Dict[fType]
currentRecord.ObjectType = objectType.Dict[oType]
currentRecord.ProcessingState = processingState.Dict[pState]
currentRecord.UploadedState = uploadState.Dict[uState]
if err != nil {
log.Println("ERROR: ", err)
}
allInsertedFiles = append(allInsertedFiles, currentRecord)
}
if err != nil {
log.Println(err)
}
return allInsertedFiles, err
}
// UpdateBucket updates the storage bucket as part of upload process and sets Status
func (p *File) UpdateBucket(db core.PostgresAPI, uploadId string, bucket string, s3Key string, organizationId int64) error {
queryStr := fmt.Sprintf("UPDATE \"%d\".files SET s3_bucket=$1, s3_key=$2 WHERE UUID=$3;", organizationId)
result, err := db.Exec(queryStr, bucket, s3Key, uploadId)
msg := ""
if err != nil {
msg = fmt.Sprintf("Error updating the bucket location: %v", err)
log.Println(msg)
return err
}
affectedRows, err := result.RowsAffected()
if err != nil {
return err
}
if affectedRows != 1 {
if affectedRows == 0 {
nofFoundError := &ErrFileNotFound{}
log.Println(nofFoundError.Error())
return nofFoundError
}
multipleRowError := &ErrMultipleRowsAffected{}
log.Println(multipleRowError.Error())
return multipleRowError
}
return nil
}