test_filepath.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from synapse.rest.media.v1.filepath import MediaFilePaths
  15. from tests import unittest
  16. class MediaFilePathsTestCase(unittest.TestCase):
  17. def setUp(self):
  18. super().setUp()
  19. self.filepaths = MediaFilePaths("/media_store")
  20. def test_local_media_filepath(self):
  21. """Test local media paths"""
  22. self.assertEqual(
  23. self.filepaths.local_media_filepath_rel("GerZNDnDZVjsOtardLuwfIBg"),
  24. "local_content/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  25. )
  26. self.assertEqual(
  27. self.filepaths.local_media_filepath("GerZNDnDZVjsOtardLuwfIBg"),
  28. "/media_store/local_content/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  29. )
  30. def test_local_media_thumbnail(self):
  31. """Test local media thumbnail paths"""
  32. self.assertEqual(
  33. self.filepaths.local_media_thumbnail_rel(
  34. "GerZNDnDZVjsOtardLuwfIBg", 800, 600, "image/jpeg", "scale"
  35. ),
  36. "local_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale",
  37. )
  38. self.assertEqual(
  39. self.filepaths.local_media_thumbnail(
  40. "GerZNDnDZVjsOtardLuwfIBg", 800, 600, "image/jpeg", "scale"
  41. ),
  42. "/media_store/local_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale",
  43. )
  44. def test_local_media_thumbnail_dir(self):
  45. """Test local media thumbnail directory paths"""
  46. self.assertEqual(
  47. self.filepaths.local_media_thumbnail_dir("GerZNDnDZVjsOtardLuwfIBg"),
  48. "/media_store/local_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  49. )
  50. def test_remote_media_filepath(self):
  51. """Test remote media paths"""
  52. self.assertEqual(
  53. self.filepaths.remote_media_filepath_rel(
  54. "example.com", "GerZNDnDZVjsOtardLuwfIBg"
  55. ),
  56. "remote_content/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  57. )
  58. self.assertEqual(
  59. self.filepaths.remote_media_filepath(
  60. "example.com", "GerZNDnDZVjsOtardLuwfIBg"
  61. ),
  62. "/media_store/remote_content/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  63. )
  64. def test_remote_media_thumbnail(self):
  65. """Test remote media thumbnail paths"""
  66. self.assertEqual(
  67. self.filepaths.remote_media_thumbnail_rel(
  68. "example.com",
  69. "GerZNDnDZVjsOtardLuwfIBg",
  70. 800,
  71. 600,
  72. "image/jpeg",
  73. "scale",
  74. ),
  75. "remote_thumbnail/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale",
  76. )
  77. self.assertEqual(
  78. self.filepaths.remote_media_thumbnail(
  79. "example.com",
  80. "GerZNDnDZVjsOtardLuwfIBg",
  81. 800,
  82. 600,
  83. "image/jpeg",
  84. "scale",
  85. ),
  86. "/media_store/remote_thumbnail/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale",
  87. )
  88. def test_remote_media_thumbnail_legacy(self):
  89. """Test old-style remote media thumbnail paths"""
  90. self.assertEqual(
  91. self.filepaths.remote_media_thumbnail_rel_legacy(
  92. "example.com", "GerZNDnDZVjsOtardLuwfIBg", 800, 600, "image/jpeg"
  93. ),
  94. "remote_thumbnail/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg",
  95. )
  96. def test_remote_media_thumbnail_dir(self):
  97. """Test remote media thumbnail directory paths"""
  98. self.assertEqual(
  99. self.filepaths.remote_media_thumbnail_dir(
  100. "example.com", "GerZNDnDZVjsOtardLuwfIBg"
  101. ),
  102. "/media_store/remote_thumbnail/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  103. )
  104. def test_url_cache_filepath(self):
  105. """Test URL cache paths"""
  106. self.assertEqual(
  107. self.filepaths.url_cache_filepath_rel("2020-01-02_GerZNDnDZVjsOtar"),
  108. "url_cache/2020-01-02/GerZNDnDZVjsOtar",
  109. )
  110. self.assertEqual(
  111. self.filepaths.url_cache_filepath("2020-01-02_GerZNDnDZVjsOtar"),
  112. "/media_store/url_cache/2020-01-02/GerZNDnDZVjsOtar",
  113. )
  114. def test_url_cache_filepath_legacy(self):
  115. """Test old-style URL cache paths"""
  116. self.assertEqual(
  117. self.filepaths.url_cache_filepath_rel("GerZNDnDZVjsOtardLuwfIBg"),
  118. "url_cache/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  119. )
  120. self.assertEqual(
  121. self.filepaths.url_cache_filepath("GerZNDnDZVjsOtardLuwfIBg"),
  122. "/media_store/url_cache/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  123. )
  124. def test_url_cache_filepath_dirs_to_delete(self):
  125. """Test URL cache cleanup paths"""
  126. self.assertEqual(
  127. self.filepaths.url_cache_filepath_dirs_to_delete(
  128. "2020-01-02_GerZNDnDZVjsOtar"
  129. ),
  130. ["/media_store/url_cache/2020-01-02"],
  131. )
  132. def test_url_cache_filepath_dirs_to_delete_legacy(self):
  133. """Test old-style URL cache cleanup paths"""
  134. self.assertEqual(
  135. self.filepaths.url_cache_filepath_dirs_to_delete(
  136. "GerZNDnDZVjsOtardLuwfIBg"
  137. ),
  138. [
  139. "/media_store/url_cache/Ge/rZ",
  140. "/media_store/url_cache/Ge",
  141. ],
  142. )
  143. def test_url_cache_thumbnail(self):
  144. """Test URL cache thumbnail paths"""
  145. self.assertEqual(
  146. self.filepaths.url_cache_thumbnail_rel(
  147. "2020-01-02_GerZNDnDZVjsOtar", 800, 600, "image/jpeg", "scale"
  148. ),
  149. "url_cache_thumbnails/2020-01-02/GerZNDnDZVjsOtar/800-600-image-jpeg-scale",
  150. )
  151. self.assertEqual(
  152. self.filepaths.url_cache_thumbnail(
  153. "2020-01-02_GerZNDnDZVjsOtar", 800, 600, "image/jpeg", "scale"
  154. ),
  155. "/media_store/url_cache_thumbnails/2020-01-02/GerZNDnDZVjsOtar/800-600-image-jpeg-scale",
  156. )
  157. def test_url_cache_thumbnail_legacy(self):
  158. """Test old-style URL cache thumbnail paths"""
  159. self.assertEqual(
  160. self.filepaths.url_cache_thumbnail_rel(
  161. "GerZNDnDZVjsOtardLuwfIBg", 800, 600, "image/jpeg", "scale"
  162. ),
  163. "url_cache_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale",
  164. )
  165. self.assertEqual(
  166. self.filepaths.url_cache_thumbnail(
  167. "GerZNDnDZVjsOtardLuwfIBg", 800, 600, "image/jpeg", "scale"
  168. ),
  169. "/media_store/url_cache_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale",
  170. )
  171. def test_url_cache_thumbnail_directory(self):
  172. """Test URL cache thumbnail directory paths"""
  173. self.assertEqual(
  174. self.filepaths.url_cache_thumbnail_directory_rel(
  175. "2020-01-02_GerZNDnDZVjsOtar"
  176. ),
  177. "url_cache_thumbnails/2020-01-02/GerZNDnDZVjsOtar",
  178. )
  179. self.assertEqual(
  180. self.filepaths.url_cache_thumbnail_directory("2020-01-02_GerZNDnDZVjsOtar"),
  181. "/media_store/url_cache_thumbnails/2020-01-02/GerZNDnDZVjsOtar",
  182. )
  183. def test_url_cache_thumbnail_directory_legacy(self):
  184. """Test old-style URL cache thumbnail directory paths"""
  185. self.assertEqual(
  186. self.filepaths.url_cache_thumbnail_directory_rel(
  187. "GerZNDnDZVjsOtardLuwfIBg"
  188. ),
  189. "url_cache_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  190. )
  191. self.assertEqual(
  192. self.filepaths.url_cache_thumbnail_directory("GerZNDnDZVjsOtardLuwfIBg"),
  193. "/media_store/url_cache_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  194. )
  195. def test_url_cache_thumbnail_dirs_to_delete(self):
  196. """Test URL cache thumbnail cleanup paths"""
  197. self.assertEqual(
  198. self.filepaths.url_cache_thumbnail_dirs_to_delete(
  199. "2020-01-02_GerZNDnDZVjsOtar"
  200. ),
  201. [
  202. "/media_store/url_cache_thumbnails/2020-01-02/GerZNDnDZVjsOtar",
  203. "/media_store/url_cache_thumbnails/2020-01-02",
  204. ],
  205. )
  206. def test_url_cache_thumbnail_dirs_to_delete_legacy(self):
  207. """Test old-style URL cache thumbnail cleanup paths"""
  208. self.assertEqual(
  209. self.filepaths.url_cache_thumbnail_dirs_to_delete(
  210. "GerZNDnDZVjsOtardLuwfIBg"
  211. ),
  212. [
  213. "/media_store/url_cache_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  214. "/media_store/url_cache_thumbnails/Ge/rZ",
  215. "/media_store/url_cache_thumbnails/Ge",
  216. ],
  217. )