2017 © Pedro Peláez
 

library filesystem

Middleware to save or read responses using Flysystem

image

middlewares/filesystem

Middleware to save or read responses using Flysystem

  • Thursday, January 25, 2018
  • by oscarotero
  • Repository
  • 2 Watchers
  • 5 Stars
  • 1,936 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 9 Versions
  • 20 % Grown

The README.md

middlewares/filesystem

Latest Version on Packagist ![Software License][ico-license] Testing ![Total Downloads][ico-downloads], (*1)

Middleware to save or read responses from files. It uses Flysystem as filesystem handler, so you can use not only a local directories, but also any other adapter like ftp, sftp, dropbox, etc... This package includes the following components:, (*2)

Requirements

Installation

This package is installable and autoloadable via Composer as middlewares/filesystem., (*3)

composer require middlewares/filesystem

Example

Dispatcher::run([
    Middlewares\Reader::createFromDirectory(__DIR__.'/assets')
]);

Reader

To read the response body from a file under the following conditions:, (*4)

  • Only GET methods are allowed, returning a 405 code otherwise.
  • If the request path has no extension, assume it's a directory and append /index.html. For example: if the request path is /post/23, the file used is /post/23/index.html.
  • It can handle gzipped files. For example, if /post/23/index.html does not exists but /post/23/index.html.gz is available and the request header Accept-Encoding contains gzip, returns it.
  • Accept-Ranges is also supported, useful to server big files like videos.

Example using a ftp storage:, (*5)

use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Ftp;

$filesystem = new Filesystem(new Ftp([
    'host' => 'ftp.example.com',
    'username' => 'username',
    'password' => 'password',
    'port' => 21,
    'root' => '/path/to/root',
    'passive' => true,
    'ssl' => true,
    'timeout' => 30,
]));

Dispatcher::run([
    new Middlewares\Reader($filesystem)
]);

Optionally, you can provide a Psr\Http\Message\ResponseFactoryInterface and Psr\Http\Message\StreamFactoryInterface, that will be used to create the response and stream. If they are not not defined, Middleware\Utils\Factory will be used to detect them automatically., (*6)

$responseFactory = new MyOwnResponseFactory();
$streamFactory = new MyOwnStreamFactory();

$reader = new Middlewares\Reader($filesystem, $responseFactory, $streamFactory);

continueOnError

Allows to continue to the next middleware on error (file not found, method not allowed, etc). This allows to create a simple caching system as the following:, (*7)

$cache = new Flysystem(new Local(__DIR__.'/path/to/files'));

Dispatcher::run([
    (new Middlewares\Reader($cache))    //read and returns the cached response...
        ->continueOnError(),            //...but continue if the file does not exists

    new Middlewares\Writer($cache),     //save the response in the cache

    new Middlewares\AuraRouter($route), //create a response using, for example, Aura.Router
]);

Writer

Saves the response content into a file if all of the following conditions are met:, (*8)

  • The method is GET
  • The status code is 200
  • The Cache-Control header does not contain no-cache and no-store

To be compatible with Reader behaviour:, (*9)

  • If the request path has no extension, assume it's a directory and append /index.html. For example: if the request path is /post/23, the file saved is /post/23/index.html.
  • If the response is gzipped (has the header Content-Encoding: gzip) the file is saved with the extension .gz. For example /post/23/index.html.gz (instead /post/23/index.html).
$filesystem = new Flysystem(new Local(__DIR__.'/storage'));

Dispatcher::run([
    new Middlewares\Writer($filesystem)
]);

Optionally, you can provide a Psr\Http\Message\StreamFactoryInterface as the second that will be used to create a new body to the response. If it's not defined, Middleware\Utils\Factory will be used to detect it automatically., (*10)

$streamFactory = new MyOwnStreamFactory();

$reader = new Middlewares\Writer($filesystem, $streamFactory);

Helpers

createFromDirectory

Both Reader and Writer have a static method as a shortcut to create instances using a directory in the local filesystem, due this is the most common case:, (*11)

Dispatcher::run([
    Middlewares\Writer::createFromDirectory(__DIR__.'/assets')
    Middlewares\Reader::createFromDirectory(__DIR__.'/assets')
]);

Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details., (*12)

The MIT License (MIT). Please see LICENSE for more information., (*13)

The Versions

25/01 2018

dev-master

9999999-dev https://github.com/middlewares/filesystem

Middleware to save or read responses using Flysystem

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware psr-7 filesystem cache http flysystem psr-15

25/01 2018

v1.0.0

1.0.0.0 https://github.com/middlewares/filesystem

Middleware to save or read responses using Flysystem

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware psr-7 filesystem cache http flysystem psr-15

13/11 2017
21/09 2017

v0.4.0

0.4.0.0 https://github.com/middlewares/filesystem

Middleware to save or read responses using Flysystem

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware psr-7 filesystem cache http flysystem psr-15

21/09 2017

v0.4.1

0.4.1.0 https://github.com/middlewares/filesystem

Middleware to save or read responses using Flysystem

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware psr-7 filesystem cache http flysystem psr-15

06/05 2017

v0.3.1

0.3.1.0 https://github.com/middlewares/filesystem

Middleware to save or read responses using Flysystem

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware psr-7 filesystem cache http flysystem psr-15

26/12 2016

v0.3.0

0.3.0.0 https://github.com/middlewares/filesystem

Middleware to save or read responses using Flysystem

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware psr-7 filesystem cache http flysystem psr-15

22/11 2016

v0.2.0

0.2.0.0 https://github.com/middlewares/filesystem

Middleware to save or read responses using Flysystem

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware psr-7 filesystem cache http flysystem psr-15

02/10 2016