media_repository.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from _base import SQLBaseStore
  16. class MediaRepositoryStore(SQLBaseStore):
  17. """Persistence for attachments and avatars"""
  18. def get_default_thumbnails(self, top_level_type, sub_type):
  19. return []
  20. def get_local_media(self, media_id):
  21. """Get the metadata for a local piece of media
  22. Returns:
  23. None if the meia_id doesn't exist.
  24. """
  25. return self._simple_select_one(
  26. "local_media_repository",
  27. {"media_id": media_id},
  28. ("media_type", "media_length", "upload_name", "created_ts"),
  29. allow_none=True,
  30. )
  31. def store_local_media(self, media_id, media_type, time_now_ms, upload_name,
  32. media_length, user_id):
  33. return self._simple_insert(
  34. "local_media_repository",
  35. {
  36. "media_id": media_id,
  37. "media_type": media_type,
  38. "created_ts": time_now_ms,
  39. "upload_name": upload_name,
  40. "media_length": media_length,
  41. "user_id": user_id.to_string(),
  42. }
  43. )
  44. def get_local_media_thumbnails(self, media_id):
  45. return self._simple_select_list(
  46. "local_media_repository_thumbnails",
  47. {"media_id": media_id},
  48. (
  49. "thumbnail_width", "thumbnail_height", "thumbnail_method",
  50. "thumbnail_type", "thumbnail_length",
  51. )
  52. )
  53. def store_local_thumbnail(self, media_id, thumbnail_width,
  54. thumbnail_height, thumbnail_type,
  55. thumbnail_method, thumbnail_length):
  56. return self._simple_insert(
  57. "local_media_repository_thumbnails",
  58. {
  59. "media_id": media_id,
  60. "thumbnail_width": thumbnail_width,
  61. "thumbnail_height": thumbnail_height,
  62. "thumbnail_method": thumbnail_method,
  63. "thumbnail_type": thumbnail_type,
  64. "thumbnail_length": thumbnail_length,
  65. }
  66. )
  67. def get_cached_remote_media(self, origin, media_id):
  68. return self._simple_select_one(
  69. "remote_media_cache",
  70. {"media_origin": origin, "media_id": media_id},
  71. (
  72. "media_type", "media_length", "upload_name", "created_ts",
  73. "filesystem_id",
  74. ),
  75. allow_none=True,
  76. )
  77. def store_cached_remote_media(self, origin, media_id, media_type,
  78. media_length, time_now_ms, upload_name,
  79. filesystem_id):
  80. return self._simple_insert(
  81. "remote_media_cache",
  82. {
  83. "media_origin": origin,
  84. "media_id": media_id,
  85. "media_type": media_type,
  86. "media_length": media_length,
  87. "created_ts": time_now_ms,
  88. "upload_name": upload_name,
  89. "filesystem_id": filesystem_id,
  90. }
  91. )
  92. def get_remote_media_thumbnails(self, origin, media_id):
  93. return self._simple_select_list(
  94. "remote_media_cache_thumbnails",
  95. {"media_origin": origin, "media_id": media_id},
  96. (
  97. "thumbnail_width", "thumbnail_height", "thumbnail_method",
  98. "thumbnail_type", "thumbnail_length", "filesystem_id",
  99. )
  100. )
  101. def store_remote_media_thumbnail(self, origin, media_id, filesystem_id,
  102. thumbnail_width, thumbnail_height,
  103. thumbnail_type, thumbnail_method,
  104. thumbnail_length):
  105. return self._simple_insert(
  106. "remote_media_cache_thumbnails",
  107. {
  108. "media_origin": origin,
  109. "media_id": media_id,
  110. "thumbnail_width": thumbnail_width,
  111. "thumbnail_height": thumbnail_height,
  112. "thumbnail_method": thumbnail_method,
  113. "thumbnail_type": thumbnail_type,
  114. "thumbnail_length": thumbnail_length,
  115. "filesystem_id": filesystem_id,
  116. }
  117. )