|
| 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 | + |
| 16 | +from tests import unittest |
| 17 | + |
| 18 | + |
| 19 | +class MediaFilePathsTestCase(unittest.TestCase): |
| 20 | + def setUp(self): |
| 21 | + super().setUp() |
| 22 | + |
| 23 | + self.filepaths = MediaFilePaths("/media_store") |
| 24 | + |
| 25 | + def test_local_media_filepath(self): |
| 26 | + """Test local media paths""" |
| 27 | + self.assertEqual( |
| 28 | + self.filepaths.local_media_filepath_rel("GerZNDnDZVjsOtardLuwfIBg"), |
| 29 | + "local_content/Ge/rZ/NDnDZVjsOtardLuwfIBg", |
| 30 | + ) |
| 31 | + self.assertEqual( |
| 32 | + self.filepaths.local_media_filepath("GerZNDnDZVjsOtardLuwfIBg"), |
| 33 | + "/media_store/local_content/Ge/rZ/NDnDZVjsOtardLuwfIBg", |
| 34 | + ) |
| 35 | + |
| 36 | + def test_local_media_thumbnail(self): |
| 37 | + """Test local media thumbnail paths""" |
| 38 | + self.assertEqual( |
| 39 | + self.filepaths.local_media_thumbnail_rel( |
| 40 | + "GerZNDnDZVjsOtardLuwfIBg", 800, 600, "image/jpeg", "scale" |
| 41 | + ), |
| 42 | + "local_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale", |
| 43 | + ) |
| 44 | + self.assertEqual( |
| 45 | + self.filepaths.local_media_thumbnail( |
| 46 | + "GerZNDnDZVjsOtardLuwfIBg", 800, 600, "image/jpeg", "scale" |
| 47 | + ), |
| 48 | + "/media_store/local_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale", |
| 49 | + ) |
| 50 | + |
| 51 | + def test_local_media_thumbnail_dir(self): |
| 52 | + """Test local media thumbnail directory paths""" |
| 53 | + self.assertEqual( |
| 54 | + self.filepaths.local_media_thumbnail_dir("GerZNDnDZVjsOtardLuwfIBg"), |
| 55 | + "/media_store/local_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg", |
| 56 | + ) |
| 57 | + |
| 58 | + def test_remote_media_filepath(self): |
| 59 | + """Test remote media paths""" |
| 60 | + self.assertEqual( |
| 61 | + self.filepaths.remote_media_filepath_rel( |
| 62 | + "example.com", "GerZNDnDZVjsOtardLuwfIBg" |
| 63 | + ), |
| 64 | + "remote_content/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg", |
| 65 | + ) |
| 66 | + self.assertEqual( |
| 67 | + self.filepaths.remote_media_filepath( |
| 68 | + "example.com", "GerZNDnDZVjsOtardLuwfIBg" |
| 69 | + ), |
| 70 | + "/media_store/remote_content/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg", |
| 71 | + ) |
| 72 | + |
| 73 | + def test_remote_media_thumbnail(self): |
| 74 | + """Test remote media thumbnail paths""" |
| 75 | + self.assertEqual( |
| 76 | + self.filepaths.remote_media_thumbnail_rel( |
| 77 | + "example.com", |
| 78 | + "GerZNDnDZVjsOtardLuwfIBg", |
| 79 | + 800, |
| 80 | + 600, |
| 81 | + "image/jpeg", |
| 82 | + "scale", |
| 83 | + ), |
| 84 | + "remote_thumbnail/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale", |
| 85 | + ) |
| 86 | + self.assertEqual( |
| 87 | + self.filepaths.remote_media_thumbnail( |
| 88 | + "example.com", |
| 89 | + "GerZNDnDZVjsOtardLuwfIBg", |
| 90 | + 800, |
| 91 | + 600, |
| 92 | + "image/jpeg", |
| 93 | + "scale", |
| 94 | + ), |
| 95 | + "/media_store/remote_thumbnail/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale", |
| 96 | + ) |
| 97 | + |
| 98 | + def test_remote_media_thumbnail_legacy(self): |
| 99 | + """Test old-style remote media thumbnail paths""" |
| 100 | + self.assertEqual( |
| 101 | + self.filepaths.remote_media_thumbnail_rel_legacy( |
| 102 | + "example.com", "GerZNDnDZVjsOtardLuwfIBg", 800, 600, "image/jpeg" |
| 103 | + ), |
| 104 | + "remote_thumbnail/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg", |
| 105 | + ) |
| 106 | + |
| 107 | + def test_remote_media_thumbnail_dir(self): |
| 108 | + """Test remote media thumbnail directory paths""" |
| 109 | + self.assertEqual( |
| 110 | + self.filepaths.remote_media_thumbnail_dir( |
| 111 | + "example.com", "GerZNDnDZVjsOtardLuwfIBg" |
| 112 | + ), |
| 113 | + "/media_store/remote_thumbnail/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg", |
| 114 | + ) |
| 115 | + |
| 116 | + def test_url_cache_filepath(self): |
| 117 | + """Test URL cache paths""" |
| 118 | + self.assertEqual( |
| 119 | + self.filepaths.url_cache_filepath_rel("2020-01-02_GerZNDnDZVjsOtar"), |
| 120 | + "url_cache/2020-01-02/GerZNDnDZVjsOtar", |
| 121 | + ) |
| 122 | + self.assertEqual( |
| 123 | + self.filepaths.url_cache_filepath("2020-01-02_GerZNDnDZVjsOtar"), |
| 124 | + "/media_store/url_cache/2020-01-02/GerZNDnDZVjsOtar", |
| 125 | + ) |
| 126 | + |
| 127 | + def test_url_cache_filepath_legacy(self): |
| 128 | + """Test old-style URL cache paths""" |
| 129 | + self.assertEqual( |
| 130 | + self.filepaths.url_cache_filepath_rel("GerZNDnDZVjsOtardLuwfIBg"), |
| 131 | + "url_cache/Ge/rZ/NDnDZVjsOtardLuwfIBg", |
| 132 | + ) |
| 133 | + self.assertEqual( |
| 134 | + self.filepaths.url_cache_filepath("GerZNDnDZVjsOtardLuwfIBg"), |
| 135 | + "/media_store/url_cache/Ge/rZ/NDnDZVjsOtardLuwfIBg", |
| 136 | + ) |
| 137 | + |
| 138 | + def test_url_cache_filepath_dirs_to_delete(self): |
| 139 | + """Test URL cache cleanup paths""" |
| 140 | + self.assertEqual( |
| 141 | + self.filepaths.url_cache_filepath_dirs_to_delete( |
| 142 | + "2020-01-02_GerZNDnDZVjsOtar" |
| 143 | + ), |
| 144 | + ["/media_store/url_cache/2020-01-02"], |
| 145 | + ) |
| 146 | + |
| 147 | + def test_url_cache_filepath_dirs_to_delete_legacy(self): |
| 148 | + """Test old-style URL cache cleanup paths""" |
| 149 | + self.assertEqual( |
| 150 | + self.filepaths.url_cache_filepath_dirs_to_delete( |
| 151 | + "GerZNDnDZVjsOtardLuwfIBg" |
| 152 | + ), |
| 153 | + [ |
| 154 | + "/media_store/url_cache/Ge/rZ", |
| 155 | + "/media_store/url_cache/Ge", |
| 156 | + ], |
| 157 | + ) |
| 158 | + |
| 159 | + def test_url_cache_thumbnail(self): |
| 160 | + """Test URL cache thumbnail paths""" |
| 161 | + self.assertEqual( |
| 162 | + self.filepaths.url_cache_thumbnail_rel( |
| 163 | + "2020-01-02_GerZNDnDZVjsOtar", 800, 600, "image/jpeg", "scale" |
| 164 | + ), |
| 165 | + "url_cache_thumbnails/2020-01-02/GerZNDnDZVjsOtar/800-600-image-jpeg-scale", |
| 166 | + ) |
| 167 | + self.assertEqual( |
| 168 | + self.filepaths.url_cache_thumbnail( |
| 169 | + "2020-01-02_GerZNDnDZVjsOtar", 800, 600, "image/jpeg", "scale" |
| 170 | + ), |
| 171 | + "/media_store/url_cache_thumbnails/2020-01-02/GerZNDnDZVjsOtar/800-600-image-jpeg-scale", |
| 172 | + ) |
| 173 | + |
| 174 | + def test_url_cache_thumbnail_legacy(self): |
| 175 | + """Test old-style URL cache thumbnail paths""" |
| 176 | + self.assertEqual( |
| 177 | + self.filepaths.url_cache_thumbnail_rel( |
| 178 | + "GerZNDnDZVjsOtardLuwfIBg", 800, 600, "image/jpeg", "scale" |
| 179 | + ), |
| 180 | + "url_cache_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale", |
| 181 | + ) |
| 182 | + self.assertEqual( |
| 183 | + self.filepaths.url_cache_thumbnail( |
| 184 | + "GerZNDnDZVjsOtardLuwfIBg", 800, 600, "image/jpeg", "scale" |
| 185 | + ), |
| 186 | + "/media_store/url_cache_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale", |
| 187 | + ) |
| 188 | + |
| 189 | + def test_url_cache_thumbnail_directory(self): |
| 190 | + """Test URL cache thumbnail directory paths""" |
| 191 | + self.assertEqual( |
| 192 | + self.filepaths.url_cache_thumbnail_directory_rel( |
| 193 | + "2020-01-02_GerZNDnDZVjsOtar" |
| 194 | + ), |
| 195 | + "url_cache_thumbnails/2020-01-02/GerZNDnDZVjsOtar", |
| 196 | + ) |
| 197 | + self.assertEqual( |
| 198 | + self.filepaths.url_cache_thumbnail_directory("2020-01-02_GerZNDnDZVjsOtar"), |
| 199 | + "/media_store/url_cache_thumbnails/2020-01-02/GerZNDnDZVjsOtar", |
| 200 | + ) |
| 201 | + |
| 202 | + def test_url_cache_thumbnail_directory_legacy(self): |
| 203 | + """Test old-style URL cache thumbnail directory paths""" |
| 204 | + self.assertEqual( |
| 205 | + self.filepaths.url_cache_thumbnail_directory_rel( |
| 206 | + "GerZNDnDZVjsOtardLuwfIBg" |
| 207 | + ), |
| 208 | + "url_cache_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg", |
| 209 | + ) |
| 210 | + self.assertEqual( |
| 211 | + self.filepaths.url_cache_thumbnail_directory("GerZNDnDZVjsOtardLuwfIBg"), |
| 212 | + "/media_store/url_cache_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg", |
| 213 | + ) |
| 214 | + |
| 215 | + def test_url_cache_thumbnail_dirs_to_delete(self): |
| 216 | + """Test URL cache thumbnail cleanup paths""" |
| 217 | + self.assertEqual( |
| 218 | + self.filepaths.url_cache_thumbnail_dirs_to_delete( |
| 219 | + "2020-01-02_GerZNDnDZVjsOtar" |
| 220 | + ), |
| 221 | + [ |
| 222 | + "/media_store/url_cache_thumbnails/2020-01-02/GerZNDnDZVjsOtar", |
| 223 | + "/media_store/url_cache_thumbnails/2020-01-02", |
| 224 | + ], |
| 225 | + ) |
| 226 | + |
| 227 | + def test_url_cache_thumbnail_dirs_to_delete_legacy(self): |
| 228 | + """Test old-style URL cache thumbnail cleanup paths""" |
| 229 | + self.assertEqual( |
| 230 | + self.filepaths.url_cache_thumbnail_dirs_to_delete( |
| 231 | + "GerZNDnDZVjsOtardLuwfIBg" |
| 232 | + ), |
| 233 | + [ |
| 234 | + "/media_store/url_cache_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg", |
| 235 | + "/media_store/url_cache_thumbnails/Ge/rZ", |
| 236 | + "/media_store/url_cache_thumbnails/Ge", |
| 237 | + ], |
| 238 | + ) |
0 commit comments