Merged in feature/preserve-folder-paths (pull request #179)
support folder paths * support folder paths * s3 storage docs
This commit is contained in:
@@ -15,9 +15,10 @@ import (
|
||||
)
|
||||
|
||||
type Create struct {
|
||||
Bucket string
|
||||
Key objectstore.BucketKey
|
||||
Hash string
|
||||
Bucket string
|
||||
Key objectstore.BucketKey
|
||||
Hash string
|
||||
Filename *string // Optional filename for batch processing
|
||||
}
|
||||
|
||||
func (s *Service) Create(ctx context.Context, doc *Create) (uuid.UUID, error) {
|
||||
@@ -47,10 +48,11 @@ func (s *Service) Create(ctx context.Context, doc *Create) (uuid.UUID, error) {
|
||||
}
|
||||
|
||||
type createDocumentParams struct {
|
||||
ID *uuid.UUID
|
||||
Hash string
|
||||
Bucket string
|
||||
Key objectstore.BucketKey
|
||||
ID *uuid.UUID
|
||||
Hash string
|
||||
Bucket string
|
||||
Key objectstore.BucketKey
|
||||
Filename *string
|
||||
}
|
||||
|
||||
func (s *Service) getCreateParams(ctx context.Context, doc *Create) (*createDocumentParams, error) {
|
||||
@@ -66,10 +68,11 @@ func (s *Service) getCreateParams(ctx context.Context, doc *Create) (*createDocu
|
||||
}
|
||||
|
||||
return &createDocumentParams{
|
||||
ID: docID,
|
||||
Hash: doc.Hash,
|
||||
Key: doc.Key,
|
||||
Bucket: doc.Bucket,
|
||||
ID: docID,
|
||||
Hash: doc.Hash,
|
||||
Key: doc.Key,
|
||||
Bucket: doc.Bucket,
|
||||
Filename: doc.Filename,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -83,6 +86,7 @@ func (s *Service) submitCreate(ctx context.Context, params *createDocumentParams
|
||||
Clientid: params.Key.ClientID,
|
||||
Hash: params.Hash,
|
||||
BatchID: params.Key.BatchID,
|
||||
Filename: params.Filename,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -52,7 +52,7 @@ func TestCreate(t *testing.T) {
|
||||
pgxmock.NewRows([]string{"id"}),
|
||||
)
|
||||
pool.ExpectBegin()
|
||||
pool.ExpectQuery("name: CreateDocument :one").WithArgs(doc.ClientID, doc.Hash, (*uuid.UUID)(nil)).
|
||||
pool.ExpectQuery("name: CreateDocument :one").WithArgs(doc.ClientID, doc.Hash, (*uuid.UUID)(nil), (*string)(nil)).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}).
|
||||
AddRow(doc.ID),
|
||||
@@ -80,14 +80,14 @@ func TestCreate(t *testing.T) {
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", doc.ID.String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
id, err := svc.Create(ctx, &Create{
|
||||
Hash: doc.Hash,
|
||||
Key: key,
|
||||
Bucket: bucket,
|
||||
Hash: doc.Hash,
|
||||
Key: key,
|
||||
Bucket: bucket,
|
||||
Filename: nil, // No filename for this test
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, doc.ID, id)
|
||||
@@ -235,7 +235,7 @@ func TestSubmitCreate(t *testing.T) {
|
||||
}
|
||||
|
||||
pool.ExpectBegin()
|
||||
pool.ExpectQuery("name: CreateDocument :one").WithArgs(doc.ClientID, doc.Hash, (*uuid.UUID)(nil)).
|
||||
pool.ExpectQuery("name: CreateDocument :one").WithArgs(doc.ClientID, doc.Hash, (*uuid.UUID)(nil), (*string)(nil)).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}).
|
||||
AddRow(doc.ID),
|
||||
@@ -245,9 +245,10 @@ func TestSubmitCreate(t *testing.T) {
|
||||
pool.ExpectCommit()
|
||||
|
||||
id, err := svc.submitCreate(ctx, &createDocumentParams{
|
||||
Hash: doc.Hash,
|
||||
Bucket: bucket,
|
||||
Key: key,
|
||||
Hash: doc.Hash,
|
||||
Bucket: bucket,
|
||||
Key: key,
|
||||
Filename: nil, // No filename for this test
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, doc.ID, id)
|
||||
|
||||
@@ -34,7 +34,14 @@ func detectContentType(filename string) string {
|
||||
}
|
||||
|
||||
func (s *Service) Upload(ctx context.Context, file File) error {
|
||||
return s.cfg.ExecuteDBTransaction(ctx, func(ctx context.Context, q *repository.Queries) error {
|
||||
_, err := s.UploadAndReturnKey(ctx, file)
|
||||
return err
|
||||
}
|
||||
|
||||
// UploadAndReturnKey uploads a file and returns the BucketKey used
|
||||
func (s *Service) UploadAndReturnKey(ctx context.Context, file File) (objectstore.BucketKey, error) {
|
||||
var key objectstore.BucketKey
|
||||
err := s.cfg.ExecuteDBTransaction(ctx, func(ctx context.Context, q *repository.Queries) error {
|
||||
now := time.Now().UTC()
|
||||
part, err := s.cfg.GetDBQueries().GetDocumentUploadCurrentPart(ctx, &repository.GetDocumentUploadCurrentPartParams{
|
||||
Clientid: &file.ClientID,
|
||||
@@ -49,7 +56,7 @@ func (s *Service) Upload(ctx context.Context, file File) error {
|
||||
|
||||
uploadId := uuid.New()
|
||||
newPart := s.cfg.GetDirectoryPart(part.Part, part.Count)
|
||||
key := objectstore.BucketKey{
|
||||
key = objectstore.BucketKey{
|
||||
ClientID: file.ClientID,
|
||||
Location: objectstore.Import,
|
||||
CreatedAt: now,
|
||||
@@ -70,6 +77,8 @@ func (s *Service) Upload(ctx context.Context, file File) error {
|
||||
Valid: true,
|
||||
Time: now,
|
||||
},
|
||||
Filename: &file.Filename,
|
||||
BatchID: file.BatchID,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -78,11 +87,18 @@ func (s *Service) Upload(ctx context.Context, file File) error {
|
||||
// Detect content type from original filename
|
||||
contentType := detectContentType(file.Filename)
|
||||
|
||||
// Prepare metadata for S3 object
|
||||
metadata := make(map[string]string)
|
||||
if file.Filename != "" {
|
||||
metadata["original-filename"] = file.Filename
|
||||
}
|
||||
|
||||
_, err = s.cfg.GetStoreClient().PutObject(ctx, &s3.PutObjectInput{
|
||||
Bucket: &bucket,
|
||||
Key: &keyStr,
|
||||
Body: file.Content,
|
||||
ContentType: &contentType,
|
||||
Metadata: metadata,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -90,4 +106,6 @@ func (s *Service) Upload(ctx context.Context, file File) error {
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return key, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user