media_repository.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 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 .upload_resource import UploadResource
  16. from .download_resource import DownloadResource
  17. from .thumbnail_resource import ThumbnailResource
  18. from .identicon_resource import IdenticonResource
  19. from .preview_url_resource import PreviewUrlResource
  20. from .filepath import MediaFilePaths
  21. from twisted.web.resource import Resource
  22. import logging
  23. logger = logging.getLogger(__name__)
  24. class MediaRepositoryResource(Resource):
  25. """File uploading and downloading.
  26. Uploads are POSTed to a resource which returns a token which is used to GET
  27. the download::
  28. => POST /_matrix/media/v1/upload HTTP/1.1
  29. Content-Type: <media-type>
  30. Content-Length: <content-length>
  31. <media>
  32. <= HTTP/1.1 200 OK
  33. Content-Type: application/json
  34. { "content_uri": "mxc://<server-name>/<media-id>" }
  35. => GET /_matrix/media/v1/download/<server-name>/<media-id> HTTP/1.1
  36. <= HTTP/1.1 200 OK
  37. Content-Type: <media-type>
  38. Content-Disposition: attachment;filename=<upload-filename>
  39. <media>
  40. Clients can get thumbnails by supplying a desired width and height and
  41. thumbnailing method::
  42. => GET /_matrix/media/v1/thumbnail/<server_name>
  43. /<media-id>?width=<w>&height=<h>&method=<m> HTTP/1.1
  44. <= HTTP/1.1 200 OK
  45. Content-Type: image/jpeg or image/png
  46. <thumbnail>
  47. The thumbnail methods are "crop" and "scale". "scale" trys to return an
  48. image where either the width or the height is smaller than the requested
  49. size. The client should then scale and letterbox the image if it needs to
  50. fit within a given rectangle. "crop" trys to return an image where the
  51. width and height are close to the requested size and the aspect matches
  52. the requested size. The client should scale the image if it needs to fit
  53. within a given rectangle.
  54. """
  55. def __init__(self, hs):
  56. Resource.__init__(self)
  57. filepaths = MediaFilePaths(hs.config.media_store_path)
  58. self.putChild("upload", UploadResource(hs, filepaths))
  59. self.putChild("download", DownloadResource(hs, filepaths))
  60. self.putChild("thumbnail", ThumbnailResource(hs, filepaths))
  61. self.putChild("identicon", IdenticonResource())
  62. if hs.config.url_preview_enabled:
  63. try:
  64. self.putChild("preview_url", PreviewUrlResource(hs, filepaths))
  65. except Exception as e:
  66. logger.warn("Failed to mount preview_url")
  67. logger.exception(e)