Skip to content

Commit 650cfd4

Browse files
committed
first commit
0 parents  commit 650cfd4

19 files changed

+506
-0
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_size = 4
6+
indent_style = space
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.{yml,yaml,js,scss,css}]
15+
indent_size = 2

.gitattributes

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/.github export-ignore
2+
/.editorconfig export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
5+
/phpcs.xml export-ignore

.github/dependabot.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Please see the documentation for all configuration options:
2+
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
3+
4+
version: 2
5+
updates:
6+
7+
- package-ecosystem: "github-actions"
8+
directory: "/"
9+
schedule:
10+
interval: "weekly"
11+
labels:
12+
- "dependencies"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: dependabot-auto-merge
2+
on: pull_request_target
3+
4+
permissions:
5+
pull-requests: write
6+
contents: write
7+
8+
jobs:
9+
dependabot:
10+
runs-on: ubuntu-latest
11+
if: ${{ github.actor == 'dependabot[bot]' }}
12+
steps:
13+
14+
- name: Dependabot metadata
15+
id: metadata
16+
uses: dependabot/[email protected]
17+
with:
18+
github-token: "${{ secrets.GITHUB_TOKEN }}"
19+
20+
- name: Auto-merge Dependabot PRs for semver-minor updates
21+
if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}}
22+
run: gh pr merge --auto --merge "$PR_URL"
23+
env:
24+
PR_URL: ${{github.event.pull_request.html_url}}
25+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
26+
27+
- name: Auto-merge Dependabot PRs for semver-patch updates
28+
if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}}
29+
run: gh pr merge --auto --merge "$PR_URL"
30+
env:
31+
PR_URL: ${{github.event.pull_request.html_url}}
32+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: "Update Changelog"
2+
3+
on:
4+
release:
5+
types: [released]
6+
7+
jobs:
8+
update:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v3
14+
with:
15+
ref: main
16+
17+
- name: Update Changelog
18+
uses: stefanzweifel/changelog-updater-action@v1
19+
with:
20+
latest-version: ${{ github.event.release.name }}
21+
release-notes: ${{ github.event.release.body }}
22+
23+
- name: Commit updated CHANGELOG
24+
uses: stefanzweifel/git-auto-commit-action@v4
25+
with:
26+
branch: main
27+
commit_message: Update CHANGELOG
28+
file_pattern: CHANGELOG.md

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea
2+
.php_cs
3+
.php_cs.cache
4+
.php-cs-fixer.cache
5+
composer.lock
6+
vendor

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Changelog
2+
3+
All notable changes to will be documented in this file.

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 runthis
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Laravel Media
2+
3+
A tiny laravel package to conveniently handle single file media uploads with little configuration.
4+
5+
6+
7+
## Installation
8+
9+
composer require runthis/laravel-media
10+
11+
12+
13+
## Usage
14+
15+
Add to the file you want to process uploads in, such as a controller.
16+
17+
use Runthis\Media\Facades\Media;
18+
use Runthis\Media\Requests\MediaRequest;
19+
20+
21+
Include the `MediaRequest` class in the function parameter and execute the `create` method on the `Media` facade.
22+
23+
Example:
24+
25+
public function upload(MediaRequest $request) {
26+
$test = Media::create($request);
27+
dd($test);
28+
}
29+
30+
Inside the `dd()` you can see the complete object details and process these as you see fit (such as keeping track of these uploads in a database if you like).
31+
32+
Within the object results is a `size` key. You can simply echo this out to get the bytes, or you can add `->pretty()` to get a prettier output.
33+
You can also pass a string parameter to the `pretty()` method.
34+
35+
Options:
36+
37+
l: lowercase suffix (12.45 mb instead of 12.45 MB)
38+
s: spacing omitted (12.45MB instead of 12.45 MB)
39+
b: Ending "B" removed (12.45 M instead of 12.45 MB)
40+
41+
Examples:
42+
43+
$test->size->pretty('sb'); // 12.45M
44+
$test->size->pretty('ls'); // 12.45m
45+
$test->size->pretty('l'); // 12.45 mb
46+
$test->size->pretty('bl'); // 12.45 m
47+
48+
49+
*The `Media::create` method expects a file with the key named file.*
50+
51+
52+
53+
## Publish Config
54+
55+
If you want, run the below command to add a media.php file to your config folder.
56+
57+
php artisan vendor:publish --tag="media-config"
58+
59+
From the media config file you can change the storage disk name and the rules for the media (such as file types, size limit, etc).
60+
61+
62+
63+
## Changelog
64+
65+
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
66+

composer.json

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "runthis/laravel-media",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"description": "Handle media in laravel packages",
6+
"keywords": [
7+
"laravel",
8+
"media"
9+
],
10+
"homepage": "https://github.com/runthis/laravel-media",
11+
"authors": [
12+
{
13+
"name": "runthis"
14+
}
15+
],
16+
"require": {
17+
"php": "^8.0",
18+
"guzzlehttp/guzzle": "^7.4",
19+
"illuminate/contracts": "^9.0",
20+
"illuminate/http": "^9.9",
21+
"illuminate/support": "^9.9",
22+
"spatie/laravel-package-tools": "^1.9.2"
23+
},
24+
"require-dev": {
25+
"nunomaduro/collision": "^6.0",
26+
"orchestra/testbench": "^7.0",
27+
"spatie/laravel-ray": "^1.26",
28+
"squizlabs/php_codesniffer": "^3.6"
29+
},
30+
"autoload": {
31+
"psr-4": {
32+
"Runthis\\Media\\": "src"
33+
}
34+
},
35+
"config": {
36+
"sort-packages": true
37+
},
38+
"extra": {
39+
"laravel": {
40+
"providers": [
41+
"Runthis\\Media\\MediaServiceProvider"
42+
],
43+
"aliases": {
44+
"Media": "Runthis\\Media\\Facades\\Media"
45+
}
46+
}
47+
},
48+
"minimum-stability": "dev",
49+
"prefer-stable": true
50+
}

config/media.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'disk' => 'public',
5+
'rules' => 'mimes:jpeg,jpg,png,gif,webp,svg,image/svg+xml|max:1024',
6+
];

phpcs.xml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Laravel Standards">
3+
<description>The Laravel Coding Standards</description>
4+
5+
<arg name="report" value="summary"/>
6+
<arg name="colors"/>
7+
<arg value="p"/>
8+
9+
<ini name="memory_limit" value="512M"/>
10+
11+
<exclude-pattern>*/node_modules/*</exclude-pattern>
12+
<exclude-pattern>*/resources/*</exclude-pattern>
13+
<exclude-pattern>*/tests/*</exclude-pattern>
14+
<exclude-pattern>*/vendor/*</exclude-pattern>
15+
<exclude-pattern>*/src/Commands/*</exclude-pattern>
16+
17+
<rule ref="PSR12">
18+
<exclude name="PSR12.Files.FileHeader.SpacingInsideBlock"/>
19+
<exclude name="Generic.Files.LineLength"/>
20+
<exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingAfterOpen"/>
21+
</rule>
22+
<rule ref="Generic.WhiteSpace.ScopeIndent">
23+
<properties>
24+
<property name="indent" value="4"/>
25+
<property name="tabIndent" value="false"/>
26+
</properties>
27+
</rule>
28+
</ruleset>

src/Attributes/Size.php

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Runthis\Media\Attributes;
4+
5+
use function floor;
6+
use function in_array;
7+
use function log;
8+
use function pow;
9+
use function sprintf;
10+
use function str_ireplace;
11+
use function str_replace;
12+
use function str_split;
13+
use function strtolower;
14+
15+
class Size
16+
{
17+
/**
18+
* Set the bytes.
19+
*/
20+
public function __construct(public int $bytes)
21+
{
22+
}
23+
24+
/**
25+
* Format file size provided from bytes.
26+
*
27+
* Options:
28+
* l: lowercase suffix (12.45 mb instead of 12.45 MB)
29+
* s: spacing omitted (12.45MB instead of 12.45 MB)
30+
* b: Ending "B" removed (12.45 M instead of 12.45 MB)
31+
*/
32+
public function pretty(string $options = ''): string
33+
{
34+
$format = '%.02F %sB';
35+
$suffix = 'BKMGTPEZY';
36+
$options = str_split($options);
37+
38+
// Set byte formatting
39+
$format = $this->bytes < 1024 ? (in_array('b', $options, true) ? '%.0F' : '%.0F %s') : $format;
40+
41+
// Lowercase
42+
$suffix = in_array('l', $options, true) ? strtolower($suffix) : $suffix;
43+
$format = in_array('l', $options, true) ? str_replace('B', 'b', $format) : $format;
44+
45+
// No ending "b"
46+
$format = in_array('b', $options, true) ? str_ireplace('b', '', $format) : $format;
47+
48+
// No spacing
49+
$format = in_array('s', $options, true) ? str_replace(' ', '', $format) : $format;
50+
51+
$index = floor(log($this->bytes) / log(1024));
52+
53+
return sprintf($format, $this->bytes / pow(1024, $index), str_split($suffix)[$index]);
54+
}
55+
56+
/**
57+
* Just send back the bytes.
58+
*/
59+
public function __toString(): string
60+
{
61+
return (string) $this->bytes;
62+
}
63+
}

src/Contracts/Media.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Runthis\Media\Contracts;
4+
5+
use Runthis\Media\Requests\MediaRequest;
6+
7+
interface Media
8+
{
9+
/**
10+
* Create the media.
11+
*
12+
* @return array{
13+
* file: \Illuminate\Http\UploadedFile,
14+
* storage: \Illuminate\Contracts\Filesystem\Filesystem,
15+
* size: string | \Runthis\Media\Attributes\Size,
16+
* url: string
17+
* }
18+
*/
19+
public function create(MediaRequest $request, string $directory = ''): object;
20+
}

src/Facades/Media.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Runthis\Media\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* @method static object{ file: \Illuminate\Http\UploadedFile, storage: \Illuminate\Contracts\Filesystem\Filesystem, size: string | \Runthis\Media\Attributes\Size, url: string, path: string } create(\Runthis\Media\Requests\MediaRequest $request, string $directory = '') Create media and return object.
9+
*/
10+
class Media extends Facade
11+
{
12+
protected static function getFacadeAccessor(): string
13+
{
14+
return 'media';
15+
}
16+
}

0 commit comments

Comments
 (0)