bokoworker.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package bokoworker
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. "imuslab.com/bokofs/bokofsd/mod/bokofs/bokofile"
  7. "imuslab.com/bokofs/bokofsd/mod/bokofs/bokothumb"
  8. )
  9. /*
  10. Boko Worker
  11. A boko worker is an instance of WebDAV file server that serves a specific
  12. disk partition or subpath in which the user can interact with the disk
  13. via WebDAV interface
  14. */
  15. type Options struct {
  16. NodeName string //The node name (also the id) of the directory tree, e.g. disk1
  17. ServePath string // The actual path to serve, e.g. /media/disk1/mydir
  18. ThumbnailStore string // The path to the thumbnail store, e.g. /media/disk1/thumbs
  19. }
  20. type Worker struct {
  21. /* Worker Properties */
  22. NodeName string //The node name (also the id) of the directory tree, e.g. disk1
  23. ServePath string // The actual path to serve, e.g. /media/disk1/mydir
  24. /* Runtime Properties */
  25. Filesystem *bokofile.RouterDir //The file system to serve
  26. Thumbnails *bokothumb.RouterDir //Thumbnail interface for this worker
  27. }
  28. // NewFSWorker creates a new file system worker from a directory
  29. func NewFSWorker(options *Options) (*Worker, error) {
  30. nodeName := options.NodeName
  31. mountPath := options.ServePath
  32. thumbnailStore := options.ThumbnailStore
  33. if !strings.HasPrefix(nodeName, "/") {
  34. nodeName = "/" + nodeName
  35. }
  36. mountPath, _ = filepath.Abs(mountPath)
  37. fs, err := bokofile.CreateRouterFromDir(mountPath, nodeName, false)
  38. if err != nil {
  39. return nil, err
  40. }
  41. //Create the thumbnail store if it does not exist
  42. os.MkdirAll(thumbnailStore, 0755)
  43. thumbrender, err := bokothumb.CreateThumbnailRenderer(thumbnailStore, mountPath, nodeName, false)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return &Worker{
  48. NodeName: nodeName,
  49. ServePath: mountPath,
  50. Filesystem: fs,
  51. Thumbnails: thumbrender,
  52. }, nil
  53. }