summaryrefslogtreecommitdiff
path: root/resources/views/f.blade.php
blob: 4ba18d9a13f8a4a74756df05aac81009929367a8 (plain)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
@extends('template')
<?php
    //THIS IS GENERATED FROM CLAUDE, as a modification of the original f.blade.php
    
    $baseDir = 'storage/uploads/'; 
    $currentDir = $baseDir;

    // Directory handling
    if (isset($_GET['dir'])) {
        $dir = $_GET['dir'];
        if (strpos($dir, '..') !== false || strpos($dir, '/') !== false || strpos($dir, '\\') !== false) {
            echo 'Access Denied';
            exit;
        }
        $dir = basename($dir); // Ensuring the folder name is isolated
        if (!is_dir("${baseDir}/${dir}")) {
            echo 'Directory not found';
            exit;
        }
        $currentDir = "${baseDir}/${dir}";
    }

    // File download handling
    if (isset($_GET['file'])) {
        $fileRequested = $_GET['file'];
        $filePath = realpath($fileRequested);

        if (file_exists($filePath) && is_file($filePath)) {
            // Set headers to force download
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="' . basename($filePath) . '"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($filePath));
            readfile($filePath);
            exit;
        } else {
            echo "File not found.";
            exit;
        }
    }

    // Extracting the name of the current directory
    $currentDirName = basename($currentDir);

    // Determine the depth of the current directory relative to the base directory
    $depth = substr_count(str_replace($baseDir, '', $currentDir), '/');

    // Get list of files and directories
    $contents = scandir($currentDir);

    // Prepare file data with additional information
    $fileData = [];
    foreach ($contents as $item) {
        if ($item !== "." && $item !== "..") {
            $path = $currentDir . '/' . $item;
            $isDir = is_dir($path);
            
            // Get file information
            $size = $isDir ? 0 : filesize($path);
            $modified = filemtime($path);
            $extension = $isDir ? '' : pathinfo($item, PATHINFO_EXTENSION);
            
            $fileData[] = [
                'name' => $item,
                'path' => $path,
                'is_dir' => $isDir,
                'size' => $size,
                'size_formatted' => $isDir ? '-' : formatFileSize($size),
                'modified' => $modified,
                'modified_formatted' => date('Y-m-d H:i:s', $modified),
                'extension' => $extension
            ];
        }
    }

    // Sort function
    function sortFiles($files, $sortBy = 'name', $sortOrder = 'asc') {
        usort($files, function($a, $b) use ($sortBy, $sortOrder) {
            // Directories always come first
            if ($a['is_dir'] && !$b['is_dir']) {
                return -1;
            }
            if (!$a['is_dir'] && $b['is_dir']) {
                return 1;
            }
            
            // Compare values based on sort field
            $compareA = $a[$sortBy];
            $compareB = $b[$sortBy];
            
            // Sort alphabetically for strings
            if (is_string($compareA)) {
                $result = strcasecmp($compareA, $compareB);
            } else {
                // Sort numerically for numbers
                $result = $compareA <=> $compareB;
            }
            
            // Apply sort order
            return $sortOrder === 'asc' ? $result : -$result;
        });
        
        return $files;
    }

    // Format file size
    function formatFileSize($bytes) {
        $units = ['B', 'KB', 'MB', 'GB', 'TB'];
        $bytes = max($bytes, 0);
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
        $pow = min($pow, count($units) - 1);
        
        $bytes /= (1 << (10 * $pow));
        
        return round($bytes, 2) . ' ' . $units[$pow];
    }

    // Get sort parameters
    $sortBy = isset($_GET['sort']) && in_array($_GET['sort'], ['name', 'size', 'modified']) 
        ? $_GET['sort'] : 'name';
    $sortOrder = isset($_GET['order']) && $_GET['order'] === 'desc' ? 'desc' : 'asc';
    
    // Apply sorting
    $fileData = sortFiles($fileData, $sortBy, $sortOrder);
?>
@section('head')
<style>
    .file-list {
        width: 100%;
        border-collapse: collapse;
    }
    .file-list th, .file-list td {
        padding: 8px 12px;
        text-align: left;
        border-bottom: 1px solid #e1e1e1;
    }
    .file-list th {
        cursor: pointer;
    }
    .file-list th:hover {
        background-color: #f2f2f2;
    }
    .file-list tr:hover {
        background-color: #f9f9f9;
    }
    .file-list .sorting-icon::after {
        content: "↓";
        margin-left: 5px;
        font-size: 12px;
    }
    .file-list .sorting-icon.desc::after {
        content: "↑";
    }
    .dir-icon {
        margin-right: 5px;
        color: #5b6f9d;
    }
    .file-icon {
        margin-right: 5px;
        color: #7d7d7d;
    }
    .file-tag {
        display: inline-block;
        font-size: 12px;
        padding: 2px 6px;
        margin: 2px;
        background-color: #e1ebf7;
        border-radius: 3px;
    }
    .breadcrumb {
        padding: 10px 0;
        margin-bottom: 20px;
    }
    .breadcrumb a {
        text-decoration: none;
        color: #4a69bd;
    }
    .breadcrumb a:hover {
        text-decoration: underline;
    }
    .breadcrumb span {
        margin: 0 5px;
    }
    .file-actions {
        white-space: nowrap;
    }
    .action-btn {
        display: inline-block;
        padding: 4px 8px;
        margin: 0 2px;
        border-radius: 3px;
        font-size: 12px;
        text-decoration: none;
    }
    .download-btn {
        background-color: #4a69bd;
        color: white;
    }
    .tag-btn {
        background-color: #f2f2f2;
        color: #333;
    }
    .action-btn:hover {
        opacity: 0.9;
    }
    .main-actions {
        margin-bottom: 20px;
    }
    .upload-form {
        margin-top: 20px;
        padding: 15px;
        border-radius: 5px;
    }
</style>
@endsection

@section('body')
<main>
    <h1>{{ htmlspecialchars(ucfirst($currentDirName)) }}</h1>
    
    <!-- Breadcrumb Navigation -->
    <div class="breadcrumb">
        <a href="/f">Home</a>
        @if ($currentDir != $baseDir)
            <span>/</span>
            <a href="/f?dir={{ $currentDirName }}">{{ htmlspecialchars($currentDirName) }}</a>
        @endif
    </div>
    
    <!-- Main Actions -->
    <div class="main-actions">
        <a href="#upload-section" class="action-btn download-btn">Upload File</a>
        @if ($currentDir != $baseDir)
            <a href="/f" class="action-btn tag-btn">Go to Root Directory</a>
        @endif
    </div>

    <!-- Upload Section -->
    <section>
    <div id="upload-section" class="upload-form">
        <h2>Upload Files</h2>
        <form action="/f" method="POST" enctype="multipart/form-data">
            @csrf
            @error('f')  
                <div class="error-message">{{ $message }}</div>  
            @enderror
            
            <div style="margin-bottom: 15px;">
                <input multiple name="f[]" type="file" id="file-input">
            </div>
            
            <div style="margin-bottom: 15px;">
                <label for="tags">Tags (comma separated):</label>
                <input type="text" id="tags" name="tags" placeholder="e.g. work, document, important">
            </div>
            
            <input hidden name="response_format" value="html" />
            <input hidden name="current_dir" value="{{ $currentDir }}" />
            
            <button type="submit" class="action-btn download-btn">Upload</button>
        </form>
    </div>
    </section>


    <!-- File Table -->
    <section>
    <table class="file-list">
        <thead>
            <tr>
                <th>
                    <a href="/f?dir={{ isset($_GET['dir']) ? $_GET['dir'] : '' }}&sort=name&order={{ ($sortBy === 'name' && $sortOrder === 'asc') ? 'desc' : 'asc' }}">
                        Name
                        @if ($sortBy === 'name')
                            <span class="sorting-icon {{ $sortOrder }}"></span>
                        @endif
                    </a>
                </th>
                <th>
                    <a href="/f?dir={{ isset($_GET['dir']) ? $_GET['dir'] : '' }}&sort=size&order={{ ($sortBy === 'size' && $sortOrder === 'asc') ? 'desc' : 'asc' }}">
                        Size
                        @if ($sortBy === 'size')
                            <span class="sorting-icon {{ $sortOrder }}"></span>
                        @endif
                    </a>
                </th>
                <th>
                    <a href="/f?dir={{ isset($_GET['dir']) ? $_GET['dir'] : '' }}&sort=modified&order={{ ($sortBy === 'modified' && $sortOrder === 'asc') ? 'desc' : 'asc' }}">
                        Last Modified
                        @if ($sortBy === 'modified')
                            <span class="sorting-icon {{ $sortOrder }}"></span>
                        @endif
                    </a>
                </th>
                <th>Tags</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            @if ($currentDir != $baseDir)
                <tr>
                    <td colspan="5">
                        <a href="/f{{ substr_count($currentDir, '/') > 1 ? '?dir=' . dirname(str_replace($baseDir . '/', '', $currentDir)) : '' }}">
                            <span class="dir-icon">📁</span> ../ (Go Up)
                        </a>
                    </td>
                </tr>
            @endif
            
            @foreach ($fileData as $item)
                <tr>
                    <td>
                        @if ($item['is_dir'])
                            <span class="dir-icon">📁</span>
                            <a href="/f?dir={{ $item['name'] }}">{{ htmlspecialchars($item['name']) }}</a>
                        @else
                            <span class="file-icon">📄</span>
                            <a href="/f/{{ $item['name'] }}">{{ htmlspecialchars($item['name']) }}</a>
                        @endif
                    </td>
                    <td>{{ $item['size_formatted'] }}</td>
                    <td>{{ $item['modified_formatted'] }}</td>
                    <td>
                        <?php
                        // In a real implementation, you would fetch tags for each file from database
                        // This is just a placeholder for the example
                        $dummyTags = ['document', 'important', 'work'];
                        if (!$item['is_dir'] && mt_rand(0, 2) > 0) {
                            $numTags = mt_rand(1, 3);
                            $shuffled = $dummyTags;
                            shuffle($shuffled);
                            $fileTags = array_slice($shuffled, 0, $numTags);
                            
                            foreach ($fileTags as $tag) {
                                echo '<span class="file-tag">' . $tag . '</span>';
                            }
                        }
                        ?>
                    </td>
                    <td class="file-actions">
                        @if (!$item['is_dir'])
                            <a href="?file={{ $item['path'] }}" class="action-btn download-btn">Download</a>
                            <a href="#" class="action-btn tag-btn" onclick="promptForTag('{{ $item['name'] }}'); return false;">Tag</a>
                        @endif
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>
    </section>
</main>

<script>
    function promptForTag(fileName) {
        const tag = prompt("Enter tags for " + fileName + " (comma separated):", "");
        if (tag !== null && tag !== "") {
            // In a real implementation, you would send an AJAX request to save the tags
            alert("Tags added: " + tag + " (This is just a demo)");
        }
    }
</script>
@endsection