resource.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package mysql
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "github.com/usememos/memos/store"
  8. )
  9. func (d *DB) CreateResource(ctx context.Context, create *store.Resource) (*store.Resource, error) {
  10. fields := []string{"`resource_name`", "`filename`", "`blob`", "`external_link`", "`type`", "`size`", "`creator_id`", "`internal_path`", "`memo_id`"}
  11. placeholder := []string{"?", "?", "?", "?", "?", "?", "?", "?", "?"}
  12. args := []any{create.ResourceName, create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID, create.InternalPath, create.MemoID}
  13. stmt := "INSERT INTO `resource` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
  14. result, err := d.db.ExecContext(ctx, stmt, args...)
  15. if err != nil {
  16. return nil, err
  17. }
  18. id, err := result.LastInsertId()
  19. if err != nil {
  20. return nil, err
  21. }
  22. id32 := int32(id)
  23. return d.GetResource(ctx, &store.FindResource{ID: &id32})
  24. }
  25. func (d *DB) ListResources(ctx context.Context, find *store.FindResource) ([]*store.Resource, error) {
  26. where, args := []string{"1 = 1"}, []any{}
  27. if v := find.ID; v != nil {
  28. where, args = append(where, "`id` = ?"), append(args, *v)
  29. }
  30. if v := find.ResourceName; v != nil {
  31. where, args = append(where, "`resource_name` = ?"), append(args, *v)
  32. }
  33. if v := find.CreatorID; v != nil {
  34. where, args = append(where, "`creator_id` = ?"), append(args, *v)
  35. }
  36. if v := find.Filename; v != nil {
  37. where, args = append(where, "`filename` = ?"), append(args, *v)
  38. }
  39. if v := find.MemoID; v != nil {
  40. where, args = append(where, "`memo_id` = ?"), append(args, *v)
  41. }
  42. if find.HasRelatedMemo {
  43. where = append(where, "`memo_id` IS NOT NULL")
  44. }
  45. fields := []string{"`id`", "`resource_name`", "`filename`", "`external_link`", "`type`", "`size`", "`creator_id`", "UNIX_TIMESTAMP(`created_ts`)", "UNIX_TIMESTAMP(`updated_ts`)", "`internal_path`", "`memo_id`"}
  46. if find.GetBlob {
  47. fields = append(fields, "`blob`")
  48. }
  49. query := fmt.Sprintf("SELECT %s FROM `resource` WHERE %s ORDER BY `updated_ts` DESC, `created_ts` DESC", strings.Join(fields, ", "), strings.Join(where, " AND "))
  50. if find.Limit != nil {
  51. query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
  52. if find.Offset != nil {
  53. query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
  54. }
  55. }
  56. rows, err := d.db.QueryContext(ctx, query, args...)
  57. if err != nil {
  58. return nil, err
  59. }
  60. defer rows.Close()
  61. list := make([]*store.Resource, 0)
  62. for rows.Next() {
  63. resource := store.Resource{}
  64. var memoID sql.NullInt32
  65. dests := []any{
  66. &resource.ID,
  67. &resource.ResourceName,
  68. &resource.Filename,
  69. &resource.ExternalLink,
  70. &resource.Type,
  71. &resource.Size,
  72. &resource.CreatorID,
  73. &resource.CreatedTs,
  74. &resource.UpdatedTs,
  75. &resource.InternalPath,
  76. &memoID,
  77. }
  78. if find.GetBlob {
  79. dests = append(dests, &resource.Blob)
  80. }
  81. if err := rows.Scan(dests...); err != nil {
  82. return nil, err
  83. }
  84. if memoID.Valid {
  85. resource.MemoID = &memoID.Int32
  86. }
  87. list = append(list, &resource)
  88. }
  89. if err := rows.Err(); err != nil {
  90. return nil, err
  91. }
  92. return list, nil
  93. }
  94. func (d *DB) GetResource(ctx context.Context, find *store.FindResource) (*store.Resource, error) {
  95. list, err := d.ListResources(ctx, find)
  96. if err != nil {
  97. return nil, err
  98. }
  99. if len(list) == 0 {
  100. return nil, nil
  101. }
  102. return list[0], nil
  103. }
  104. func (d *DB) UpdateResource(ctx context.Context, update *store.UpdateResource) (*store.Resource, error) {
  105. set, args := []string{}, []any{}
  106. if v := update.ResourceName; v != nil {
  107. set, args = append(set, "`resource_name` = ?"), append(args, *v)
  108. }
  109. if v := update.UpdatedTs; v != nil {
  110. set, args = append(set, "`updated_ts` = FROM_UNIXTIME(?)"), append(args, *v)
  111. }
  112. if v := update.Filename; v != nil {
  113. set, args = append(set, "`filename` = ?"), append(args, *v)
  114. }
  115. if v := update.InternalPath; v != nil {
  116. set, args = append(set, "`internal_path` = ?"), append(args, *v)
  117. }
  118. if v := update.ExternalLink; v != nil {
  119. set, args = append(set, "`external_link` = ?"), append(args, *v)
  120. }
  121. if v := update.MemoID; v != nil {
  122. set, args = append(set, "`memo_id` = ?"), append(args, *v)
  123. }
  124. if v := update.Blob; v != nil {
  125. set, args = append(set, "`blob` = ?"), append(args, v)
  126. }
  127. args = append(args, update.ID)
  128. stmt := "UPDATE `resource` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"
  129. if _, err := d.db.ExecContext(ctx, stmt, args...); err != nil {
  130. return nil, err
  131. }
  132. return d.GetResource(ctx, &store.FindResource{ID: &update.ID})
  133. }
  134. func (d *DB) DeleteResource(ctx context.Context, delete *store.DeleteResource) error {
  135. stmt := "DELETE FROM `resource` WHERE `id` = ?"
  136. result, err := d.db.ExecContext(ctx, stmt, delete.ID)
  137. if err != nil {
  138. return err
  139. }
  140. if _, err := result.RowsAffected(); err != nil {
  141. return err
  142. }
  143. if err := d.Vacuum(ctx); err != nil {
  144. // Prevent linter warning.
  145. return err
  146. }
  147. return nil
  148. }
  149. func vacuumResource(ctx context.Context, tx *sql.Tx) error {
  150. stmt := "DELETE FROM `resource` WHERE `creator_id` NOT IN (SELECT `id` FROM `user`)"
  151. _, err := tx.ExecContext(ctx, stmt)
  152. if err != nil {
  153. return err
  154. }
  155. return nil
  156. }