-
Notifications
You must be signed in to change notification settings - Fork 0
/
trb_request_document.go
93 lines (78 loc) · 3.31 KB
/
trb_request_document.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
package resolvers
import (
"context"
"fmt"
"path/filepath"
"github.com/google/uuid"
"github.com/cmsgov/easi-app/pkg/appcontext"
"github.com/cmsgov/easi-app/pkg/easiencoding"
"github.com/cmsgov/easi-app/pkg/graph/model"
"github.com/cmsgov/easi-app/pkg/models"
"github.com/cmsgov/easi-app/pkg/storage"
"github.com/cmsgov/easi-app/pkg/upload"
)
// GetTRBRequestDocumentsByRequestID fetches all documents attached to the TRB request with the given ID.
func GetTRBRequestDocumentsByRequestID(ctx context.Context, store *storage.Store, s3Client *upload.S3Client, id uuid.UUID) ([]*models.TRBRequestDocument, error) {
return store.GetTRBRequestDocumentsByRequestID(ctx, id)
}
// GetURLForTRBRequestDocument queries S3 for a presigned URL that can be used to fetch the document with the given s3Key
func GetURLForTRBRequestDocument(s3Client *upload.S3Client, s3Key string) (string, error) {
presignedURL, err := s3Client.NewGetPresignedURL(s3Key)
if err != nil {
return "", err
}
return presignedURL.URL, nil
}
// GetStatusForTRBRequestDocument queries S3 for the virus-scanning status of a document with the given s3Key
func GetStatusForTRBRequestDocument(s3Client *upload.S3Client, s3Key string) (models.TRBRequestDocumentStatus, error) {
avStatus, err := s3Client.TagValueForKey(s3Key, upload.AVStatusTagName)
if err != nil {
return "", err
}
// possible tag values come from virus scanning lambda
if avStatus == "CLEAN" {
return models.TRBRequestDocumentStatusAvailable, nil
} else if avStatus == "INFECTED" {
return models.TRBRequestDocumentStatusUnavailable, nil
} else {
return models.TRBRequestDocumentStatusPending, nil
}
}
// CreateTRBRequestDocument uploads a document to S3, then saves its metadata to our database.
func CreateTRBRequestDocument(ctx context.Context, store *storage.Store, s3Client *upload.S3Client, input model.CreateTRBRequestDocumentInput) (*models.TRBRequestDocument, error) {
s3Key := uuid.New().String()
existingExtension := filepath.Ext(input.FileData.Filename)
if existingExtension != "" {
s3Key += existingExtension
} else {
s3Key += fallbackExtension
}
decodedReadSeeker, err := easiencoding.DecodeBase64File(&input.FileData.File)
if err != nil {
return nil, fmt.Errorf("...%w...FileName: %s", err, input.FileData.Filename) //Wrap error and provide filename
}
err = s3Client.UploadFile(s3Key, decodedReadSeeker)
if err != nil {
return nil, err
}
documentDatabaseRecord := models.TRBRequestDocument{
TRBRequestID: input.RequestID,
CommonDocumentType: input.DocumentType,
FileName: input.FileData.Filename,
S3Key: s3Key,
Bucket: s3Client.GetBucket(),
// Status isn't saved in database - will be fetched from S3
// URL isn't saved in database - will be generated by querying S3
}
documentDatabaseRecord.CreatedBy = appcontext.Principal(ctx).ID()
if input.OtherTypeDescription != nil {
documentDatabaseRecord.OtherType = *input.OtherTypeDescription
}
return store.CreateTRBRequestDocument(ctx, &documentDatabaseRecord)
}
// DeleteTRBRequestDocument deletes an existing TRBRequestDocument, given its ID.
//
// Does *not* delete the uploaded file from S3.
func DeleteTRBRequestDocument(ctx context.Context, store *storage.Store, id uuid.UUID) (*models.TRBRequestDocument, error) {
return store.DeleteTRBRequestDocument(ctx, id)
}