fixed remote media streaming

This commit is contained in:
Denis Duliçi 2021-05-11 23:43:19 +03:00
parent d46bff3c2d
commit bd39c2b9a2
4 changed files with 266 additions and 16 deletions

View File

@ -5,8 +5,8 @@ namespace App\Http\Controllers\Common;
use App\Abstracts\Http\Controller;
use App\Models\Common\Media;
use App\Traits\Uploads as Helper;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use File;
class Uploads extends Controller
{
@ -31,7 +31,7 @@ class Uploads extends Controller
return response(null, 204);
}
return response()->file($path);
return $this->streamMedia($media, $path);
}
/**
@ -112,7 +112,7 @@ class Uploads extends Controller
return false;
}
return response()->download($path);
return $this->streamMedia($media, $path);
}
/**
@ -151,7 +151,7 @@ class Uploads extends Controller
$media->delete(); //will not delete files
File::delete($path);
Storage::delete($path);
if (!empty($request->input('page'))) {
switch ($request->input('page')) {

View File

@ -111,4 +111,51 @@ trait Uploads
return Storage::path($path);
}
public function streamMedia($media, $path = '', $action = '')
{
if ($this->isLocalStorage()) {
if (empty($path)) {
$path = $this->getMediaPathOnStorage($media);
}
if (empty($action)) {
$action = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
}
return $this->streamLocalMedia($path, $action);
}
return $this->streamRemoteMedia($media);
}
public function streamLocalMedia($path, $action)
{
$function = ($action == 'get') ? 'file' : $action;
return response()->$function($path);
}
public function streamRemoteMedia($media)
{
return response()->streamDownload(
function() use ($media) {
$stream = $media->stream();
while($bytes = $stream->read(1024)) {
echo $bytes;
}
},
$media->basename,
[
'Content-Type' => $media->mime_type,
'Content-Length' => $media->size,
],
);
}
public function isLocalStorage()
{
return config('filesystems.disks.' . config('filesystems.default') . '.driver') == 'local';
}
}

View File

@ -46,6 +46,7 @@
"laravel/tinker": "^2.5",
"laravel/ui": "^3.0",
"laravelcollective/html": "6.2.*",
"league/flysystem-aws-s3-v3": "1.0.*",
"league/oauth2-client": "2.6.*",
"league/omnipay": "3.1.*",
"livewire/livewire": "2.3.*",

226
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "0f9d8ca997b25292972d0b41575258a6",
"content-hash": "49b08641aed852dd08fe0e3da19ea37e",
"packages": [
{
"name": "akaunting/laravel-firewall",
@ -627,6 +627,96 @@
},
"time": "2021-03-11T06:42:03+00:00"
},
{
"name": "aws/aws-sdk-php",
"version": "3.180.6",
"source": {
"type": "git",
"url": "https://github.com/aws/aws-sdk-php.git",
"reference": "1a836be14ba664ea11eb965dec77715e9e607013"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/1a836be14ba664ea11eb965dec77715e9e607013",
"reference": "1a836be14ba664ea11eb965dec77715e9e607013",
"shasum": ""
},
"require": {
"ext-json": "*",
"ext-pcre": "*",
"ext-simplexml": "*",
"guzzlehttp/guzzle": "^5.3.3|^6.2.1|^7.0",
"guzzlehttp/promises": "^1.4.0",
"guzzlehttp/psr7": "^1.7.0",
"mtdowling/jmespath.php": "^2.6",
"php": ">=5.5"
},
"require-dev": {
"andrewsville/php-token-reflection": "^1.4",
"aws/aws-php-sns-message-validator": "~1.0",
"behat/behat": "~3.0",
"doctrine/cache": "~1.4",
"ext-dom": "*",
"ext-openssl": "*",
"ext-pcntl": "*",
"ext-sockets": "*",
"nette/neon": "^2.3",
"paragonie/random_compat": ">= 2",
"phpunit/phpunit": "^4.8.35|^5.4.3",
"psr/cache": "^1.0",
"psr/simple-cache": "^1.0",
"sebastian/comparator": "^1.2.3"
},
"suggest": {
"aws/aws-php-sns-message-validator": "To validate incoming SNS notifications",
"doctrine/cache": "To use the DoctrineCacheAdapter",
"ext-curl": "To send requests using cURL",
"ext-openssl": "Allows working with CloudFront private distributions and verifying received SNS messages",
"ext-sockets": "To use client-side monitoring"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"autoload": {
"psr-4": {
"Aws\\": "src/"
},
"files": [
"src/functions.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"authors": [
{
"name": "Amazon Web Services",
"homepage": "http://aws.amazon.com"
}
],
"description": "AWS SDK for PHP - Use Amazon Web Services in your PHP project",
"homepage": "http://aws.amazon.com/sdkforphp",
"keywords": [
"amazon",
"aws",
"cloud",
"dynamodb",
"ec2",
"glacier",
"s3",
"sdk"
],
"support": {
"forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
"issues": "https://github.com/aws/aws-sdk-php/issues",
"source": "https://github.com/aws/aws-sdk-php/tree/3.180.6"
},
"time": "2021-05-10T18:17:07+00:00"
},
{
"name": "balping/json-raw-encoder",
"version": "v1.0.1",
@ -4979,16 +5069,16 @@
},
{
"name": "laravel/framework",
"version": "v8.40.0",
"version": "v8.41.0",
"source": {
"type": "git",
"url": "https://github.com/laravel/framework.git",
"reference": "a654897ad7f97aea9d7ef292803939798c4a02a4"
"reference": "05417155d886df8710e55c84e12622b52d83c47c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/framework/zipball/a654897ad7f97aea9d7ef292803939798c4a02a4",
"reference": "a654897ad7f97aea9d7ef292803939798c4a02a4",
"url": "https://api.github.com/repos/laravel/framework/zipball/05417155d886df8710e55c84e12622b52d83c47c",
"reference": "05417155d886df8710e55c84e12622b52d83c47c",
"shasum": ""
},
"require": {
@ -5096,7 +5186,7 @@
"phpunit/phpunit": "Required to use assertions and run tests (^8.5.8|^9.3.3).",
"predis/predis": "Required to use the predis connector (^1.1.2).",
"psr/http-message": "Required to allow Storage::put to accept a StreamInterface (^1.0).",
"pusher/pusher-php-server": "Required to use the Pusher broadcast driver (^4.0|^5.0).",
"pusher/pusher-php-server": "Required to use the Pusher broadcast driver (^4.0|^5.0|^6.0).",
"symfony/cache": "Required to PSR-6 cache bridge (^5.1.4).",
"symfony/filesystem": "Required to enable support for relative symbolic links (^5.1.4).",
"symfony/psr-http-message-bridge": "Required to use PSR-7 bridging features (^2.0).",
@ -5143,7 +5233,7 @@
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"time": "2021-04-28T14:38:56+00:00"
"time": "2021-05-11T14:00:02+00:00"
},
{
"name": "laravel/slack-notification-channel",
@ -5405,16 +5495,16 @@
},
{
"name": "league/commonmark",
"version": "1.6.0",
"version": "1.6.1",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/commonmark.git",
"reference": "19a9673b833cc37770439097b381d86cd125bfe8"
"reference": "2651c497f005de305c7ba3f232cbd87b8c00ee8c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/commonmark/zipball/19a9673b833cc37770439097b381d86cd125bfe8",
"reference": "19a9673b833cc37770439097b381d86cd125bfe8",
"url": "https://api.github.com/repos/thephpleague/commonmark/zipball/2651c497f005de305c7ba3f232cbd87b8c00ee8c",
"reference": "2651c497f005de305c7ba3f232cbd87b8c00ee8c",
"shasum": ""
},
"require": {
@ -5502,7 +5592,7 @@
"type": "tidelift"
}
],
"time": "2021-05-01T19:00:49+00:00"
"time": "2021-05-08T16:08:00+00:00"
},
{
"name": "league/flysystem",
@ -5599,6 +5689,57 @@
],
"time": "2020-08-23T07:39:11+00:00"
},
{
"name": "league/flysystem-aws-s3-v3",
"version": "1.0.29",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/flysystem-aws-s3-v3.git",
"reference": "4e25cc0582a36a786c31115e419c6e40498f6972"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/flysystem-aws-s3-v3/zipball/4e25cc0582a36a786c31115e419c6e40498f6972",
"reference": "4e25cc0582a36a786c31115e419c6e40498f6972",
"shasum": ""
},
"require": {
"aws/aws-sdk-php": "^3.20.0",
"league/flysystem": "^1.0.40",
"php": ">=5.5.0"
},
"require-dev": {
"henrikbjorn/phpspec-code-coverage": "~1.0.1",
"phpspec/phpspec": "^2.0.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"autoload": {
"psr-4": {
"League\\Flysystem\\AwsS3v3\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Frank de Jonge",
"email": "info@frenky.net"
}
],
"description": "Flysystem adapter for the AWS S3 SDK v3.x",
"support": {
"issues": "https://github.com/thephpleague/flysystem-aws-s3-v3/issues",
"source": "https://github.com/thephpleague/flysystem-aws-s3-v3/tree/1.0.29"
},
"time": "2020-10-08T18:58:37+00:00"
},
{
"name": "league/fractal",
"version": "0.19.2",
@ -6798,6 +6939,67 @@
},
"time": "2020-10-10T14:11:58+00:00"
},
{
"name": "mtdowling/jmespath.php",
"version": "2.6.0",
"source": {
"type": "git",
"url": "https://github.com/jmespath/jmespath.php.git",
"reference": "42dae2cbd13154083ca6d70099692fef8ca84bfb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/jmespath/jmespath.php/zipball/42dae2cbd13154083ca6d70099692fef8ca84bfb",
"reference": "42dae2cbd13154083ca6d70099692fef8ca84bfb",
"shasum": ""
},
"require": {
"php": "^5.4 || ^7.0 || ^8.0",
"symfony/polyfill-mbstring": "^1.17"
},
"require-dev": {
"composer/xdebug-handler": "^1.4",
"phpunit/phpunit": "^4.8.36 || ^7.5.15"
},
"bin": [
"bin/jp.php"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.6-dev"
}
},
"autoload": {
"psr-4": {
"JmesPath\\": "src/"
},
"files": [
"src/JmesPath.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
}
],
"description": "Declaratively specify how to extract elements from a JSON document",
"keywords": [
"json",
"jsonpath"
],
"support": {
"issues": "https://github.com/jmespath/jmespath.php/issues",
"source": "https://github.com/jmespath/jmespath.php/tree/2.6.0"
},
"time": "2020-07-31T21:01:56+00:00"
},
{
"name": "myclabs/php-enum",
"version": "1.8.0",