Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #1

Merged
merged 18 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
composer.lock
/vendor
.env
.updated
101 changes: 91 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Laravel Fileable

[![Latest Version on Packagist](https://img.shields.io/packagist/v/toneflix-code/laravel-fileable.svg?style=flat-round)](https://packagist.org/packages/toneflix-code/laravel-fileable)
[![Total Downloads](https://img.shields.io/packagist/dt/toneflix-code/laravel-fileable.svg?style=flat-round)](https://packagist.org/packages/toneflix-code/laravel-fileable)
[![Test & Lint](https://github.com/toneflix/laravel-fileable/actions/workflows/php.yml/badge.svg?branch=dev)](https://github.com/toneflix/laravel-fileable/actions/workflows/php.yml)
[![Latest Stable Version](http://poser.pugx.org/toneflix-code/laravel-fileable/v)](https://packagist.org/packages/toneflix-code/laravel-fileable) [![Total Downloads](http://poser.pugx.org/toneflix-code/laravel-fileable/downloads)](https://packagist.org/packages/toneflix-code/laravel-fileable) [![Latest Unstable Version](http://poser.pugx.org/toneflix-code/laravel-fileable/v/unstable)](https://packagist.org/packages/toneflix-code/laravel-fileable) [![License](http://poser.pugx.org/toneflix-code/laravel-fileable/license)](https://packagist.org/packages/toneflix-code/laravel-fileable) [![PHP Version Require](http://poser.pugx.org/toneflix-code/laravel-fileable/require/php)](https://packagist.org/packages/toneflix-code/laravel-fileable)
[![codecov](https://codecov.io/gh/toneflix/laravel-fileable/graph/badge.svg?token=2O7aFulQ9P)](https://codecov.io/gh/toneflix/laravel-fileable)

<!-- ![GitHub Actions](https://github.com/toneflix/laravel-fileable/actions/workflows/main.yml/badge.svg) -->

Expand All @@ -15,7 +16,7 @@ You can install the package via composer:
composer require toneflix-code/laravel-fileable
```

## Installation
## Package Discovery

Laravel automatically discovers and publishes service providers but optionally after you have installed Laravel Fileable, open your Laravel config file config/app.php and add the following lines.

Expand All @@ -31,6 +32,16 @@ Add the facade of this package to the $aliases array.
'Fileable' => ToneflixCode\LaravelFileable\Facades\Fileable::class
```

## Upgrading

Version 2.x is not compatible with version 1.x, if you are ugrading from version 1.x here are a few notes:

### Config

1. If you published the configuration file, remove `image_templates`. Templates are no longer needed, just set you responsive image sizes using the `image_sizes` property.

2. Add `responsive_image_route` and set the value `route/path/{file}/{size}`, `route/path` can be whatever you want it to be, `{file}/{size}` can be anything you want to name them but both are required.

## Configuration

By default Laravel Fileable `avatar` and `media` directories and symlinks to your `storage/app/public` directories, and also adds the `file` directory to your `storage/app` directory.
Expand Down Expand Up @@ -103,15 +114,21 @@ class User extends Model

```

The `fileableLoader()` method accepts and array of `[key => value]` pairs that determines which files should be auto discovered in your request, the `key` should match the name field in your input field E.g `<input type="file" name="avatar">`, the `value` should be an existing collection in your Laravel Fileable configuration.
### fileableLoader.

The `fileableLoader` is responsible for mapping your model to the required collection and indicates that you want to use Laravel Filable to manage your model files.

The `fileableLoader()` method accepts an array of `[key => value]` pairs that determines which files should be auto discovered in your request, the `key` should match the name field in your input field E.g `<input type="file" name="avatar">`, the `value` should be an existing collection in your Laravel Fileable configuration.

#### Single collection initialization.

```php
$this->fileableLoader([
'avatar' => 'avatar',
]);
```

OR
#### Multiple collection initialization.

```php
$this->fileableLoader([
Expand All @@ -120,19 +137,84 @@ $this->fileableLoader([
]);
```

The `fileableLoader()` method also accepts the `key` as a string as the first parameter and the `value` as a string as the second parameter.
#### String parameter initialization.

The `fileableLoader()` method also accepts the `key` as a string first parameter and the `value` as a string as the second parameter.

```php
$this->fileableLoader('avatar', 'default');
```

#### Loading|Not Loading default media.
#### Default media.

COnfigured default files are not loaded by default, to load the default file for the model, the `fileableLoader` exposes a third parameter, the `useDefault` parameter, setting it to true will ensure that your default file is loaded when the model's file is not found or missing.

```php
$this->fileableLoader('avatar', 'default', true);
```

OR

The third parameter of the `fileableLoader()` is a boolean value that determines wether to return null or the default image when the requested file is not found.
```php
$this->fileableLoader([
'avatar' => 'avatar',
], 'default', true);
```

#### Supporting old setup (Legacy Mode)

If you had your model running before the introducation of the the Fileable trait, you might still be able to load your existing files by passing a fourth parameter to the `fileableLoader()`, the **Legacy mode** attempts to load media files that had been stored or managed by a different logic before the introduction of the fileable trait.
If you had your model running before the introducation of the the Fileable trait, you might still be able to load your existing files by passing a fourth parameter to the `fileableLoader()`, the **Legacy mode** attempts to load media files that had been stored or managed by a different logic or system before the introduction of the fileable trait.

```php
$this->fileableLoader('avatar', 'default', true, true);
```

OR

```php
$this->fileableLoader([
'avatar' => 'avatar',
], 'default', true, true);
```

#### Custom Database field.

There are times when you may want to use a different file name from your database field name, an instance could be when your request includes two diffrent file requests for different models that have the same database field names, the last parameter of the `fileableLoader` was added to support this scenario.

The 5th parameter of the `fileableLoader` is a string that should equal to the database field where you want your file reference stored in or an array that maps the request file name to the database field name.

Take a look at this example.

```html
<input name="cover" type="file" /> <input name="admin_avatar" type="file" />
```

```php
$this->fileableLoader('admin_avatar', 'default', true, true, 'image');
```

OR

```php
$this->fileableLoader([
'admin_avatar' => 'avatar',
], 'default', true, true, 'image');
```

OR

```php
$this->fileableLoader([
'cover' => 'cover',
'admin_avatar' => 'avatar',
], 'default', true, true, [
'cover' => 'cover_image',
'admin_avatar' => 'image',
]);
```

In the last example, `cover_image` is an existing database field mapped to the `cover` input request file name and `image` is an existing database field mapped to the `admin_avatar` input request file name.


### Model Events

Expand Down Expand Up @@ -224,7 +306,6 @@ var_dump($post->responsive_images['banner']);

While the library will try to resolve media files from the configured collection, you can also force media file search from collections different from the configured ones by saving the path reference on the database with a `collection:filename.ext` prefix, this will allow the system to look for media files in a collection named `collection` even if the current collection for the model is a collection named `images`;


### Testing

```bash
Expand Down
37 changes: 24 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,33 @@
"description": "Laravel Fileable exposes methods that make handling file upload with Laravel filesystem even easier, it also exposes a trait that automatically handles file uploads for you.",
"keywords": [
"toneflix-code",
"laravel-fileable"
"laravel-fileable",
"version 2.x"
],
"homepage": "https://github.com/toneflix/laravel-fileable",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "Toneflix Code",
"email": "code@toneflix.com.ng",
"name": "Legacy",
"email": "legacy@toneflix.com.ng",
"homepage": "https://legacy.toneflix.com.ng",
"role": "Developer"
}
],
"require": {
"php": "^8.0|^8.1|8.2",
"laravel/framework": "^9.2|^10.0",
"illuminate/filesystem": "^8.0|~9|~10",
"intervention/image": "^2.7",
"intervention/imagecache": "^2.5|^2.6"
"php": "^8.1|^8.2|^8.3",
"illuminate/filesystem": "^8.1|^9.0|^10.0|^11.0",
"illuminate/support": "^8.1|^9.0|^10.0|^11.0",
"intervention/image": "^3.5"
},
"require-dev": {
"orchestra/testbench": "^7.0",
"phpunit/phpunit": "^9.0"
"pestphp/pest": "2.x-dev",
"laravel/pint": "^1.15",
"fakerphp/faker": "^1.23",
"illuminate/contracts": "^9.0|^10.0|^11.0",
"orchestra/testbench": "^8.8",
"pestphp/pest-plugin-laravel": "^2.0"
},
"autoload": {
"psr-4": {
Expand All @@ -33,14 +38,18 @@
},
"autoload-dev": {
"psr-4": {
"ToneflixCode\\LaravelFileable\\Tests\\": "tests"
"ToneflixCode\\LaravelFileable\\Tests\\": "tests",
"ToneflixCode\\LaravelFileable\\Tests\\Database\\Factories\\": "tests/database/factories"
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
},
"sort-packages": true
},
"extra": {
Expand All @@ -52,5 +61,7 @@
"LaravelFileable": "ToneflixCode\\LaravelFileable\\Facades\\Fileable"
}
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
8 changes: 4 additions & 4 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

//
return [
'collections' => [
'avatar' => [
Expand Down Expand Up @@ -49,13 +50,12 @@
'lg-square' => '720x720',
'xl-square' => '1080x1080',
],
'file_route_secure_middleware' => 'web',
'file_route_secure_middleware' => 'window_auth',
'responsive_image_route' => 'images/responsive/{file}/{size}',
'file_route_secure' => 'secure/files/{file}',
'file_route_open' => 'open/files/{file}',
'image_templates' => [
],
'symlinks' => [
public_path('avatars') => storage_path('app/public/avatars'),
public_path('media') => storage_path('app/public/media'),
],
];
];
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
15 changes: 12 additions & 3 deletions routes/routes.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
<?php

use Illuminate\Support\Facades\Route;
use ToneflixCode\LaravelFileable\Media;

// The public private secure generator route
Route::get(config('toneflix-fileable.file_route_secure', 'load/images/{file}'), function ($file) {
return (new Media)->privateFile($file);
return (new Media())->privateFile($file);
})->middleware(config('toneflix-fileable.file_route_secure_middleware', []) ?? [])->name('fileable.secure.file');

// The public image generator route
Route::get(config('toneflix-fileable.file_route_open', 'load/images/{file}'), function ($file) {
return (new Media)->privateFile($file);
})->name('fileable.open.file');
return (new Media())->privateFile($file);
})->name('fileable.open.file');

// The responsive images route
Route::get(config('toneflix-fileable.responsive_image_route', 'images/responsive/{size}/{file}'),
function (string $size, string $file) {
return (new Media())->resizeResponse($file, $size);
})->name('imagecache');
Binary file added src/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Facades/Fileable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use ToneflixCode\LaravelFileable\Media;

/**
* @see \ToneflixCode\FileableFacade\Skeleton\SkeletonClass
* @see \ToneflixCode\LaravelFileable\Media
*/
class Fileable extends Facade
{
Expand Down
1 change: 1 addition & 0 deletions src/FileableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ToneflixCode\LaravelFileable;

use Illuminate\Support\ServiceProvider;

use ToneflixCode\LaravelFileable\Intervention\Media1080;
use ToneflixCode\LaravelFileable\Intervention\Media1080Square;
use ToneflixCode\LaravelFileable\Intervention\Media431;
Expand Down
4 changes: 2 additions & 2 deletions src/Initiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Initiator
*
* @return Collection<TKey, TValue>
*/
public static function collectionPaths(): array | Collection
public static function collectionPaths(): array|Collection
{
$deepPaths = collect(config('toneflix-fileable.collections', []))->map(function ($col, $key) {
$getPath = Arr::get(config('toneflix-fileable.collections', []), $key.'.path');
Expand Down Expand Up @@ -41,7 +41,7 @@ public static function collectionPaths(): array | Collection
public static function asset(string $url, $absolute = false): string
{
if ($absolute) {
return str($url)->replace('http:', request()->isSecure() ? 'https:' :'http:')->toString();
return str($url)->replace('http:', request()->isSecure() ? 'https:' : 'http:')->toString();
}

return request()->isSecure()
Expand Down
14 changes: 0 additions & 14 deletions src/Intervention/Media1080.php

This file was deleted.

14 changes: 0 additions & 14 deletions src/Intervention/Media431.php

This file was deleted.

14 changes: 0 additions & 14 deletions src/Intervention/Media694.php

This file was deleted.

Loading
Loading