diff --git a/src/Download.php b/src/Download.php index 83fa164..72dba47 100644 --- a/src/Download.php +++ b/src/Download.php @@ -2,27 +2,31 @@ namespace DevsWebDev\DevTube; +use Illuminate\Support\Facades\Storage; use Masih\YoutubeDownloader\YoutubeDownloader; class Download { - protected $youtube; + public $youtube; - protected $path; + public $path; - public function __construct($url, $path) - { - // dd($url); - $this->youtube = new YoutubeDownloader($url); + public $savedPath; - $this->path = $path ?? storage_path("/"); - $this->youtube->setPath($this->path); + public function __construct($url, $path) + { + $this->url = $url; + $this->path = storage_path("app/public/music"); } public function download() { - $this->youtube->onProgress = function ($downloadedBytes, $fileSize, $index, $count) { + $youtube = new YoutubeDownloader($this->url); + $youtube->setPath($this->path); + $this->youtube = $youtube; + + $youtube->onProgress = function ($downloadedBytes, $fileSize, $index, $count) { if ($count > 1) { // echo '[' . $index . ' of ' . $count . ' videos] '; } @@ -33,24 +37,32 @@ public function download() } }; - $this->youtube->onFinalized = function ($filePath, $fileSize, $index, $count) { - if ($count > 1) { - // echo '[' . $index . ' of ' . $count . ' videos] '; - } - // echo $filePath . ' Finalized' . PHP_EOL; + $youtube->onComplete = function ($filePath, $fileSize, $index, $count) { + return $this->redirect($filePath); }; - $this->youtube->onComplete = function ($filePath, $fileSize, $index, $count) { - if ($count > 1) { - // echo '[' . $index . ' of ' . $count . ' videos] '; - } - // echo 'Downloading of ' . $fileSize . ' bytes has been completed. It is saved in ' . $filePath . PHP_EOL; - }; + $youtube->download(); + } + + public function redirect($filePath) + { + $youtube = $this->youtube; + $file = basename($filePath); + + // $files = \Storage::files('public/music'); + // dump($files); + session([$_SERVER['REMOTE_ADDR'] => 'app/public/music/'.$file]); - return response()->streamDownload(function () { - $this->youtube->download(); - }, 'laravel-readme.mp4'); - // return view('welcome') ; + // \Session::put('public/music/'.$file:'public/music/'.$file) + // dump('public/music/'.$file); + // $dl = Storage::download('public/music/'.$file); + // // return $dl; + // dump($dl); + // $contents = Storage::get('public/music/'.$file); + // // return json_encode($contents); + // // dump($contents); + // $url = \Storage::url('public/music/'.$file); + // echo $url; } }