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.

75 lines
2.0 KiB

  1. package storage
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/spf13/afero"
  6. )
  7. const StorageFolderName = "files"
  8. type IStorageProvider interface {
  9. StoreRaw(bucketName string, objectName string, data []byte) (string, error)
  10. StoreExisting(bucketName string, objectName string, existingFilePath string) (string, error)
  11. GetPath(bucketName string, objectName string) string
  12. }
  13. type FileSystemStorageProvider struct {
  14. fileSystem afero.Fs
  15. basePath string
  16. wd string
  17. }
  18. func (sp FileSystemStorageProvider) StoreRaw(bucketName string, objectName string, data []byte) (string, error) {
  19. directoryPath := filepath.Join(sp.basePath, bucketName)
  20. if err := sp.fileSystem.MkdirAll(directoryPath, os.ModePerm); err != nil {
  21. return "", err
  22. }
  23. filePath := filepath.Join(directoryPath, objectName)
  24. if err := afero.WriteFile(sp.fileSystem, filePath, data, os.ModePerm); err != nil {
  25. return "", err
  26. }
  27. return filePath, nil
  28. }
  29. func (sp FileSystemStorageProvider) StoreExisting(bucketName string, objectName string, existingFilePath string) (string, error) {
  30. bytesRead, err := os.ReadFile(existingFilePath)
  31. if err != nil {
  32. return "", err
  33. }
  34. return sp.StoreRaw(bucketName, objectName, bytesRead)
  35. }
  36. func (sp FileSystemStorageProvider) GetPath(bucketName string, objectName string) string {
  37. return filepath.Join(sp.wd, StorageFolderName, sp.basePath, bucketName, objectName)
  38. }
  39. func (sp FileSystemStorageProvider) OpenFile(bucketName string, objectName string) (*os.File, error) {
  40. return os.Open(sp.GetPath(bucketName, objectName))
  41. }
  42. func GetFileSystemStorageProvider(basePath string, wd string) FileSystemStorageProvider {
  43. if wd == "" {
  44. wd, _ = os.Getwd()
  45. }
  46. return FileSystemStorageProvider{
  47. fileSystem: afero.NewBasePathFs(afero.NewOsFs(), filepath.Join(wd, StorageFolderName)),
  48. basePath: basePath,
  49. wd: wd,
  50. }
  51. }
  52. // TODO: Move this out of this file
  53. func GetMemoryStorageProvider() FileSystemStorageProvider {
  54. return FileSystemStorageProvider{
  55. fileSystem: afero.NewBasePathFs(afero.NewMemMapFs(), "/"),
  56. basePath: "/tmp/foo/bar",
  57. }
  58. }