1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
<?php
namespace App\Http\Controllers;
use App\Models\File;
use App\Models\Tag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
class FileController extends Controller
{
/**
* Display a listing of files.
*/
public function index(Request $request)
{
$baseDir = 'storage/uploads/';
$currentDir = $baseDir;
// Directory handling logic (similar to f.blade.php)
if ($request->has('dir')) {
$dir = $request->dir;
// Security checks...
$currentDir = "${baseDir}/${dir}";
}
// Get file listings
$contents = scandir($currentDir);
$fileData = [];
foreach ($contents as $item) {
if ($item !== "." && $item !== "..") {
$path = $currentDir . '/' . $item;
$isDir = is_dir($path);
if (!$isDir) {
// Try to find file in database to get tags
$fileModel = File::where('filename', $item)->first();
$tags = $fileModel ? $fileModel->tags : collect();
} else {
$tags = collect();
}
// Build file info
$fileData[] = [
'name' => $item,
'path' => $path,
'is_dir' => $isDir,
'size' => $isDir ? 0 : filesize($path),
'modified' => filemtime($path),
'extension' => $isDir ? '' : pathinfo($item, PATHINFO_EXTENSION),
'tags' => $tags
];
}
}
// Get all available tags for the tagging UI
$allTags = Tag::orderBy('label')->get();
return view('files.index', compact('fileData', 'allTags', 'currentDir'));
}
/**
* Store a newly created file in storage.
*/
public function store(Request $request)
{
$dir = 'uploads';
if ($request->has('current_dir')) {
// Extract relative directory path if provided
$dir = str_replace('storage/', '', $request->current_dir);
}
// Validate files
$maxsize = config('filesystems.max_upload_size', 10240); // Default to 10MB
$validated = $request->validate([
'f' => "required|array|max:${maxsize}",
'f.*' => "required|file|max:${maxsize}"
]);
$results = [
'num_files' => count($request->file('f')),
'num_uploaded' => 0,
'files' => []
];
// Process tags
$tags = [];
if ($request->has('tags') && !empty($request->tags)) {
$tags = array_map('trim', explode(',', $request->tags));
}
foreach ($request->file('f') as $file) {
$filename = $file->getClientOriginalName();
// Avoid duplicate filenames by adding timestamp
if (Storage::disk('public')->exists("${dir}/${filename}")) {
$filename = Carbon::now()->timestamp . '_' . $filename;
}
// Store the file
$path = $file->storeAs($dir, $filename, 'public');
if ($path) {
// Create file record in database
$fileModel = File::create([
'filename' => $filename,
'path' => $path,
'source' => $request->ip(),
'description' => '',
'md5' => md5_file($file->getRealPath()),
'size' => $file->getSize(),
'mime_type' => $file->getMimeType(),
'user_id' => Auth::id()
]);
// Add tags if provided
if (!empty($tags)) {
$fileModel->addTags($tags);
}
$results['num_uploaded']++;
$results['files'][] = [
'filename' => $filename,
'path' => $path,
'id' => $fileModel->id
];
}
}
// Return based on requested response format
if ($request->input('response_format') === 'html') {
return redirect()->back()->with('success', "{$results['num_uploaded']} files uploaded successfully");
}
return response()->json($results);
}
/**
* Add tags to an existing file
*/
public function addTags(Request $request, $fileId)
{
$file = File::findOrFail($fileId);
$validated = $request->validate([
'tags' => 'required|string'
]);
$tags = array_map('trim', explode(',', $validated['tags']));
$file->addTags($tags);
if ($request->wantsJson()) {
return response()->json([
'success' => true,
'tags' => $file->tags
]);
}
return redirect()->back()->with('success', 'Tags added successfully');
}
/**
* Remove a tag from a file
*/
public function removeTag(Request $request, $fileId, $tagId)
{
$file = File::findOrFail($fileId);
$tag = Tag::findOrFail($tagId);
$file->tags()->detach($tagId);
$tag->decrement('refs');
if ($request->wantsJson()) {
return response()->json([
'success' => true
]);
}
return redirect()->back()->with('success', 'Tag removed');
}
/**
* Display the specified file info
*/
public function show(File $file)
{
return view('files.show', compact('file'));
}
/**
* Update the specified file's info
*/
public function update(Request $request, File $file)
{
$validated = $request->validate([
'description' => 'nullable|string',
'tags' => 'nullable|string'
]);
$file->description = $validated['description'] ?? $file->description;
$file->save();
if (isset($validated['tags'])) {
$tags = array_map('trim', explode(',', $validated['tags']));
$file->tags()->detach();
$file->addTags($tags);
}
return redirect()->back()->with('success', 'File updated successfully');
}
/**
* Remove the specified file
*/
public function destroy(File $file)
{
// Remove the file from storage
if (Storage::disk('public')->exists($file->path)) {
Storage::disk('public')->delete($file->path);
}
// Delete from database (will auto detach tags)
$file->delete();
return redirect()->route('files.index')->with('success', 'File deleted successfully');
}
/**
* Download a file
*/
public function download($filename)
{
$file = File::where('filename', $filename)->first();
if (!$file) {
// Try to find file in storage even if not in DB
$path = 'uploads/' . $filename;
if (Storage::disk('public')->exists($path)) {
return Storage::disk('public')->download($path);
}
abort(404, 'File not found');
}
return Storage::disk('public')->download($file->path);
}
/**
* List files by tag
*/
public function byTag($tagId)
{
$tag = Tag::findOrFail($tagId);
$files = $tag->files()->paginate(20);
return view('files.by-tag', compact('tag', 'files'));
}
}
|