WIP: storage-fix-local-nas work before playlist controls feature

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
ghassanandClaude Sonnet 4.6 committed 2026-05-16 11:15:20 +03:00
1 parent 6b3ab5b65e
commit c160242dbc
46 files changed
+5365 -815

No files matched your search

+65 -29
View File
@@ -6,9 +6,9 @@ use App\Models\Post;
use App\Models\PostImage;
use App\Models\PostVideo;
use App\Models\User;
use App\Services\NasSyncService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
class PostController extends Controller
{
@@ -29,7 +29,6 @@ class PostController extends Controller
'images.*' => 'image|mimes:jpg,jpeg,png,webp,gif|max:8192',
'video_ids' => 'nullable|array|max:10',
'video_ids.*' => 'exists:videos,id',
// Legacy fields
'video_id' => 'nullable|exists:videos,id',
'image' => 'nullable|image|mimes:jpg,jpeg,png,webp,gif|max:8192',
]);
@@ -43,35 +42,69 @@ class PostController extends Controller
return back()->withErrors(['body' => 'Post cannot be empty.']);
}
$data = [
// Create post first — we need the ID as the folder name
$post = Post::create([
'user_id' => $user->id,
'body' => $request->body,
'video_id' => $request->video_id ?? null,
];
]);
// Legacy single image (backward compat)
if ($hasLegacyImg) {
$filename = uniqid('post_', true) . '.' . $request->file('image')->getClientOriginalExtension();
$request->file('image')->storeAs('public/post_images', $filename);
$data['image'] = $filename;
}
$nas = app(NasSyncService::class);
$nasMode = $nas->isEnabled();
$postDir = $nas->resolvePostDir($post); // "users/{slug}/posts/{id}"
$post = Post::create($data);
if ($hasImages || $hasLegacyImg) {
if ($nasMode) {
// ── NAS primary: upload directly from PHP temp files ──────────
$nas->mkdirp($postDir);
// New multi-image
if ($hasImages) {
foreach ($request->file('images') as $idx => $file) {
$filename = uniqid('post_', true) . '.' . $file->getClientOriginalExtension();
$file->storeAs('public/post_images', $filename);
PostImage::create([
'post_id' => $post->id,
'filename' => $filename,
'sort_order' => $idx,
]);
if ($hasLegacyImg) {
$file = $request->file('image');
$ext = $file->getClientOriginalExtension() ?: 'jpg';
$nasPath = "{$postDir}/0.{$ext}";
$nas->putFile($file->getRealPath(), $nasPath);
$post->update(['image' => $nasPath]);
}
if ($hasImages) {
foreach ($request->file('images') as $idx => $file) {
$ext = $file->getClientOriginalExtension() ?: 'jpg';
$nasPath = "{$postDir}/" . ($idx + 1) . ".{$ext}";
$nas->putFile($file->getRealPath(), $nasPath);
PostImage::create([
'post_id' => $post->id,
'filename' => $nasPath,
'sort_order' => $idx,
]);
}
}
} else {
// ── Local storage: save inside the user's posts directory ─────
$localDir = storage_path('app/' . $postDir);
@mkdir($localDir, 0755, true);
if ($hasLegacyImg) {
$ext = $request->file('image')->getClientOriginalExtension() ?: 'jpg';
$filename = "0.{$ext}";
$request->file('image')->move($localDir, $filename);
$post->update(['image' => "{$postDir}/{$filename}"]);
}
if ($hasImages) {
foreach ($request->file('images') as $idx => $file) {
$ext = $file->getClientOriginalExtension() ?: 'jpg';
$filename = ($idx + 1) . ".{$ext}";
$file->move($localDir, $filename);
PostImage::create([
'post_id' => $post->id,
'filename' => "{$postDir}/{$filename}",
'sort_order' => $idx,
]);
}
}
}
}
// New multi-video
if ($hasVideoIds) {
foreach ($request->input('video_ids') as $idx => $videoId) {
PostVideo::create([
@@ -91,14 +124,17 @@ class PostController extends Controller
abort(403);
}
if ($post->image) {
Storage::delete('public/post_images/' . $post->image);
$post->loadMissing('postImages');
$nas = app(NasSyncService::class);
if ($nas->isEnabled()) {
try {
$nas->deleteNasPost($post);
} catch (\Throwable) {}
}
// Delete multi-image files
foreach ($post->postImages as $postImage) {
Storage::delete('public/post_images/' . $postImage->filename);
}
// Always clean up local copies (handles both legacy flat and new structured format)
$nas->deleteLocalPostImages($post);
$post->delete();
@@ -107,7 +143,7 @@ class PostController extends Controller
public function react(Post $post)
{
$user = Auth::user();
$user = Auth::user();
$existing = $post->reactions()->where('user_id', $user->id)->first();
if ($existing) {