@@ -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 /**
0 commit comments