This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bd03a1
commit 626ad5a
Showing
10 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package storage | ||
|
||
import ( | ||
"github.com/google/uuid" | ||
"github.com/riotkit-org/backup-repository/collections" | ||
"gorm.io/gorm" | ||
"time" | ||
) | ||
|
||
type UploadedVersion struct { | ||
Id string `json:"id" structs:"id" sql:"type:string;primary_key;default:uuid_generate_v4()` | ||
CollectionId string `json:"collectionId"` | ||
VersionNumber int `json:"versionNumber"` | ||
Filename string `json:"filename"` // full filename e.g. iwa-ait-v1-db.tar.gz | ||
Filesize int `json:"filesize"` // in bytes | ||
|
||
// auditing | ||
UploadedBySessionId string `json:"uploadedBySessionId"` | ||
Uploader string `json:"user" structs:"user"` | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
DeletedAt gorm.DeletedAt `gorm:"index"` | ||
} | ||
|
||
func (u *UploadedVersion) GetTargetPath() string { | ||
return u.CollectionId + "/" + u.Filename | ||
} | ||
|
||
func CreateNewVersionFromCollection(c collections.Collection, svc Service, uploader string, uploaderSessionId string, filesize int) UploadedVersion { | ||
nextVersion := svc.FindNextVersionForCollectionId(c.Metadata.Name) | ||
|
||
return UploadedVersion{ | ||
Id: uuid.New().String(), | ||
CollectionId: c.Metadata.Name, | ||
VersionNumber: nextVersion, | ||
Filename: c.GenerateNextVersionFilename(nextVersion), | ||
Filesize: filesize, | ||
UploadedBySessionId: uploaderSessionId, | ||
Uploader: uploader, | ||
CreatedAt: time.Time{}, | ||
UpdatedAt: time.Time{}, | ||
DeletedAt: gorm.DeletedAt{}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package storage | ||
|
||
type VersionsRepository struct { | ||
} | ||
|
||
func (r VersionsRepository) findLastHighestVersionNumber(name string) interface{} { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package storage | ||
|
||
import ( | ||
"context" | ||
"github.com/sirupsen/logrus" | ||
"gocloud.dev/blob" | ||
) | ||
|
||
type Service struct { | ||
storage *blob.Bucket | ||
repository *VersionsRepository | ||
} | ||
|
||
func (s *Service) FindNextVersionForCollectionId(name string) int { | ||
lastHigherVersion := s.repository.findLastHighestVersionNumber(name) | ||
return lastHigherVersion + 1 | ||
} | ||
|
||
func NewService(driverUrl string) (Service, error) { | ||
driver, err := blob.OpenBucket(context.Background(), driverUrl) | ||
if err != nil { | ||
logrus.Errorf("Cannot construct storage driver: %v", err) | ||
return Service{}, err | ||
} | ||
|
||
return Service{storage: driver}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package storage | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"gocloud.dev/blob" | ||
"io" | ||
) | ||
|
||
func (s *Service) createGPGValidator() func([]byte) error { | ||
startFound := false | ||
endingFound := false | ||
|
||
return func(buff []byte) error { | ||
if bytes.Contains(buff, []byte("-----BEGIN PGP MESSAGE")) { | ||
startFound = true | ||
} | ||
if bytes.Contains(buff, []byte("-----END PGP MESSAGE")) { | ||
endingFound = true | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func (s *Service) UploadFile(inputStream io.ReadCloser, version UploadedVersion) (bool, error) { | ||
writeStream, err := s.storage.NewWriter(context.Background(), version.GetTargetPath(), &blob.WriterOptions{}) | ||
if err != nil { | ||
return false, errors.New(fmt.Sprintf("cannot upload file, attempted to open a writable stream, error: %v", err)) | ||
} | ||
|
||
if writeErr := s.CopyStream(inputStream, writeStream, 1024, s.createGPGValidator()); writeErr != nil { | ||
return false, errors.New(fmt.Sprintf("cannot upload file, cannot copy stream, error: %v", writeErr)) | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// CopyStream copies a readable stream to writable stream, while providing a possibility to use a validation callback on-the-fly | ||
func (s *Service) CopyStream(inputStream io.ReadCloser, writeStream io.WriteCloser, bufferLen int, processor func([]byte) error) error { | ||
p := make([]byte, bufferLen) | ||
|
||
for { | ||
n, err := inputStream.Read(p) | ||
|
||
if err != nil { | ||
if err == io.EOF { | ||
// validation callback | ||
if processingError := processor(p[:n]); processingError != nil { | ||
return processingError | ||
} | ||
// write to second stream | ||
_, writeErr := writeStream.Write(p[:n]) | ||
if writeErr != nil { | ||
return writeErr | ||
} | ||
} | ||
|
||
break | ||
} | ||
// validation callback | ||
if processingError := processor(p[:n]); processingError != nil { | ||
return processingError | ||
} | ||
// write to second stream | ||
_, writeErr := writeStream.Write(p[:n]) | ||
if writeErr != nil { | ||
return writeErr | ||
} | ||
} | ||
|
||
return nil | ||
} |