media_attachment_serializer.rb 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # frozen_string_literal: true
  2. class REST::MediaAttachmentSerializer < ActiveModel::Serializer
  3. include RoutingHelper
  4. attributes :id, :type, :url, :preview_url,
  5. :remote_url, :preview_remote_url, :text_url, :meta,
  6. :description, :blurhash
  7. def id
  8. object.id.to_s
  9. end
  10. def url
  11. if object.not_processed?
  12. nil
  13. elsif object.needs_redownload?
  14. media_proxy_url(object.id, :original)
  15. else
  16. full_asset_url(object.file.url(:original))
  17. end
  18. end
  19. def remote_url
  20. object.remote_url.presence
  21. end
  22. def preview_url
  23. if object.needs_redownload?
  24. media_proxy_url(object.id, :small)
  25. elsif object.thumbnail.present?
  26. full_asset_url(object.thumbnail.url(:original))
  27. elsif object.file.styles.key?(:small)
  28. full_asset_url(object.file.url(:small))
  29. end
  30. end
  31. def preview_remote_url
  32. object.thumbnail_remote_url.presence
  33. end
  34. def text_url
  35. object.local? ? medium_url(object) : nil
  36. end
  37. def meta
  38. object.file.meta
  39. end
  40. end