simplefiledao.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package dao
  2. import (
  3. "bufio"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "os"
  10. "github.com/google/uuid"
  11. "github.com/willie68/schematic-service-go/model"
  12. )
  13. // SimpleDAO a file based dao
  14. type SimpleDAO struct {
  15. Path string
  16. initialised bool
  17. tags []string
  18. manufacturers []string
  19. users map[string]string
  20. }
  21. // AddFile adding a file to the filestorage
  22. func (s *SimpleDAO) AddFile(filename string, reader io.Reader) (string, error) {
  23. id := uuid.New().String()
  24. filesPath := s.Path + "/files/" + id
  25. err := os.MkdirAll(filesPath, os.ModePerm)
  26. if err != nil {
  27. return "", err
  28. }
  29. f, err := os.Create(fmt.Sprintf("%s/%s", filesPath, filename))
  30. defer f.Close()
  31. writer := bufio.NewWriter(f)
  32. _, err = writer.ReadFrom(reader)
  33. if err != nil {
  34. return "", err
  35. }
  36. err = writer.Flush()
  37. if err != nil {
  38. return "", err
  39. }
  40. fmt.Printf("path %s\n", filesPath)
  41. return id, nil
  42. }
  43. //CreateSchematic creates a new schematic file
  44. func (s *SimpleDAO) CreateSchematic(schematic model.Schematic) (string, error) {
  45. id := uuid.New().String()
  46. filesPath := s.Path + "/schematics/" + id
  47. err := os.MkdirAll(filesPath, os.ModePerm)
  48. if err != nil {
  49. return "", err
  50. }
  51. schematicJson, _ := json.Marshal(schematic)
  52. filesPath = filesPath + "/schematic.json"
  53. err = ioutil.WriteFile(filesPath, schematicJson, 0644)
  54. if err != nil {
  55. return "", err
  56. }
  57. return id, nil
  58. }
  59. //GetSchematic getting a schematic from an id
  60. func (s *SimpleDAO) GetSchematic(schematicID string) (model.Schematic, error) {
  61. return model.Schematic{}, errors.New("not implemented yet")
  62. }
  63. //GetFile getting a stream of a file
  64. func (s *SimpleDAO) GetFile(fileid string, stream io.Writer) error {
  65. return errors.New("not implemented yet")
  66. }
  67. //GetSchematics query the schematics
  68. func (s *SimpleDAO) GetSchematics(query string, offset int, limit int, owner string) ([]model.Schematic, error) {
  69. return nil, errors.New("not implemented yet")
  70. }
  71. //CreateTag create a new tag
  72. func (s *SimpleDAO) CreateTag(tag string) error {
  73. return errors.New("not implemented yet")
  74. }
  75. //GetTags getting all tags
  76. func (s *SimpleDAO) GetTags() []string {
  77. return nil
  78. }
  79. //GetTagsCount getting the count of all tags
  80. func (s *SimpleDAO) GetTagsCount() int {
  81. return 0
  82. }
  83. //CreateManufacturer create a new manufacturer
  84. func (s *SimpleDAO) CreateManufacturer(manufacturer string) error {
  85. return errors.New("not implemented yet")
  86. }
  87. //GetManufacturers getting all manufacturers
  88. func (s *SimpleDAO) GetManufacturers() []string {
  89. return nil
  90. }
  91. //GetManufacturersCount getting the count of all manufacturers
  92. func (s *SimpleDAO) GetManufacturersCount() int {
  93. return 0
  94. }
  95. //CheckUser check user password
  96. func (s *SimpleDAO) CheckUser(username string, password string) bool {
  97. return true
  98. }
  99. //GetUser getting an user model
  100. func (s *SimpleDAO) GetUser(username string) (model.User, bool) {
  101. return model.User{}, false
  102. }
  103. //AddUser adding a new user
  104. func (s *SimpleDAO) AddUser(user model.User) error {
  105. return errors.New("not implemented yet")
  106. }
  107. //DeleteUser delete an user
  108. func (s *SimpleDAO) DeleteUser(username string) error {
  109. return errors.New("not implemented yet")
  110. }
  111. //ChangePWD change the password of a user
  112. func (s *SimpleDAO) ChangePWD(username string, newpassword string, oldpassword string) error {
  113. return errors.New("not implemented yet")
  114. }
  115. //DropAll drop all data
  116. func (s *SimpleDAO) DropAll() {
  117. }
  118. //Ping short ping if file system is availble
  119. func (s *SimpleDAO) Ping() error {
  120. return errors.New("not implemented yet")
  121. }
  122. //Stop this service
  123. func (s *SimpleDAO) Stop() {
  124. }