Skip to content

A self-hosted media server and file management system

experiment with private media folder and temporary urls (commented out)

aminnausinad3f4bb19507
diff --git a/app/Http/Controllers/MediaController.php b/app/Http/Controllers/MediaController.php
new file mode 100644
index 00000000..0ebde6a1
--- /dev/null
+++ b/app/Http/Controllers/MediaController.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use Illuminate\Support\Facades\Response;
+use Illuminate\Support\Facades\Storage;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Log;
+
+class MediaController extends Controller {
+ public function show(Request $request, $path) {
+ Log::info('Request path: ' . $path);
+ Log::info('Request full URL: ' . $request->fullUrl());
+ if (!$request->hasValidSignature()) {
+ Log::warning('Invalid signature for URL: ' . $request->fullUrl());
+ abort(403);
+ }
+
+ $fullPath = storage_path("app/private/{$path}");
+ if (file_exists($fullPath)) {
+ $mimeType = Storage::mimeType("private/{$path}");
+
+ return response()->file($fullPath, [
+ 'Content-Type' => $mimeType,
+ 'Content-Disposition' => 'inline; filename="' . basename($fullPath) . '"',
+ ]);
+ } else {
+ abort(404);
+ }
+
+ // $path = 'private/' . $path; // Adjust the path as needed
+ // if (Storage::exists($path)) {
+ // // Prevent direct download by setting appropriate headers
+ // // return Storage::temporaryUrl(
+ // // $path,
+ // // now()->addHours(5)
+ // // );
+ // $fileContent = Storage::get($path);
+ // $mimeType = Storage::mimeType($path);
+
+ // return Response::make($fileContent, 200, [
+ // 'Content-Type' => $mimeType,
+ // 'Content-Disposition' => 'inline; filename="' . basename($path) . '"',
+ // ]);
+ // } else {
+ // abort(403);
+ // }
+ }
+}
diff --git a/app/Jobs/SyncFiles.php b/app/Jobs/SyncFiles.php
index 8b9ea9ec..501016fa 100755
--- a/app/Jobs/SyncFiles.php
+++ b/app/Jobs/SyncFiles.php
@@ -14,23 +14,20 @@ use App\Models\Folder;
use App\Models\Video;
use Illuminate\Bus\Batchable;
-class SyncFiles implements ShouldQueue, ShouldBeUnique
-{
+class SyncFiles implements ShouldQueue, ShouldBeUnique {
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
- public function __construct()
- {
+ public function __construct() {
//
}
/**
* Execute the job.
*/
- public function handle(): void
- {
+ public function handle(): void {
if ($this->batch() && $this->batch()->cancelled()) {
// Determine if the batch has been cancelled...
return;
@@ -39,15 +36,15 @@ class SyncFiles implements ShouldQueue, ShouldBeUnique
$this->syncCache();
}
- public function syncCache()
- {
- // Idea: Compare categories folders and videos json files with data on sql server. Sync local copies with sql server if this is master storage (ie all files should be available)
+ public function syncCache() {
+ // Idea: Compare categories folders and videos json files with data on sql server. Sync local copies with sql server if this is master storage (ie all files should be available)
// -> then if you index files, it should delete sql entries correctly if anything there does not exist locally
$path = "public/media/";
+ // $path = "private/media/";
if (!Storage::exists($path)) {
- $error = 'Invalid Directory: "media"';
+ $error = 'Missing "media" directory in storage';
dd(json_encode(array("success" => false, "result" => "", "error" => $error), JSON_UNESCAPED_SLASHES));
}
@@ -75,8 +72,7 @@ class SyncFiles implements ShouldQueue, ShouldBeUnique
if (!$this->batch()) dump('Categories | Folders | Videos | Data | dataCache', $directories, $subDirectories, $files, $data, $dataCache);
}
- private function generateCategories()
- {
+ private function generateCategories() {
$data = Storage::json('public/categories.json') ?? array("next_ID" => 1, "categoryStructure" => array()); //array("anime"=>1,"tv"=>2,"yogscast"=>3); // read from json
$scanned = Category::all(); // read folder structure
@@ -117,8 +113,7 @@ class SyncFiles implements ShouldQueue, ShouldBeUnique
return array("categoryChanges" => $changes, "data" => $data);
}
- private function generateFolders($path)
- {
+ private function generateFolders($path) {
$data = Storage::json('public/folders.json') ?? array("next_ID" => 1, "folderStructure" => array()); //array("anime/frieren"=>array("id"=>0,"name"=>"frieren"),"starwars/andor"=>array("id"=1,"name"="andor")); // read from json
$cost = 0;
$scanned = Folder::all();
@@ -162,8 +157,7 @@ class SyncFiles implements ShouldQueue, ShouldBeUnique
return array("folderChanges" => $changes, "data" => $data, "cost" => $cost);
}
- private function generateVideos($path, $folderStructure)
- {
+ private function generateVideos($path, $folderStructure) {
$data = Storage::json('public/videos.json') ?? array("next_ID" => 1, "videoStructure" => array()); //array("anime/frieren/S1E01.mp4"=>array("id"=>0,"name"=>"S1E01"),"starwars/andor/S1E01.mkv"=>array("id"=1,"name"="S1E01.mkv")); // read from json
$scanned = Video::all();
$cost = 0;
diff --git a/app/Jobs/VerifyFiles.php b/app/Jobs/VerifyFiles.php
index dccbf2df..50506cd2 100755
--- a/app/Jobs/VerifyFiles.php
+++ b/app/Jobs/VerifyFiles.php
@@ -67,6 +67,7 @@ class VerifyFiles implements ShouldQueue {
$compositeId = $video->folder->path . "/" . basename($video->path);
$filePath = str_replace('\\', '/', Storage::path('')) . 'public/media/' . $video->folder->path . "/" . basename($video->path);
+ // $filePath = str_replace('\\', '/', Storage::path('app/private/')) . 'media/' . $video->folder->path . "/" . basename($video->path);
$fileMetaData = is_null($video->uuid) ? $this->getFileMetadata($filePath) : []; // Empty unless uuid is missing or duration is missing
$uuid = $video->uuid ?? ''; // video has ? file has
diff --git a/bootstrap/app.php b/bootstrap/app.php
index 037e17df..86d2c54c 100755
--- a/bootstrap/app.php
+++ b/bootstrap/app.php
@@ -1,5 +1,22 @@
<?php
+// use Illuminate\Foundation\Application;
+// use Illuminate\Foundation\Configuration\Exceptions;
+// use Illuminate\Foundation\Configuration\Middleware;
+
+// return Application::configure(basePath: dirname(__DIR__))
+// ->withRouting(
+// web: __DIR__.'/../routes/web.php',
+// commands: __DIR__.'/../routes/console.php',
+// health: '/up',
+// )
+// ->withMiddleware(function (Middleware $middleware) {
+// //
+// })
+// ->withExceptions(function (Exceptions $exceptions) {
+// //
+// })->create();
+
/*
|--------------------------------------------------------------------------
| Create The Application
diff --git a/config/filesystems.php b/config/filesystems.php
index e9d9dbdb..7e538871 100755
--- a/config/filesystems.php
+++ b/config/filesystems.php
@@ -34,16 +34,23 @@ return [
'driver' => 'local',
'root' => storage_path('app'),
'throw' => false,
+ // 'serve' => true,
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
- 'url' => env('APP_URL').'/storage',
+ 'url' => env('APP_URL') . '/storage',
'visibility' => 'public',
'throw' => false,
],
+ // 'private' => [
+ // 'driver' => 'local',
+ // 'root' => storage_path('app/private'),
+ // 'throw' => false,
+ // ],
+
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
diff --git a/routes/web.php b/routes/web.php
index b3164c69..50f7d007 100755
--- a/routes/web.php
+++ b/routes/web.php
@@ -2,6 +2,8 @@
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DirectoryController;
+use App\Http\Controllers\MediaController;
+use Illuminate\Support\Facades\URL;
/*
|--------------------------------------------------------------------------
@@ -26,12 +28,23 @@ Route::middleware('auth')->group(function () {
// public
-Route::get('php', function () {
- phpinfo();
-})->name('php');
+// Route::get('php', function () {
+// phpinfo();
+// })->name('php');->middleware('auth')->
+
+// Route::get('/storage/{path}', [MediaController::class, 'show'])->where('path', '.*')->name('media.serve');
+// Route::get('/signed-url/{path}', function ($path) {
+// return URL::temporarySignedRoute(
+// 'media.serve',
+// now()->addSeconds(5), // URL is valid for 5 minutes
+// ['path' => $path]
+// );
+// })->middleware('auth')->where('path', '.*');
+
Route::get('/welcome', function () {
return view('welcome');
});
+
Route::get('/{dir?}/{folderName?}', function () {
return view('home');
})->name('root');