diff --git a/constants/constants.go b/constants/constants.go index 65e6ab7..9b23adf 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -7,8 +7,10 @@ const ( AlgSha512 = "sha512" BagItProfileBTR = "btr-v1.0.json" BagItProfileDefault = "aptrust-v2.2.json" - BagWriterTypeTar = "tar" + BagReaderTypeFileSystem = "filesystem" + BagReaderTypeTar = "tar" BagWriterTypeFileSystem = "filesystem" + BagWriterTypeTar = "tar" BTRProfileIdentifier = "https://github.com/dpscollaborative/btr_bagit_profile/releases/download/1.0/btr-bagit-profile.json" DefaultProfileIdentifier = "https://raw.githubusercontent.com/APTrust/preservation-services/master/profiles/aptrust-v2.2.json" EmptyProfileIdentifier = "https://raw.githubusercontent.com/APTrust/dart/tree/master/profiles/empty_profile.json" @@ -112,6 +114,15 @@ var BagWriterTypeFor = map[string]string{ SerialFormatTar: BagWriterTypeTar, } +// BagReaderTypeFor maps a BagIt serialization format to the +// type of reader that can write that format. +var BagReaderTypeFor = map[string]string{ + "": BagReaderTypeFileSystem, + ".tar": BagReaderTypeTar, + SerialFormatNone: BagReaderTypeFileSystem, + SerialFormatTar: BagReaderTypeTar, +} + var SerializationOptions = []string{ SerializationForbidden, SerializationOptional, diff --git a/core/bag_reader.go b/core/bag_reader.go index 50fbaa5..edfca8e 100644 --- a/core/bag_reader.go +++ b/core/bag_reader.go @@ -1,5 +1,7 @@ package core +import "github.com/APTrust/dart-runner/constants" + // BagReader describes the interface that bag readers must implement, // whether reading from a file system, tar file, zip file, etc. type BagReader interface { @@ -21,3 +23,17 @@ type BagReader interface { // Close closes the underlying reader. Close() } + +// GetBagReader returns a bag writer of the specified type. +// Valid types include constants.BagWriterTypeFileSystem and +// constants.BagWriterTypeTar. +func GetBagReader(readerType string, validator *Validator) (BagReader, error) { + switch readerType { + case constants.BagReaderTypeFileSystem: + return NewFileSystemBagReader(validator) + case constants.BagWriterTypeTar: + return NewTarredBagReader(validator) + default: + return nil, constants.ErrUnknownType + } +} diff --git a/core/bag_reader_test.go b/core/bag_reader_test.go new file mode 100644 index 0000000..7f4bfce --- /dev/null +++ b/core/bag_reader_test.go @@ -0,0 +1,42 @@ +package core_test + +import ( + "os" + "path/filepath" + "testing" + + "github.com/APTrust/dart-runner/constants" + "github.com/APTrust/dart-runner/core" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetBagReader(t *testing.T) { + pathToBag := filepath.Join(os.TempDir(), "testbag.tar") + _, err := os.Create(pathToBag) + defer os.Remove(pathToBag) + require.NoError(t, err) + + profile := loadProfile(t, "aptrust-v2.2.json") + validator, err := core.NewValidator(pathToBag, profile) + require.Nil(t, err) + + // This should get us a file system bag reader + fsReader, err := core.GetBagReader(constants.BagWriterTypeFileSystem, validator) + require.NoError(t, err) + require.NotNil(t, fsReader) + _, ok := fsReader.(*core.FileSystemBagReader) + assert.True(t, ok) + + // And this should get us a tarred bag writer + tarReader, err := core.GetBagReader(constants.BagReaderTypeTar, validator) + require.NoError(t, err) + require.NotNil(t, fsReader) + _, ok = tarReader.(*core.TarredBagReader) + assert.True(t, ok) + + // And this should get us an error + noWriter, err := core.GetBagWriter("bad type", filepath.Join(os.TempDir(), "test.tar"), []string{}) + require.Error(t, err) + require.Nil(t, noWriter) +} diff --git a/core/file_system_bag_reader.go b/core/file_system_bag_reader.go index d2c4d3a..5b47069 100644 --- a/core/file_system_bag_reader.go +++ b/core/file_system_bag_reader.go @@ -4,3 +4,25 @@ package core // TODO: Define bag reader interface, similar to bag writer interface. // Then implement this so it's interchangeable with TarredBagReader. + +type FileSystemBagReader struct { + validator *Validator +} + +func NewFileSystemBagReader(validator *Validator) (*FileSystemBagReader, error) { + return &FileSystemBagReader{ + validator: validator, + }, nil +} + +func (reader *FileSystemBagReader) ScanMetadata() error { + return nil +} + +func (reader *FileSystemBagReader) ScanPayload() error { + return nil +} + +func (reader *FileSystemBagReader) Close() { + // No op? +}