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.

36 lines
950 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) string
  9. storeExisting(bucketName string, objectName string, existingFilePath string) 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) string {
  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. return filePath
  21. }
  22. func (sp FileSystemStorageProvider) storeExisting(bucketName string, objectName string, existingFilePath string) string {
  23. bytesRead, _ := afero.ReadFile(sp.fileSystem, existingFilePath)
  24. return sp.storeRaw(bucketName, objectName, bytesRead)
  25. }