-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathuploads.go
79 lines (60 loc) · 1.94 KB
/
uploads.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
package dao
import (
"context"
"github.com/content-services/content-sources-backend/pkg/clients/pulp_client"
"github.com/content-services/content-sources-backend/pkg/models"
"gorm.io/gorm"
)
type uploadDaoImpl struct {
db *gorm.DB
pulpClient pulp_client.PulpClient
}
func GetUploadDao(db *gorm.DB, pulpClient pulp_client.PulpClient) UploadDao {
return &uploadDaoImpl{
db: db,
pulpClient: pulpClient,
}
}
func (t uploadDaoImpl) StoreFileUpload(ctx context.Context, orgID string, uploadUUID string, sha256 string, chunkSize int64) error {
var upload models.Upload
upload.OrgID = orgID
upload.UploadUUID = uploadUUID
upload.Sha256 = sha256
upload.ChunkSize = chunkSize
upload.ChunkList = []string{}
db := t.db.Model(models.Upload{}).WithContext(ctx).Create(&upload)
if db.Error != nil {
return db.Error
}
return nil
}
func (t uploadDaoImpl) GetExistingUploadIDAndCompletedChunks(ctx context.Context, orgID string, sha256 string, chunkSize int64) (string, []string, error) {
db := t.db.Model(models.Upload{}).WithContext(ctx)
var result models.Upload
db.Where("org_id = ?", orgID).Where("chunk_size = ?", chunkSize).Where("sha256 = ?", sha256).First(&result)
if db.Error != nil {
return "", []string{}, db.Error
}
return result.UploadUUID, result.ChunkList, nil
}
func (t uploadDaoImpl) StoreChunkUpload(ctx context.Context, orgID string, uploadUUID string, sha256 string) error {
db := t.db.Model(models.Upload{}).
WithContext(ctx).
Where("org_id = ?", orgID).
Where("upload_uuid = ?", uploadUUID).
Where("? != all(chunk_list)", sha256).
Update("chunk_list", gorm.Expr(`array_append(chunk_list, ?)`, sha256))
if db.Error != nil {
return db.Error
}
return nil
}
func (t uploadDaoImpl) DeleteUpload(ctx context.Context, uploadUUID string) error {
err := t.db.WithContext(ctx).
Where("upload_uuid = ?", UuidifyString(uploadUUID)).
Delete(models.Upload{}).Error
if err != nil {
return err
}
return nil
}