catpkt/picture-provider

此包最新版本(dev-master)没有可用的许可证信息。

存储第三方图片和提供者URI。

dev-master 2017-07-20 12:57 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:03:50 UTC


README

存储第三方图片和提供者URI。

APIs

Upload a Picture
	POST /{app}
	param
		dir
			in query
			defaut
				/
		content-type
			in header
		content
			in body
			binary
	response
		200
			hash
				in body
				string

Get the Urls of a Picture
	GET /{app}/urls
	param
		hash
			in query
		sizes
			in query
			array
	response
		200
			urls
				in body
				array
				example
					100x100:http://...

Get the origin Url of a Picture
	GET /{app}/url
	param
		hash
			in query
	response
		200
			url
				in body
				string

Get the Content of a Picture
	GET /{app}/content
	param
		hash
			in query
	response
		200
			content
				in body
				binary

Preview the Pictures in the directory
	GET /{app}
	param
		dir
			in query
		sizes
			in query
			array
	response
		200
			list
				in body
				array

Delete Picture
	DELETE /{app}
	param
		path
			in query
	response
		200 Success
		404 No such Picture
		502 503 Failed
	comment
		Only delete Picture relation from the directory, never real delete the picture from storage

Copy Picture
	PATCH /{app}
	param
		path
			in form
		dir
			in form

Move Picture
	PATCH /{app}
	param
		path
			in form
		dir
			in form

用法

1. 创建扩展 \CatPKT\PictureProvider\AApp\CatPKT\PictureProvider\AStorage 的类,以及实现 \CatPKT\PictureProvider\IPicture 接口的类。
use CatPKT\PictureProvider as CP;

////////////////////////////////////////////////////////////////

class FooPicture implements IPicture
{
	public function getOriginUrl():string {}
	public function getUrlBySize( int$width, int$height ):string {}
	public function getContent():string {}
	public function getHash():string {}
}

class BarApp extends CP\AApp
{
	protected function createEncryptor():CP\Encryptor {}
}

class BazStorage extends CP\AStorage
{
	protected function findApp( string$appName ):CP\AApp {}                                          // Returns instance of BarApp actually.
	public function list( string$dir ):CP\PictureList {}
	public function get( string$hash ):CP\IPicture {}                                                // Returns instance of FooPicture actually.
	public function store( string$content, string$dir='/', string$contentType=null ):CP\IPicture {}  // Returns instance of FooPicture actually.
	public function delete( string$path ):bool {}
	public function copy( string$path, string$dir ):bool {}
	public function move( string$path, string$dir ):bool {}
}
2. 创建 CatPKT\PictureProvider\PictureProvider 的实例,并处理请求。
use CatPKT\PictureProvider as CP;
use Symfony\Component\HttpFoundation\Request;

////////////////////////////////////////////////////////////////

$pictureProvider= new CP\PictureProvider(
	new BarStorage();
);

$request= Request::createFromGlobals();

try{
	$response= $pictureProvider->handle( $request );
}
3. 如果存在,捕获异常或错误,并发送响应。
catch( \Throwable$e )
{
	#
}

$response->send();

使用框架

通常,您会使用框架来构建您的图片提供者。只需在控制器中使用此库,并将所有请求路由到 APIs。然后只需返回响应,让框架发送它。