media assets: include file URLs in /media_assets.json API.

Include information about the asset's variants (sample images) in the /media_assets.json API:

    {
      "id": 6410907,
      "created_at": "2022-07-31T15:44:34.522-04:00",
      "updated_at": "2022-07-31T15:44:38.002-04:00",
      "md5": "19a2be6a1a8582bb349de9734b7a649a",
      "file_ext": "jpg",
      "file_size": 369029,
      "image_width": 600,
      "image_height": 900,
      "duration": null,
      "status": "active",
      "file_key": "R4DBCxBID",
      "is_public": true,
      "variants": [
         {
           "variant": "preview",
           "url": "https://cdn.donmai.us/preview/19/a2/19a2be6a1a8582bb349de9734b7a649a.jpg",
           "width": 100,
           "height": 150,
           "file_ext": "jpg"
         },
         {
           "variant": "180x180",
           "url": "https://cdn.donmai.us/180x180/19/a2/19a2be6a1a8582bb349de9734b7a649a.jpg",
           "width": 120,
           "height": 180,
           "file_ext": "jpg"
         },
         {
           "variant": "360x360",
           "url": "https://cdn.donmai.us/360x360/19/a2/19a2be6a1a8582bb349de9734b7a649a.jpg",
           "width": 240,
           "height": 360,
           "file_ext": "jpg"
         },
         {
           "variant": "720x720",
           "url": "https://cdn.donmai.us/720x720/19/a2/19a2be6a1a8582bb349de9734b7a649a.webp",
           "width": 480,
           "height": 720,
           "file_ext": "webp"
         },
         {
           "variant": "original",
           "url": "https://cdn.donmai.us/original/19/a2/19a2be6a1a8582bb349de9734b7a649a.jpg",
           "width": 600,
           "height": 900,
           "file_ext": "jpg"
         }
      ]
    }
This commit is contained in:
evazion
2022-10-16 17:16:28 -05:00
parent 3b0e94040f
commit 16e74650e8
2 changed files with 9 additions and 5 deletions

View File

@@ -51,6 +51,8 @@ class MediaAsset < ApplicationRecord
class Variant
extend Memoist
include ActiveModel::Serializers::JSON
include ActiveModel::Serializers::Xml
attr_reader :media_asset, :variant
delegate :md5, :storage_service, :backup_storage_service, to: :media_asset
@@ -164,6 +166,10 @@ class MediaAsset < ApplicationRecord
dimensions[1]
end
def serializable_hash(*options)
{ variant: variant, url: file_url, width: width, height: height, file_ext: file_ext }
end
memoize :file_name, :file_ext, :max_dimensions, :dimensions
end

View File

@@ -10,10 +10,8 @@ class MediaAssetPolicy < ApplicationPolicy
end
def api_attributes
if can_see_image?
super
else
super.excluding(:md5, :file_key)
end
attributes = super + [:variants]
attributes -= [:md5, :file_key, :variants] if !can_see_image?
attributes
end
end