libraryService.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package service
  2. import (
  3. "iot_manager_service/app/multimedia/dao"
  4. "iot_manager_service/app/multimedia/model"
  5. "iot_manager_service/app/system/service"
  6. "iot_manager_service/config"
  7. "iot_manager_service/util/common"
  8. "iot_manager_service/util/logger"
  9. "iot_manager_service/util/minio"
  10. "mime/multipart"
  11. "strings"
  12. "time"
  13. )
  14. var LibraryService = new(libraryService)
  15. type libraryService struct{}
  16. func (s *libraryService) Get(id int) (*dao.Library, *common.Errors) {
  17. library := &dao.Library{
  18. ID: id,
  19. }
  20. err := library.Get()
  21. if err != nil {
  22. return nil, common.FailResponse(err.Error(), nil)
  23. }
  24. return library, nil
  25. }
  26. func (s *libraryService) List(tenantId int, searchValue string, current, size int) ([]model.LibraryDetail,
  27. *common.Errors) {
  28. library := &dao.Library{
  29. TenantId: tenantId,
  30. }
  31. offset := (current - 1) * size
  32. limit := size
  33. if searchValue != "" {
  34. library.LibName = searchValue
  35. }
  36. libraries, err := library.GetLibraries(offset, limit)
  37. if err != nil {
  38. return nil, common.FailResponse(err.Error(), nil)
  39. }
  40. var rsp []model.LibraryDetail
  41. for _, lib := range libraries {
  42. rsp = append(rsp, model.LibToModel(lib))
  43. }
  44. return rsp, nil
  45. }
  46. func (s *libraryService) Remove(userId int64, tenantId int, id int) *common.Errors {
  47. library := &dao.Library{
  48. ID: id,
  49. IsDeleted: 1,
  50. UpdateUser: userId,
  51. UpdateTime: time.Now(),
  52. }
  53. err := library.Delete()
  54. if err != nil {
  55. return common.FailResponse(err.Error(), nil)
  56. }
  57. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeInfoBar,
  58. common.DeviceTypeDefault, "", common.OperationSuccess)
  59. return nil
  60. }
  61. func (s *libraryService) GetList(tenantId int, libName string, libType int) ([]dao.Library, *common.Errors) {
  62. library := &dao.Library{
  63. TenantId: tenantId,
  64. LibName: libName,
  65. LibType: libType,
  66. }
  67. libraries, err := library.GetAll()
  68. if err != nil {
  69. return nil, common.FailResponse(err.Error(), nil)
  70. }
  71. return libraries, nil
  72. }
  73. func (s *libraryService) UploadFile(tenantId int, fileHeader *multipart.FileHeader) (*model.RspUploadFile,
  74. *common.Errors) {
  75. fileContent, err := fileHeader.Open()
  76. if err != nil {
  77. logger.Logger.Errorf("UploadFile fileHeader.Open fail, err = %s", err.Error())
  78. return nil, common.ParamsInvalidResponse(err.Error(), nil)
  79. }
  80. fileNameArray := strings.Split(fileHeader.Filename, ".")
  81. if len(fileNameArray) < 2 {
  82. return nil, common.ParamsInvalidResponse("invalid file name", nil)
  83. }
  84. // happy8as8f0980f_92389h.jpg
  85. objectName := "library/" + common.RandomString2(16) + "_" + common.RandomString2(6) + "." + fileNameArray[len(
  86. fileNameArray)-1]
  87. err = minio.PutFile(minio.FileObject{
  88. TenantId: tenantId,
  89. ObjectName: objectName,
  90. ObjectSize: fileHeader.Size,
  91. Reader: fileContent,
  92. })
  93. if err != nil {
  94. logger.Logger.Errorf("UploadFile PutFile fail, err = %s", err.Error())
  95. return nil, common.FailResponse("", nil)
  96. }
  97. contentTypeArr := fileHeader.Header["Content-Type"]
  98. contentType := ""
  99. if len(contentTypeArr) > 0 {
  100. contentType = contentTypeArr[0]
  101. }
  102. fileType := 0
  103. if contentType == "image/jpeg" {
  104. fileType = 2
  105. } else if contentType == "video/mp4" {
  106. fileType = 1
  107. } else if contentType == "audio/mpeg" {
  108. fileType = 3
  109. }
  110. return &model.RspUploadFile{
  111. Link: config.Instance().Minio.Endpoint,
  112. Domain: "",
  113. Name: objectName,
  114. OriginalName: fileHeader.Filename,
  115. AttachId: 0,
  116. Duration: 0,
  117. Resolution: "",
  118. FileSize: fileHeader.Size,
  119. FileType: fileType,
  120. }, nil
  121. }
  122. func (s *libraryService) Submit(tenantId int, userId int64, req *model.ReqLibrarySubmit) *common.Errors {
  123. library := &dao.Library{
  124. ID: req.Id,
  125. MaterialAddress: req.MaterialAddress,
  126. LibName: req.LibName,
  127. LibExplain: req.LibExplain,
  128. LibType: req.LibType,
  129. Resolution: req.Resolution,
  130. FileSize: req.FileSize,
  131. LibDuration: req.LibDuration,
  132. TalkSpeed: req.TalkSpeed,
  133. TenantId: tenantId,
  134. UpdateTime: time.Now(),
  135. UpdateUser: userId,
  136. IsDeleted: 0,
  137. }
  138. if library.ID == 0 {
  139. library.CreateTime = time.Now()
  140. library.CreateUser = userId
  141. if err := library.Create(); err != nil {
  142. logger.Logger.Errorf("Create err = %s \n", err.Error())
  143. return common.FailResponse(err.Error(), nil)
  144. }
  145. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeInfoBar,
  146. common.DeviceTypeDefault, library.LibName, common.OperationSuccess)
  147. return common.SuccessResponse(common.Succeeded, nil)
  148. }
  149. if err := library.Update(); err != nil {
  150. logger.Logger.Errorf("Update err = %s \n", err.Error())
  151. return common.FailResponse(err.Error(), nil)
  152. }
  153. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeInfoBar,
  154. common.DeviceTypeDefault, library.LibName, common.OperationSuccess)
  155. return common.SuccessResponse(common.Succeeded, nil)
  156. }