gotoemma / media-api

该包最新版本(3.2.0)没有提供许可证信息。

示例:Sonata MediaBundle 与 ApiPlatform 2.0 的集成,包括媒体上传以及在 GET 项目操作中公开媒体格式

安装次数: 2,739

依赖项: 0

建议者: 0

安全: 0

类型:项目

3.2.0 2021-04-28 12:13 UTC

This package is auto-updated.

Last update: 2024-09-28 19:20:45 UTC


README

示例:Sonata MediaBundle 与 ApiPlatform 2.0 的集成,包括媒体上传以及在 GET 项目操作中公开媒体格式

依赖项

  • Symfony 3.1
  • Sonata MediaBundle
  • Cweagans Composer Patches

配置

  • 将此存储库克隆到您的 symfony 实例的 src/ 文件夹中
  • 在不使用您的 AppBundle 的情况下创建一个 MediaElement 实体,并从 Gotoemma\MediaApiBundle\Entity\MediaElement 继承
  • 根据您的需求配置序列化组
  • 配置 Sonata MediaBundle 以使用您的 MediaElement 类
  • 添加 UploadAction 的路由配置

您的项目主 composer.json 文件

如果您已配置并启用了 Sonata Classification,则可以跳过此更改。Media Bundle 中仍然存在一些对 Sonata Classification Bundle 的硬依赖,但在当前的 GIT master 中大多数都已解决,但仍有少数留下。启用补丁,以应用我们提供的补丁以解决最后的依赖,并且在不使用 Classification Bundle 的情况下使用 MediaBundle。

{ 
  "extra": {
      "enable-patching": true
  }
}

app/config/config.yml

告诉 Sonata MediaBundle 从您的 Bundle 中使用哪个实体作为媒体元素。

sonata_media:
    class:
        media: YourBundle\Entity\MediaElement

app/config/routing.yml

加载 UploadAction 类中 __invoke() 方法上配置的注释路由的 upload 操作。

media_action:
    resource: '@MediaApiBundle/Action/'
    type:     'annotation'

src/YourBundle/Entity/MediaElement.php

设置您的媒体实体。如果您与 Api Platform 和序列化组一起使用,则必须在您的实体中定义从扩展类继承的所有属性,以定义序列化组。在此示例中,我们定义了已在扩展类中声明的 $formats 属性,以添加一些序列化组。

<?php

namespace YourBundle\Entity;

use Doctrine\ORM\Mapping AS ORM;
use Gotoemma\MediaApiBundle\Entity\MediaElement as BaseMediaElement;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Annotation\ApiResource;

/**
 * @ApiResource(
 * 		itemOperations={
 * 			"get"={"method"="GET"}
 * 		},
 * 		collectionOperations={
 * 			"get"={"method"="GET"}
 *	 	}
 * )
 * @ORM\Entity
 * @ORM\Table(name="MediaElement")
 */
class MediaElement extends BaseMediaElement
{
	/**
	 * @ORM\Id
	 * @ORM\Column(type="guid")
	 * @ORM\GeneratedValue(strategy="UUID")
	 */
	protected $id;

    /**
     * @var string
     * @Groups({"api_mediaelement_get_item", "api_article_get_item", "api_article_get_collection"})
     */
    protected $formats;
}

示例

POST 请求

POST /api/media_elements HTTP/1.1
Host: api.devloc.site
Content-Type: application/json

{
	"fileName": "testbild-83.png",
	"data": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABaQAAASMCAYAAAB3UopLAAAMF2lDQ1BJQ0MgUHJvZmlsZQAASImVlwdUU0kXx+eVFEJCC0RASuhNkF6l9yIgHWyEJEAoIQSCih1ZVHAtqFiwoisgCq4FkLUiioVFsPcNKirKuliwofJNEkD"
}

GET 请求

此 GET 项目操作完全由 Api Platform 提供。Media Api Bundle 不处理此操作,我们仅拦截 postLoad 事件以在您的媒体实体中设置 formats 属性。

GET /api/media_elements/f71e2f38-9230-11e6-b60a-ca5a65ec716d HTTP/1.1
Host: api.devloc.site

响应

{
  "@context": "/api/contexts/MediaElement",
  "@id": "/api/media_elements/f71e2f38-9230-11e6-b60a-ca5a65ec716d",
  "@type": "MediaElement",
  "formats": [
    {
      "alt": "testbild-83.png",
      "title": "testbild-83.png",
      "src": "https://api.devloc.site/uploads/media/default/0001/01/thumb_f71e2f38-9230-11e6-b60a-ca5a65ec716d_default_small.png",
      "width": 79,
      "height": 64,
      "context": "default",
      "format": "small"
    },
    {
      "alt": "testbild-83.png",
      "title": "testbild-83.png",
      "src": "https://api.devloc.site/uploads/media/default/0001/01/thumb_f71e2f38-9230-11e6-b60a-ca5a65ec716d_default_medium.png",
      "width": 200,
      "height": 161,
      "context": "default",
      "format": "medium"
    },
    {
      "alt": "testbild-83.png",
      "title": "testbild-83.png",
      "src": "https://api.devloc.site/uploads/media/default/0001/01/thumb_f71e2f38-9230-11e6-b60a-ca5a65ec716d_default_large.png",
      "width": 799,
      "height": 644,
      "context": "default",
      "format": "large"
    }
  ]
}