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.

62 lines
1.6 KiB

  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, error)
  9. StoreExisting(bucketName string, objectName string, existingFilePath string) (string, error)
  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, error) {
  16. directoryPath := filepath.Join(sp.basePath, bucketName)
  17. if err := sp.fileSystem.MkdirAll(directoryPath, os.ModePerm); err != nil {
  18. return "", err
  19. }
  20. filePath := filepath.Join(directoryPath, objectName)
  21. if err := afero.WriteFile(sp.fileSystem, filePath, data, os.ModePerm); err != nil {
  22. return "", err
  23. }
  24. return filePath, nil
  25. }
  26. func (sp FileSystemStorageProvider) StoreExisting(bucketName string, objectName string, existingFilePath string) (string, error) {
  27. bytesRead, err := afero.ReadFile(sp.fileSystem, existingFilePath)
  28. if err != nil {
  29. return "", err
  30. }
  31. return sp.StoreRaw(bucketName, objectName, bytesRead)
  32. }
  33. func GetFileSystemStorageProvider(basePath string, wd string) FileSystemStorageProvider {
  34. if wd == "" {
  35. wd, _ = os.Getwd()
  36. }
  37. return FileSystemStorageProvider{
  38. fileSystem: afero.NewBasePathFs(afero.NewOsFs(), filepath.Join(wd, "files")),
  39. basePath: basePath,
  40. }
  41. }
  42. // TODO: Move this out of this file
  43. func GetMemoryStorageProvider() FileSystemStorageProvider {
  44. return FileSystemStorageProvider{
  45. fileSystem: afero.NewBasePathFs(afero.NewMemMapFs(), "/"),
  46. basePath: "/tmp/foo/bar",
  47. }
  48. }