sunkan/php-mogilefs

MogileFs client for php

3.1.0 2023-09-27 08:41 UTC

This package is auto-updated.

Last update: 2024-03-27 09:57:57 UTC


README

Installation

The preferred method of installing this library is with Composer by running the following from your project root:

$ composer require sunkan/php-mogilefs

Documentation

Instantiating

<?php
$connection = new MogileFs\Connection([
    '127.0.0.1:7001'
]);

//or

$connection = new MogileFs\Connection([
    [
        'host' => '127.0.0.1',
        'port' => 7001
    ]
]);

Domain client

Create domain

$domainClient = new MogileFs\Client\DomainClient($connection);
try {
	$domain = $domainClient->create('example.com');

	$domain->getDomain(); // domain name
	$domain->getClasses(); // array of classes
} catch(MogileFs\Exception $e) {
	$e->getMessage() === 'domain_exists';
}

Delete domain

$domainClient = new MogileFs\Client\DomainClient($connection);
try {
	$response = $domainClient->delete('example.com');
	$response->isSuccess();
} catch(MogileFs\Exception $e) {
	$e->getMessage() === 'domain_not_found';
}

List domains

$domainClient = new MogileFs\Client\DomainClient($connection);

$collection = $domainClient->all();

foreach ($collection as $domain) {
	$domain->getDomain(); // domain name
	$domain->getClasses(); // array of classes
}

Class client

Instantiating

$classClient = new MogileFs\Client\ClassClient($connection, 'example.com');
//or
$classClient = new MogileFs\Client\ClassClient($connection);
$classClient->setDomain('example.com');

Create class

$classClient = new MogileFs\Client\ClassClient($connection, 'example.com');

try {
	$replicationCount = 2;
	$class = $classClient->create('assets', $replicationCount);
	$class->getName(); // class name
	$class->getCount(); // replication count
	$class->getPolicy(); // replication policy
	$class->getHash(); // hash policy
	$class->getDomain(); // domain name
} catch(MogileFs\Exception $e) {
	$e->getMessage() === 'class_exists';
}

Update class

$classClient = new MogileFs\Client\ClassClient($connection, 'example.com');

try {
	$newReplicationCount = 4;
	$class = $classClient->update('assets', $replicationCount);
} catch(MogileFs\Exception $e) {
	$e->getMessage() === 'class_not_found';
}

Delete class

$classClient = new MogileFs\Client\ClassClient($connection, 'example.com');

try {
	$response = $classClient->delete('assets');
	$response->isSuccess();
} catch(MogileFs\Exception $e) {
	$e->getMessage() === 'class_not_found';
}

File client

Instantiating

$fileClient = new MogileFs\Client\FileClient($connection, 'example.com');
//or
$fileClient = new MogileFs\Client\FileClient($connection);
$fileClient->setDomain('example.com');

Get paths

$fileClient = new MogileFs\Client\FileClient($connection, 'example.com');

try {
	$path = $fileClient->get('test/key');
	$path->getPath(); // first path
	$path->getPaths(); // array of paths
	$path->getCount(); // number of paths
} catch(MogileFs\Exception $e) {
	$e->getMessage() === 'unknown_key';
}

Get file info

$fileClient = new MogileFs\Client\FileClient($connection, 'example.com');

try {
	$file = $fileClient->info('test/key');
	$file->getFid(); // file id
	$file->getKey(); // file key - test/key
	$file->getSize(); // file size
	$file->getFileCount(); // replication count
	$file->getDomain(); // file domain
	$file->getClass(); // file class
} catch(MogileFs\Exception $e) {
	$e->getMessage() === 'unknown_key';
}

Delete file

$fileClient = new MogileFs\Client\FileClient($connection, 'example.com');

$response = $fileClient->delete('test/key');
$response->isSuccess();

Rename file

$fileClient = new MogileFs\Client\FileClient($connection, 'example.com');

$response = $fileClient->rename('test/key', 'test/key2');
$response->isSuccess();

List files

$fileClient = new MogileFs\Client\FileClient($connection, 'example.com');

$collection = $fileClient->listKeys($prefix, $suffix, $limit);
$collection; // contains file keys nothing else

$collection = $fileClient->listFids($fromFid, $toFid);
$collection; // contains File objects with information about all files in range

Upload file

The upload function accepts a any objects that implements MogileFs\File\FileInterface

File types
Blob
$class = "blob-class";
$file = new MogileFs\File\BlobFile('raw data', $class)
Local file
$class = "local-file-class";
$file = new MogileFs\File\LocalFile('/path/to/file', $class)
Psr-7 file upload
$class = "psr7-file-class";

$uploadFile = $request->getUploadedFiles()['file'];
$file = new MogileFs\File\Psr7File($uploadFile, $class)

//or

$stream;// instance of Psr\Http\Message\StreamInterface
$file = new MogileFs\File\Psr7File($stream, $class)
Resource
$class = "resource-file-class";
$resource = fopen('file', 'r'); // should work with any stream context
$file = new MogileFs\File\ResourceFile($resource, $class)
Factory
$factory = new MogileFs\File\Factory();
$file = $factory($fileContent, $class);
Upload
$fileClient = new MogileFs\Client\FileClient($connection, 'example.com');
$factory = new MogileFs\File\Factory();

$key = 'test/key';
$file = $factory($fileContent, $class);
$response = $fileClient->upload($key, $file);
if ($response->isSuccess()) {
	$path = $fileClient->get($key);
}