resource.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package mysql
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "github.com/pkg/errors"
  8. "google.golang.org/protobuf/encoding/protojson"
  9. storepb "github.com/usememos/memos/proto/gen/store"
  10. "github.com/usememos/memos/store"
  11. )
  12. func (d *DB) CreateResource(ctx context.Context, create *store.Resource) (*store.Resource, error) {
  13. fields := []string{"`uid`", "`filename`", "`blob`", "`type`", "`size`", "`creator_id`", "`memo_id`", "`storage_type`", "`reference`", "`payload`"}
  14. placeholder := []string{"?", "?", "?", "?", "?", "?", "?", "?", "?", "?"}
  15. storageType := ""
  16. if create.StorageType != storepb.ResourceStorageType_RESOURCE_STORAGE_TYPE_UNSPECIFIED {
  17. storageType = create.StorageType.String()
  18. }
  19. payloadString := "{}"
  20. if create.Payload != nil {
  21. bytes, err := protojson.Marshal(create.Payload)
  22. if err != nil {
  23. return nil, errors.Wrap(err, "failed to marshal resource payload")
  24. }
  25. payloadString = string(bytes)
  26. }
  27. args := []any{create.UID, create.Filename, create.Blob, create.Type, create.Size, create.CreatorID, create.MemoID, storageType, create.Reference, payloadString}
  28. stmt := "INSERT INTO `resource` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
  29. result, err := d.db.ExecContext(ctx, stmt, args...)
  30. if err != nil {
  31. return nil, err
  32. }
  33. id, err := result.LastInsertId()
  34. if err != nil {
  35. return nil, err
  36. }
  37. id32 := int32(id)
  38. return d.GetResource(ctx, &store.FindResource{ID: &id32})
  39. }
  40. func (d *DB) ListResources(ctx context.Context, find *store.FindResource) ([]*store.Resource, error) {
  41. where, args := []string{"1 = 1"}, []any{}
  42. if v := find.ID; v != nil {
  43. where, args = append(where, "`id` = ?"), append(args, *v)
  44. }
  45. if v := find.UID; v != nil {
  46. where, args = append(where, "`uid` = ?"), append(args, *v)
  47. }
  48. if v := find.CreatorID; v != nil {
  49. where, args = append(where, "`creator_id` = ?"), append(args, *v)
  50. }
  51. if v := find.Filename; v != nil {
  52. where, args = append(where, "`filename` = ?"), append(args, *v)
  53. }
  54. if v := find.FilenameSearch; v != nil {
  55. where, args = append(where, "`filename` LIKE ?"), append(args, "%"+*v+"%")
  56. }
  57. if v := find.MemoID; v != nil {
  58. where, args = append(where, "`memo_id` = ?"), append(args, *v)
  59. }
  60. if find.HasRelatedMemo {
  61. where = append(where, "`memo_id` IS NOT NULL")
  62. }
  63. if find.StorageType != nil {
  64. where, args = append(where, "`storage_type` = ?"), append(args, find.StorageType.String())
  65. }
  66. fields := []string{"`id`", "`uid`", "`filename`", "`type`", "`size`", "`creator_id`", "UNIX_TIMESTAMP(`created_ts`)", "UNIX_TIMESTAMP(`updated_ts`)", "`memo_id`", "`storage_type`", "`reference`", "`payload`"}
  67. if find.GetBlob {
  68. fields = append(fields, "`blob`")
  69. }
  70. query := fmt.Sprintf("SELECT %s FROM `resource` WHERE %s ORDER BY `updated_ts` DESC", strings.Join(fields, ", "), strings.Join(where, " AND "))
  71. if find.Limit != nil {
  72. query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
  73. if find.Offset != nil {
  74. query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
  75. }
  76. }
  77. rows, err := d.db.QueryContext(ctx, query, args...)
  78. if err != nil {
  79. return nil, err
  80. }
  81. defer rows.Close()
  82. list := make([]*store.Resource, 0)
  83. for rows.Next() {
  84. resource := store.Resource{}
  85. var memoID sql.NullInt32
  86. var storageType string
  87. var payloadBytes []byte
  88. dests := []any{
  89. &resource.ID,
  90. &resource.UID,
  91. &resource.Filename,
  92. &resource.Type,
  93. &resource.Size,
  94. &resource.CreatorID,
  95. &resource.CreatedTs,
  96. &resource.UpdatedTs,
  97. &memoID,
  98. &storageType,
  99. &resource.Reference,
  100. &payloadBytes,
  101. }
  102. if find.GetBlob {
  103. dests = append(dests, &resource.Blob)
  104. }
  105. if err := rows.Scan(dests...); err != nil {
  106. return nil, err
  107. }
  108. if memoID.Valid {
  109. resource.MemoID = &memoID.Int32
  110. }
  111. resource.StorageType = storepb.ResourceStorageType(storepb.ResourceStorageType_value[storageType])
  112. payload := &storepb.ResourcePayload{}
  113. if err := protojsonUnmarshaler.Unmarshal(payloadBytes, payload); err != nil {
  114. return nil, err
  115. }
  116. resource.Payload = payload
  117. list = append(list, &resource)
  118. }
  119. if err := rows.Err(); err != nil {
  120. return nil, err
  121. }
  122. return list, nil
  123. }
  124. func (d *DB) GetResource(ctx context.Context, find *store.FindResource) (*store.Resource, error) {
  125. list, err := d.ListResources(ctx, find)
  126. if err != nil {
  127. return nil, err
  128. }
  129. if len(list) == 0 {
  130. return nil, nil
  131. }
  132. return list[0], nil
  133. }
  134. func (d *DB) UpdateResource(ctx context.Context, update *store.UpdateResource) error {
  135. set, args := []string{}, []any{}
  136. if v := update.UID; v != nil {
  137. set, args = append(set, "`uid` = ?"), append(args, *v)
  138. }
  139. if v := update.UpdatedTs; v != nil {
  140. set, args = append(set, "`updated_ts` = FROM_UNIXTIME(?)"), append(args, *v)
  141. }
  142. if v := update.Filename; v != nil {
  143. set, args = append(set, "`filename` = ?"), append(args, *v)
  144. }
  145. if v := update.MemoID; v != nil {
  146. set, args = append(set, "`memo_id` = ?"), append(args, *v)
  147. }
  148. if v := update.Reference; v != nil {
  149. set, args = append(set, "`reference` = ?"), append(args, *v)
  150. }
  151. if v := update.Payload; v != nil {
  152. bytes, err := protojson.Marshal(v)
  153. if err != nil {
  154. return errors.Wrap(err, "failed to marshal resource payload")
  155. }
  156. set, args = append(set, "`payload` = ?"), append(args, string(bytes))
  157. }
  158. args = append(args, update.ID)
  159. stmt := "UPDATE `resource` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"
  160. result, err := d.db.ExecContext(ctx, stmt, args...)
  161. if err != nil {
  162. return err
  163. }
  164. if _, err := result.RowsAffected(); err != nil {
  165. return err
  166. }
  167. return nil
  168. }
  169. func (d *DB) DeleteResource(ctx context.Context, delete *store.DeleteResource) error {
  170. stmt := "DELETE FROM `resource` WHERE `id` = ?"
  171. result, err := d.db.ExecContext(ctx, stmt, delete.ID)
  172. if err != nil {
  173. return err
  174. }
  175. if _, err := result.RowsAffected(); err != nil {
  176. return err
  177. }
  178. return nil
  179. }