resource.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package store
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "github.com/usememos/memos/api"
  8. "github.com/usememos/memos/common"
  9. )
  10. // resourceRaw is the store model for an Resource.
  11. // Fields have exactly the same meanings as Resource.
  12. type resourceRaw struct {
  13. ID int
  14. // Standard fields
  15. CreatorID int
  16. CreatedTs int64
  17. UpdatedTs int64
  18. // Domain specific fields
  19. Filename string
  20. Blob []byte
  21. InternalPath string
  22. ExternalLink string
  23. Type string
  24. Size int64
  25. PublicID string
  26. LinkedMemoAmount int
  27. }
  28. func (raw *resourceRaw) toResource() *api.Resource {
  29. return &api.Resource{
  30. ID: raw.ID,
  31. // Standard fields
  32. CreatorID: raw.CreatorID,
  33. CreatedTs: raw.CreatedTs,
  34. UpdatedTs: raw.UpdatedTs,
  35. // Domain specific fields
  36. Filename: raw.Filename,
  37. Blob: raw.Blob,
  38. InternalPath: raw.InternalPath,
  39. ExternalLink: raw.ExternalLink,
  40. Type: raw.Type,
  41. Size: raw.Size,
  42. PublicID: raw.PublicID,
  43. LinkedMemoAmount: raw.LinkedMemoAmount,
  44. }
  45. }
  46. func (s *Store) CreateResource(ctx context.Context, create *api.ResourceCreate) (*api.Resource, error) {
  47. tx, err := s.db.BeginTx(ctx, nil)
  48. if err != nil {
  49. return nil, FormatError(err)
  50. }
  51. defer tx.Rollback()
  52. resourceRaw, err := createResourceImpl(ctx, tx, create)
  53. if err != nil {
  54. return nil, err
  55. }
  56. if err := tx.Commit(); err != nil {
  57. return nil, FormatError(err)
  58. }
  59. resource := resourceRaw.toResource()
  60. return resource, nil
  61. }
  62. func (s *Store) PatchResource(ctx context.Context, patch *api.ResourcePatch) (*api.Resource, error) {
  63. tx, err := s.db.BeginTx(ctx, nil)
  64. if err != nil {
  65. return nil, FormatError(err)
  66. }
  67. defer tx.Rollback()
  68. resourceRaw, err := patchResourceImpl(ctx, tx, patch)
  69. if err != nil {
  70. return nil, err
  71. }
  72. if err := tx.Commit(); err != nil {
  73. return nil, FormatError(err)
  74. }
  75. s.resourceCache.Store(resourceRaw.ID, resourceRaw)
  76. resource := resourceRaw.toResource()
  77. return resource, nil
  78. }
  79. func (s *Store) FindResourceList(ctx context.Context, find *api.ResourceFind) ([]*api.Resource, error) {
  80. tx, err := s.db.BeginTx(ctx, nil)
  81. if err != nil {
  82. return nil, FormatError(err)
  83. }
  84. defer tx.Rollback()
  85. resourceRawList, err := findResourceListImpl(ctx, tx, find)
  86. if err != nil {
  87. return nil, err
  88. }
  89. resourceList := []*api.Resource{}
  90. for _, raw := range resourceRawList {
  91. if !find.GetBlob {
  92. s.resourceCache.Store(raw.ID, raw)
  93. }
  94. resourceList = append(resourceList, raw.toResource())
  95. }
  96. return resourceList, nil
  97. }
  98. func (s *Store) FindResource(ctx context.Context, find *api.ResourceFind) (*api.Resource, error) {
  99. if !find.GetBlob && find.ID != nil {
  100. if raw, ok := s.resourceCache.Load(find.ID); ok {
  101. return raw.(*resourceRaw).toResource(), nil
  102. }
  103. }
  104. tx, err := s.db.BeginTx(ctx, nil)
  105. if err != nil {
  106. return nil, FormatError(err)
  107. }
  108. defer tx.Rollback()
  109. list, err := findResourceListImpl(ctx, tx, find)
  110. if err != nil {
  111. return nil, err
  112. }
  113. if len(list) == 0 {
  114. return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")}
  115. }
  116. resourceRaw := list[0]
  117. if !find.GetBlob {
  118. s.resourceCache.Store(resourceRaw.ID, resourceRaw)
  119. }
  120. resource := resourceRaw.toResource()
  121. return resource, nil
  122. }
  123. func (s *Store) DeleteResource(ctx context.Context, delete *api.ResourceDelete) error {
  124. tx, err := s.db.BeginTx(ctx, nil)
  125. if err != nil {
  126. return FormatError(err)
  127. }
  128. defer tx.Rollback()
  129. if err := deleteResource(ctx, tx, delete); err != nil {
  130. return err
  131. }
  132. if err := s.vacuumImpl(ctx, tx); err != nil {
  133. return err
  134. }
  135. if err := tx.Commit(); err != nil {
  136. return FormatError(err)
  137. }
  138. s.resourceCache.Delete(delete.ID)
  139. return nil
  140. }
  141. func createResourceImpl(ctx context.Context, tx *sql.Tx, create *api.ResourceCreate) (*resourceRaw, error) {
  142. fields := []string{"filename", "blob", "external_link", "type", "size", "creator_id", "internal_path", "public_id"}
  143. values := []any{create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID, create.InternalPath, create.PublicID}
  144. placeholders := []string{"?", "?", "?", "?", "?", "?", "?", "?"}
  145. query := `
  146. INSERT INTO resource (
  147. ` + strings.Join(fields, ",") + `
  148. )
  149. VALUES (` + strings.Join(placeholders, ",") + `)
  150. RETURNING id, ` + strings.Join(fields, ",") + `, created_ts, updated_ts
  151. `
  152. var resourceRaw resourceRaw
  153. dests := []any{
  154. &resourceRaw.ID,
  155. &resourceRaw.Filename,
  156. &resourceRaw.Blob,
  157. &resourceRaw.ExternalLink,
  158. &resourceRaw.Type,
  159. &resourceRaw.Size,
  160. &resourceRaw.CreatorID,
  161. &resourceRaw.InternalPath,
  162. &resourceRaw.PublicID,
  163. }
  164. dests = append(dests, []any{&resourceRaw.CreatedTs, &resourceRaw.UpdatedTs}...)
  165. if err := tx.QueryRowContext(ctx, query, values...).Scan(dests...); err != nil {
  166. return nil, FormatError(err)
  167. }
  168. return &resourceRaw, nil
  169. }
  170. func patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.ResourcePatch) (*resourceRaw, error) {
  171. set, args := []string{}, []any{}
  172. if v := patch.UpdatedTs; v != nil {
  173. set, args = append(set, "updated_ts = ?"), append(args, *v)
  174. }
  175. if v := patch.Filename; v != nil {
  176. set, args = append(set, "filename = ?"), append(args, *v)
  177. }
  178. if v := patch.PublicID; v != nil {
  179. set, args = append(set, "public_id = ?"), append(args, *v)
  180. }
  181. args = append(args, patch.ID)
  182. fields := []string{"id", "filename", "external_link", "type", "size", "creator_id", "created_ts", "updated_ts", "internal_path", "public_id"}
  183. query := `
  184. UPDATE resource
  185. SET ` + strings.Join(set, ", ") + `
  186. WHERE id = ?
  187. RETURNING ` + strings.Join(fields, ", ")
  188. var resourceRaw resourceRaw
  189. dests := []any{
  190. &resourceRaw.ID,
  191. &resourceRaw.Filename,
  192. &resourceRaw.ExternalLink,
  193. &resourceRaw.Type,
  194. &resourceRaw.Size,
  195. &resourceRaw.CreatorID,
  196. &resourceRaw.CreatedTs,
  197. &resourceRaw.UpdatedTs,
  198. &resourceRaw.InternalPath,
  199. &resourceRaw.PublicID,
  200. }
  201. if err := tx.QueryRowContext(ctx, query, args...).Scan(dests...); err != nil {
  202. return nil, FormatError(err)
  203. }
  204. return &resourceRaw, nil
  205. }
  206. func findResourceListImpl(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) ([]*resourceRaw, error) {
  207. where, args := []string{"1 = 1"}, []any{}
  208. if v := find.ID; v != nil {
  209. where, args = append(where, "resource.id = ?"), append(args, *v)
  210. }
  211. if v := find.CreatorID; v != nil {
  212. where, args = append(where, "resource.creator_id = ?"), append(args, *v)
  213. }
  214. if v := find.Filename; v != nil {
  215. where, args = append(where, "resource.filename = ?"), append(args, *v)
  216. }
  217. if v := find.MemoID; v != nil {
  218. where, args = append(where, "resource.id in (SELECT resource_id FROM memo_resource WHERE memo_id = ?)"), append(args, *v)
  219. }
  220. if v := find.PublicID; v != nil {
  221. where, args = append(where, "resource.public_id = ?"), append(args, *v)
  222. }
  223. fields := []string{"resource.id", "resource.filename", "resource.external_link", "resource.type", "resource.size", "resource.creator_id", "resource.created_ts", "resource.updated_ts", "internal_path", "public_id"}
  224. if find.GetBlob {
  225. fields = append(fields, "resource.blob")
  226. }
  227. query := fmt.Sprintf(`
  228. SELECT
  229. COUNT(DISTINCT memo_resource.memo_id) AS linked_memo_amount,
  230. %s
  231. FROM resource
  232. LEFT JOIN memo_resource ON resource.id = memo_resource.resource_id
  233. WHERE %s
  234. GROUP BY resource.id
  235. ORDER BY resource.id DESC
  236. `, strings.Join(fields, ", "), strings.Join(where, " AND "))
  237. if find.Limit != nil {
  238. query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
  239. if find.Offset != nil {
  240. query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
  241. }
  242. }
  243. rows, err := tx.QueryContext(ctx, query, args...)
  244. if err != nil {
  245. return nil, FormatError(err)
  246. }
  247. defer rows.Close()
  248. resourceRawList := make([]*resourceRaw, 0)
  249. for rows.Next() {
  250. var resourceRaw resourceRaw
  251. dests := []any{
  252. &resourceRaw.LinkedMemoAmount,
  253. &resourceRaw.ID,
  254. &resourceRaw.Filename,
  255. &resourceRaw.ExternalLink,
  256. &resourceRaw.Type,
  257. &resourceRaw.Size,
  258. &resourceRaw.CreatorID,
  259. &resourceRaw.CreatedTs,
  260. &resourceRaw.UpdatedTs,
  261. &resourceRaw.InternalPath,
  262. &resourceRaw.PublicID,
  263. }
  264. if find.GetBlob {
  265. dests = append(dests, &resourceRaw.Blob)
  266. }
  267. if err := rows.Scan(dests...); err != nil {
  268. return nil, FormatError(err)
  269. }
  270. resourceRawList = append(resourceRawList, &resourceRaw)
  271. }
  272. if err := rows.Err(); err != nil {
  273. return nil, FormatError(err)
  274. }
  275. return resourceRawList, nil
  276. }
  277. func deleteResource(ctx context.Context, tx *sql.Tx, delete *api.ResourceDelete) error {
  278. where, args := []string{"id = ?"}, []any{delete.ID}
  279. stmt := `DELETE FROM resource WHERE ` + strings.Join(where, " AND ")
  280. result, err := tx.ExecContext(ctx, stmt, args...)
  281. if err != nil {
  282. return FormatError(err)
  283. }
  284. rows, _ := result.RowsAffected()
  285. if rows == 0 {
  286. return &common.Error{Code: common.NotFound, Err: fmt.Errorf("resource not found")}
  287. }
  288. return nil
  289. }
  290. func vacuumResource(ctx context.Context, tx *sql.Tx) error {
  291. stmt := `
  292. DELETE FROM
  293. resource
  294. WHERE
  295. creator_id NOT IN (
  296. SELECT
  297. id
  298. FROM
  299. user
  300. )`
  301. _, err := tx.ExecContext(ctx, stmt)
  302. if err != nil {
  303. return FormatError(err)
  304. }
  305. return nil
  306. }