Micro-service for file storage and processing written in Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
896 B

  1. package storage
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/spf13/afero"
  6. )
  7. type IStorageProvider interface {
  8. storeRaw(bucketName string, objectName string, data []byte)
  9. storeExisting(bucketName string, objectName string, existingFilePath string)
  10. }
  11. type FileSystemStorageProvider struct {
  12. fileSystem afero.Fs
  13. basePath string
  14. }
  15. func (sp FileSystemStorageProvider) storeRaw(bucketName string, objectName string, data []byte) {
  16. directoryPath := filepath.Join(sp.basePath, bucketName)
  17. sp.fileSystem.MkdirAll(directoryPath, os.ModePerm)
  18. filePath := filepath.Join(directoryPath, objectName)
  19. afero.WriteFile(sp.fileSystem, filePath, data, os.ModePerm)
  20. }
  21. func (sp FileSystemStorageProvider) storeExisting(bucketName string, objectName string, existingFilePath string) {
  22. bytesRead, _ := afero.ReadFile(sp.fileSystem, existingFilePath)
  23. sp.storeRaw(bucketName, objectName, bytesRead)
  24. }