sorokinmedia/yii2-flysystem

The flysystem extension(digitalocean spaces) for the Yii framework

Installs: 39

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 77

Type:yii2-extension

dev-master 2020-01-04 10:33 UTC

This package is auto-updated.

Last update: 2024-11-04 20:55:31 UTC


README

Total Downloads

This extension provides Flysystem integration for the Yii framework. Flysystem is a filesystem abstraction which allows you to easily swap out a local filesystem for a remote one.

Installation

The preferred way to install this extension is through composer.

Either run

$ composer require sorokinmedia/yii2-flysystem

or add

"sorokinmedia/yii2-flysystem": "dev-master"

to the require section of your composer.json file.

Configuring

Local filesystem

Configure application components as follows

return [
    //...
    'components' => [
        //...
        'fs' => [
            'class' => 'sorokinmedia\flysystem\LocalFilesystem',
            'path' => '@webroot/files',
        ],
    ],
];

AWS S3 filesystem

Either run

$ composer require league/flysystem-aws-s3-v3

or add

"league/flysystem-aws-s3-v3": "~1.0"

to the require section of your composer.json file and configure application components as follows

return [
    //...
    'components' => [
        //...
        'awss3Fs' => [
            'class' => 'sorokinmedia\flysystem\AwsS3Filesystem',
            'key' => 'your-key',
            'secret' => 'your-secret',
            'bucket' => 'your-bucket',
            'region' => 'your-region',
            // 'version' => 'latest',
            // 'baseUrl' => 'your-base-url',
            // 'prefix' => 'your-prefix',
            // 'options' => [],
            // 'endpoint' => 'http://my-custom-url'
        ],
    ],
];

Caching feature

Either run

$ composer require league/flysystem-cached-adapter

or add

"league/flysystem-cached-adapter": "~1.0"

to the require section of your composer.json file and configure fsID application component as follows

return [
    //...
    'components' => [
        //...
        'fsID' => [
            //...
            'cache' => 'cacheID',
            // 'cacheKey' => 'flysystem',
            // 'cacheDuration' => 3600,
        ],
    ],
];

Global visibility settings

Configure fsID application component as follows

return [
    //...
    'components' => [
        //...
        'fsID' => [
            //...
            'config' => [
                'visibility' => \League\Flysystem\AdapterInterface::VISIBILITY_PRIVATE,
            ],
        ],
    ],
];

Usage

Writing files

To write file

Yii::$app->fs->write('filename.ext', 'contents');

To write file using stream contents

$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->writeStream('filename.ext', $stream);

Updating files

To update file

Yii::$app->fs->update('filename.ext', 'contents');

To update file using stream contents

$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->updateStream('filename.ext', $stream);

Writing or updating files

To write or update file

Yii::$app->fs->put('filename.ext', 'contents');

To write or update file using stream contents

$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->putStream('filename.ext', $stream);

Reading files

To read file

$contents = Yii::$app->fs->read('filename.ext');

To retrieve a read-stream

$stream = Yii::$app->fs->readStream('filename.ext');
$contents = stream_get_contents($stream);
fclose($stream);

Checking if a file exists

To check if a file exists

$exists = Yii::$app->fs->has('filename.ext');

Deleting files

To delete file

Yii::$app->fs->delete('filename.ext');

Reading and deleting files

To read and delete file

$contents = Yii::$app->fs->readAndDelete('filename.ext');

Renaming files

To rename file

Yii::$app->fs->rename('filename.ext', 'newname.ext');

Getting files mimetype

To get file mimetype

$mimetype = Yii::$app->fs->getMimetype('filename.ext');

Getting files timestamp

To get file timestamp

$timestamp = Yii::$app->fs->getTimestamp('filename.ext');

Getting files size

To get file size

$timestamp = Yii::$app->fs->getSize('filename.ext');

Creating directories

To create directory

Yii::$app->fs->createDir('path/to/directory');

Directories are also made implicitly when writing to a deeper path

Yii::$app->fs->write('path/to/filename.ext');

Deleting directories

To delete directory

Yii::$app->fs->deleteDir('path/to/filename.ext');

Managing visibility

Visibility is the abstraction of file permissions across multiple platforms. Visibility can be either public or private.

use League\Flysystem\AdapterInterface;

Yii::$app->fs->write('filename.ext', 'contents', [
    'visibility' => AdapterInterface::VISIBILITY_PRIVATE
]);

You can also change and check visibility of existing files

use League\Flysystem\AdapterInterface;

if (Yii::$app->fs->getVisibility('filename.ext') === AdapterInterface::VISIBILITY_PRIVATE) {
    Yii::$app->fs->setVisibility('filename.ext', AdapterInterface::VISIBILITY_PUBLIC);
}

Listing contents

To list contents

$contents = Yii::$app->fs->listContents();

foreach ($contents as $object) {
    echo $object['basename']
        . ' is located at' . $object['path']
        . ' and is a ' . $object['type'];
}

By default Flysystem lists the top directory non-recursively. You can supply a directory name and recursive boolean to get more precise results

$contents = Yii::$app->fs->listContents('path/to/directory', true);

Listing paths

To list paths

$paths = Yii::$app->fs->listPaths();

foreach ($paths as $path) {
    echo $path;
}

Listing with ensured presence of specific metadata

To list with ensured presence of specific metadata

$listing = Yii::$app->fs->listWith(
    ['mimetype', 'size', 'timestamp'],
    'optional/path/to/directory',
    true
);

foreach ($listing as $object) {
    echo $object['path'] . ' has mimetype: ' . $object['mimetype'];
}

Getting file info with explicit metadata

To get file info with explicit metadata

$info = Yii::$app->fs->getWithMetadata('path/to/filename.ext', ['timestamp', 'mimetype']);
echo $info['mimetype'];
echo $info['timestamp'];