定制 gotoemma/media-api 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gotoemma/media-api

Composer 安装命令:

composer create-project gotoemma/media-api

包简介

Example for integration of Sonata MediaBundle with ApiPlatform 2.0 including media upload and exposing media formats in GET item operation

README 文档

README

Example for integration of Sonata MediaBundle with ApiPlatform 2.0 including media upload and exposing media formats in GET item operation

Dependencies

  • Symfony 3.1
  • Sonata MediaBundle
  • Cweagans Composer Patches

Setup

  • Clone this repository to your src/ folder of your symfony instance
  • Create a MediaElement entity without your AppBundle and extend from Gotoemma\MediaApiBundle\Entity\MediaElement
  • Configure serialization groups according to your needs
  • Configure Sonata MediaBundle to use your MediaElement class
  • Add routing configuration for UploadAction

Your projects main composer.json

If you have Sonata Classification configured and enabled, you can skip this change. There are some hard dependencies to Sonata Classification Bundle in Media Bundle yet, most of them are resolved in current GIT master, but some are left. Enable patches, to apply the patch we provide to resolve the last dependency, and to use MediaBundle without Classification Bundle.

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

app/config/config.yml

Tell Sonata MediaBundle which Entity to use from your Bundle for Media elements.

sonata_media:
    class:
        media: YourBundle\Entity\MediaElement

app/config/routing.yml

Load the routing for upload action that is configured in UploadAction class as annotation on __invoke() method.

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

src/YourBundle/Entity/MediaElement.php

Setup your Media entitiy. If you are using this with Api Platform and Serialization Groups, you will have to define all inherited properties from extended class in your entity, to define serialization groups. In this example we define $formats property which is already declared in extended class, to add some serialization groups.

<?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;
}

Example

POST Request

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 Request

This GET item operation is fully served by Api Platform. Media Api Bundle is not taking care of it, we only intercept postLoad event to set formats property in your Media entity.

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

Response

{
  "@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"
    }
  ]
}

gotoemma/media-api 适用场景与选型建议

gotoemma/media-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.87k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 12 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 gotoemma/media-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gotoemma/media-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 gotoemma/media-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.87k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2017-12-27