resource.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.MemoID; v != nil {
  55. where, args = append(where, "`memo_id` = ?"), append(args, *v)
  56. }
  57. if find.HasRelatedMemo {
  58. where = append(where, "`memo_id` IS NOT NULL")
  59. }
  60. if find.StorageType != nil {
  61. where, args = append(where, "`storage_type` = ?"), append(args, find.StorageType.String())
  62. }
  63. fields := []string{"`id`", "`uid`", "`filename`", "`type`", "`size`", "`creator_id`", "UNIX_TIMESTAMP(`created_ts`)", "UNIX_TIMESTAMP(`updated_ts`)", "`memo_id`", "`storage_type`", "`reference`", "`payload`"}
  64. if find.GetBlob {
  65. fields = append(fields, "`blob`")
  66. }
  67. query := fmt.Sprintf("SELECT %s FROM `resource` WHERE %s ORDER BY `updated_ts` DESC", strings.Join(fields, ", "), strings.Join(where, " AND "))
  68. if find.Limit != nil {
  69. query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
  70. if find.Offset != nil {
  71. query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
  72. }
  73. }
  74. rows, err := d.db.QueryContext(ctx, query, args...)
  75. if err != nil {
  76. return nil, err
  77. }
  78. defer rows.Close()
  79. list := make([]*store.Resource, 0)
  80. for rows.Next() {
  81. resource := store.Resource{}
  82. var memoID sql.NullInt32
  83. var storageType string
  84. var payloadBytes []byte
  85. dests := []any{
  86. &resource.ID,
  87. &resource.UID,
  88. &resource.Filename,
  89. &resource.Type,
  90. &resource.Size,
  91. &resource.CreatorID,
  92. &resource.CreatedTs,
  93. &resource.UpdatedTs,
  94. &memoID,
  95. &storageType,
  96. &resource.Reference,
  97. &payloadBytes,
  98. }
  99. if find.GetBlob {
  100. dests = append(dests, &resource.Blob)
  101. }
  102. if err := rows.Scan(dests...); err != nil {
  103. return nil, err
  104. }
  105. if memoID.Valid {
  106. resource.MemoID = &memoID.Int32
  107. }
  108. resource.StorageType = storepb.ResourceStorageType(storepb.ResourceStorageType_value[storageType])
  109. payload := &storepb.ResourcePayload{}
  110. if err := protojsonUnmarshaler.Unmarshal(payloadBytes, payload); err != nil {
  111. return nil, err
  112. }
  113. resource.Payload = payload
  114. list = append(list, &resource)
  115. }
  116. if err := rows.Err(); err != nil {
  117. return nil, err
  118. }
  119. return list, nil
  120. }
  121. func (d *DB) GetResource(ctx context.Context, find *store.FindResource) (*store.Resource, error) {
  122. list, err := d.ListResources(ctx, find)
  123. if err != nil {
  124. return nil, err
  125. }
  126. if len(list) == 0 {
  127. return nil, nil
  128. }
  129. return list[0], nil
  130. }
  131. func (d *DB) UpdateResource(ctx context.Context, update *store.UpdateResource) error {
  132. set, args := []string{}, []any{}
  133. if v := update.UID; v != nil {
  134. set, args = append(set, "`uid` = ?"), append(args, *v)
  135. }
  136. if v := update.UpdatedTs; v != nil {
  137. set, args = append(set, "`updated_ts` = FROM_UNIXTIME(?)"), append(args, *v)
  138. }
  139. if v := update.Filename; v != nil {
  140. set, args = append(set, "`filename` = ?"), append(args, *v)
  141. }
  142. if v := update.MemoID; v != nil {
  143. set, args = append(set, "`memo_id` = ?"), append(args, *v)
  144. }
  145. if v := update.Reference; v != nil {
  146. set, args = append(set, "`reference` = ?"), append(args, *v)
  147. }
  148. if v := update.Payload; v != nil {
  149. bytes, err := protojson.Marshal(v)
  150. if err != nil {
  151. return errors.Wrap(err, "failed to marshal resource payload")
  152. }
  153. set, args = append(set, "`payload` = ?"), append(args, string(bytes))
  154. }
  155. args = append(args, update.ID)
  156. stmt := "UPDATE `resource` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"
  157. result, err := d.db.ExecContext(ctx, stmt, args...)
  158. if err != nil {
  159. return err
  160. }
  161. if _, err := result.RowsAffected(); err != nil {
  162. return err
  163. }
  164. return nil
  165. }
  166. func (d *DB) DeleteResource(ctx context.Context, delete *store.DeleteResource) error {
  167. stmt := "DELETE FROM `resource` WHERE `id` = ?"
  168. result, err := d.db.ExecContext(ctx, stmt, delete.ID)
  169. if err != nil {
  170. return err
  171. }
  172. if _, err := result.RowsAffected(); err != nil {
  173. return err
  174. }
  175. return nil
  176. }