filepath.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. import os
  16. class MediaFilePaths(object):
  17. def __init__(self, base_path):
  18. self.base_path = base_path
  19. def default_thumbnail(self, default_top_level, default_sub_type, width,
  20. height, content_type, method):
  21. top_level_type, sub_type = content_type.split("/")
  22. file_name = "%i-%i-%s-%s-%s" % (
  23. width, height, top_level_type, sub_type, method
  24. )
  25. return os.path.join(
  26. self.base_path, "default_thumbnails", default_top_level,
  27. default_sub_type, file_name
  28. )
  29. def local_media_filepath(self, media_id):
  30. return os.path.join(
  31. self.base_path, "local_content",
  32. media_id[0:2], media_id[2:4], media_id[4:]
  33. )
  34. def local_media_thumbnail(self, media_id, width, height, content_type,
  35. method):
  36. top_level_type, sub_type = content_type.split("/")
  37. file_name = "%i-%i-%s-%s-%s" % (
  38. width, height, top_level_type, sub_type, method
  39. )
  40. return os.path.join(
  41. self.base_path, "local_thumbnails",
  42. media_id[0:2], media_id[2:4], media_id[4:],
  43. file_name
  44. )
  45. def remote_media_filepath(self, server_name, file_id):
  46. return os.path.join(
  47. self.base_path, "remote_content", server_name,
  48. file_id[0:2], file_id[2:4], file_id[4:]
  49. )
  50. def remote_media_thumbnail(self, server_name, file_id, width, height,
  51. content_type, method):
  52. top_level_type, sub_type = content_type.split("/")
  53. file_name = "%i-%i-%s-%s" % (width, height, top_level_type, sub_type)
  54. return os.path.join(
  55. self.base_path, "remote_thumbnail", server_name,
  56. file_id[0:2], file_id[2:4], file_id[4:],
  57. file_name
  58. )