staticpath.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package pathing
  2. import (
  3. "fmt"
  4. "path"
  5. )
  6. // StaticFileStoragePathFormatter is an implementation of the StoragePathFormatter interface
  7. // for static files whose path does not vary with time. The format is:
  8. //
  9. // <rootDir>/<pipeline>/<prefix>.<in>.<fileExt>
  10. //
  11. // If prefix is empty the leading dot is omitted. If fileExt is empty the trailing dot is omitted.
  12. type StaticFileStoragePathFormatter struct {
  13. rootDir string
  14. pipeline string
  15. }
  16. // NewStaticFileStoragePathFormatter creates a StaticFileStoragePathFormatter with the given
  17. // root directory and pipeline name.
  18. func NewStaticFileStoragePathFormatter(rootDir, pipeline string) (*StaticFileStoragePathFormatter, error) {
  19. if rootDir == "" {
  20. return nil, fmt.Errorf("rootDir cannot be empty")
  21. }
  22. if pipeline == "" {
  23. return nil, fmt.Errorf("pipeline cannot be empty")
  24. }
  25. return &StaticFileStoragePathFormatter{
  26. rootDir: rootDir,
  27. pipeline: pipeline,
  28. }, nil
  29. }
  30. // Dir returns the directory where static files are placed.
  31. func (s *StaticFileStoragePathFormatter) Dir() string {
  32. return path.Join(s.rootDir, s.pipeline)
  33. }
  34. // ToFullPath returns the full path for a static file. The in parameter is used as the
  35. // file name and may include subdirectory segments. prefix and fileExt are optional and
  36. // apply only to the base file name component of in.
  37. func (s *StaticFileStoragePathFormatter) ToFullPath(prefix string, in string, fileExt string) string {
  38. dir, base := path.Split(in)
  39. name := base
  40. if prefix != "" {
  41. name = fmt.Sprintf("%s.%s", prefix, base)
  42. }
  43. if fileExt != "" {
  44. name = fmt.Sprintf("%s.%s", name, fileExt)
  45. }
  46. return path.Join(s.rootDir, s.pipeline, dir, name)
  47. }