Skip to content

Commit 1af21af

Browse files
committed
fix(uploads): fixing uploads so the download actually works
1 parent 780666d commit 1af21af

File tree

3 files changed

+93
-21
lines changed

3 files changed

+93
-21
lines changed

app/Http/Controllers/Auth/SocialitePlusController.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace App\Http\Controllers\Auth;
44

5-
use App\Http\Controllers\Controller;
65
use App\Models\User;
7-
use Blaspsoft\SocialitePlus\SocialitePlusFactory;
86
use Illuminate\Http\Request;
7+
use Blaspsoft\SocialitePlus\SocialitePlusFactory;
98
use Illuminate\Support\Facades\Auth;
9+
use App\Http\Controllers\Controller;
1010

1111
class SocialitePlusController extends Controller
1212
{
@@ -19,6 +19,8 @@ class SocialitePlusController extends Controller
1919

2020
/**
2121
* Create a new SocialLoginController instance.
22+
*
23+
* @param SocialitePlusFactory $socialitePlus
2224
*/
2325
public function __construct(SocialitePlusFactory $socialitePlus)
2426
{
@@ -28,6 +30,7 @@ public function __construct(SocialitePlusFactory $socialitePlus)
2830
/**
2931
* Redirect to the social login page
3032
*
33+
* @param string $provider
3134
* @return \Illuminate\Http\RedirectResponse
3235
*/
3336
public function redirect(string $provider)
@@ -44,21 +47,20 @@ public function redirect(string $provider)
4447
/**
4548
* Handle the social login callback
4649
*
50+
* @param string $provider
51+
* @param Request $request
4752
* @return \Illuminate\Http\RedirectResponse
4853
*/
4954
public function callback(string $provider, Request $request)
5055
{
51-
if (! $request->has('code')) {
52-
return redirect()->route('login')->with('error', 'Invalid request');
53-
}
56+
if (!$request->has('code')) return redirect()->route('login')->with('error', 'Invalid request');
5457

5558
$user = $this->socialitePlus->build($provider)->user();
5659

5760
$existingUser = User::where('email', $user->getEmail())->first();
5861

5962
if ($existingUser) {
6063
Auth::login($existingUser);
61-
6264
return redirect()->intended('/dashboard');
6365
}
6466

app/Http/Controllers/FileUploadController.php

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,27 @@ public function store(Request $request)
3838

3939
$file = $request->file('file');
4040
$originalFilename = $file->getClientOriginalName();
41-
$filename = Str::uuid().'.'.$file->getClientOriginalExtension();
41+
$filename = Str::uuid() . '.' . $file->getClientOriginalExtension();
4242

43-
// Store the file in the storage/app/uploads directory (private)
44-
$path = $file->storeAs('uploads', $filename);
43+
// Ensure the uploads directory exists
44+
if (!Storage::exists('uploads')) {
45+
Storage::makeDirectory('uploads');
46+
Log::info('Created uploads directory on default disk');
47+
}
48+
49+
// Store the file in the private/uploads directory
50+
$path = Storage::putFileAs('uploads', $file, $filename);
51+
52+
// Log file storage details
53+
Log::info('File stored', [
54+
'original_filename' => $originalFilename,
55+
'generated_filename' => $filename,
56+
'storage_path' => $path,
57+
'full_path' => Storage::path($path),
58+
'exists' => Storage::exists($path) ? 'Yes' : 'No',
59+
'default_disk' => config('filesystems.default'),
60+
'disk_root' => config('filesystems.disks.' . config('filesystems.default') . '.root'),
61+
]);
4562

4663
// Create a new file upload record
4764
$fileUpload = FileUpload::create([
@@ -71,20 +88,63 @@ public function download(FileUpload $fileUpload)
7188
abort(403, 'You do not have permission to access this file.');
7289
}
7390

91+
// Log the download attempt
92+
Log::info('File download requested', [
93+
'file_id' => $fileUpload->id,
94+
'filename' => $fileUpload->original_filename,
95+
'path' => $fileUpload->path,
96+
'user_id' => Auth::id(),
97+
'exists' => Storage::exists($fileUpload->path) ? 'Yes' : 'No',
98+
'default_disk' => config('filesystems.default'),
99+
]);
100+
74101
// Check if the file exists
75-
if (! Storage::exists($fileUpload->path)) {
76-
abort(404, 'File not found.');
102+
if (!Storage::exists($fileUpload->path)) {
103+
// Try to check where the file might be
104+
$alternativePaths = [
105+
'uploads/' . $fileUpload->filename,
106+
'private/uploads/' . $fileUpload->filename,
107+
'app/uploads/' . $fileUpload->filename,
108+
'app/private/uploads/' . $fileUpload->filename,
109+
];
110+
111+
$foundAlternative = false;
112+
foreach ($alternativePaths as $altPath) {
113+
if (Storage::exists($altPath)) {
114+
Log::info('Found file at alternative path', ['path' => $altPath]);
115+
$fileUpload->path = $altPath;
116+
$fileUpload->save();
117+
$foundAlternative = true;
118+
break;
119+
}
120+
}
121+
122+
if (!$foundAlternative) {
123+
Log::error('File not found for download', [
124+
'file_id' => $fileUpload->id,
125+
'path' => $fileUpload->path,
126+
'checked_alternatives' => $alternativePaths,
127+
]);
128+
129+
abort(404, 'File not found. It may have been moved or deleted.');
130+
}
77131
}
78132

79-
// Debugging information
80-
Log::info('Download requested by User: '.Auth::id().' for File: '.$fileUpload->id.' Path: '.$fileUpload->path);
81-
82-
// Force download the file
83-
return response()->download(
84-
storage_path('app/'.$fileUpload->path),
85-
$fileUpload->original_filename,
86-
['Content-Type' => $fileUpload->mime_type]
87-
);
133+
try {
134+
// Return the file as a download
135+
return Storage::download(
136+
$fileUpload->path,
137+
$fileUpload->original_filename,
138+
['Content-Type' => $fileUpload->mime_type]
139+
);
140+
} catch (\Exception $e) {
141+
Log::error('Error downloading file', [
142+
'file_id' => $fileUpload->id,
143+
'error' => $e->getMessage(),
144+
]);
145+
146+
abort(500, 'Error downloading file. Please try again later.');
147+
}
88148
}
89149

90150
/**

app/Models/FileUpload.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
use Illuminate\Support\Facades\Log;
89

910
class FileUpload extends Model
1011
{
@@ -25,6 +26,13 @@ class FileUpload extends Model
2526
'status',
2627
];
2728

29+
/**
30+
* The accessors to append to the model's array form.
31+
*
32+
* @var array
33+
*/
34+
protected $appends = ['url'];
35+
2836
/**
2937
* Get the user that owns the file upload.
3038
*/
@@ -38,6 +46,8 @@ public function user(): BelongsTo
3846
*/
3947
public function getUrlAttribute(): string
4048
{
41-
return route('content.download', $this->id);
49+
$url = route('content.download', $this->id);
50+
51+
return $url;
4252
}
4353
}

0 commit comments

Comments
 (0)