Skip to content

Commit ac0e11a

Browse files
committed
improved filebrowser and uploads
1 parent 370e5af commit ac0e11a

File tree

6 files changed

+129
-51
lines changed

6 files changed

+129
-51
lines changed

acp/core/uploads/data-reader.php

+50
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,47 @@
55
$reader_uri = '/admin/uploads/read/';
66

77

8+
if($_REQUEST['action'] == 'list_active_searches') {
9+
10+
if(isset($_SESSION['uploads_text_filter']) AND $_SESSION['uploads_text_filter'] != "") {
11+
unset($all_filter);
12+
$all_filter = explode(" ", $_SESSION['uploads_text_filter']);
13+
14+
foreach($all_filter as $f) {
15+
if($_REQUEST['rm_keyword'] == "$f") { continue; }
16+
if($f == "") { continue; }
17+
$btn_remove_keyword .= '<button class="btn btn-sm btn-default" name="rmkey" value="'.$f.'" hx-post="/admin/uploads/write/" hx-swap="none" hx-include="[name=\'csrf_token\']">'.$icon['x'].' '.$f.'</button> ';
18+
}
19+
}
20+
21+
if(isset($btn_remove_keyword)) {
22+
echo '<div class="d-inline">';
23+
echo '<p style="padding-top:5px;">' . $btn_remove_keyword . '</p>';
24+
echo '</div><hr>';
25+
}
26+
}
27+
28+
if($_REQUEST['action'] == 'show_stats') {
29+
30+
$media_cnt_images = $db_content->count("se_media",[
31+
"media_file[~]" => "../images/"
32+
]);
33+
34+
$media_cnt_files = $db_content->count("se_media",[
35+
"media_file[~]" => "../files/"
36+
]);
37+
38+
echo '<table class="table">';
39+
echo '<tr>';
40+
echo '<td>Images</td><td>'.$media_cnt_images.'</td>';
41+
echo '</tr>';
42+
echo '<tr>';
43+
echo '<td>Files</td><td>'.$media_cnt_files.'</td>';
44+
echo '</tr>';
45+
echo '</table>';
46+
}
47+
48+
849
if($_REQUEST['action'] == 'list') {
950

1051
// defaults
@@ -113,4 +154,13 @@
113154

114155
echo '</div>';
115156

157+
if($_SESSION['disk'] != 'assets/images' AND $_SESSION['disk'] != 'assets/files' AND $_SESSION['disk'] != '') {
158+
$delete_dir_btn = '<form hx-post="/admin/uploads/write/" hx-confirm="' . $lang['msg_confirm_delete_directory'] . '" hx-target="#response" class="mt-3 text-end">';
159+
$delete_dir_btn .= '<button name="delete_dir" value="' . $_SESSION['disk'] . '" class="btn btn-danger">';
160+
$delete_dir_btn .= $icon['trash_alt'] . ' ' . $_SESSION['disk'];
161+
$delete_dir_btn .= '</button>';
162+
$delete_dir_btn .= '<input type="hidden" name="csrf_token" value="' . $_SESSION['token'] . '">';
163+
$delete_dir_btn .= '</form>';
164+
echo $delete_dir_btn;
165+
}
116166
}

acp/core/uploads/data-writer.php

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
require_once __DIR__.'/functions.php';
34

45
/**
56
* delete media
@@ -103,20 +104,27 @@
103104

104105
// change directory
105106
if(isset($_POST['selected_folder'])) {
107+
echo 'Swapped folder';
106108
$_SESSION['disk'] = se_filter_filepath($_POST['selected_folder']);
107109
header( "HX-Trigger: update_uploads_list");
108110
}
109111

110-
/**
111-
* create new folder
112-
*/
112+
// create new folder
113113
if((isset($_POST['new_folder'])) && ($_POST['new_folder'] != '')) {
114114
$folder_name = clean_filename($_POST['new_folder']);
115115
$create_path = $_SESSION['disk'] . '/' . $folder_name;
116116
mkdir($create_path, 0777, true);
117117
header( "HX-Trigger: update_directories");
118118
}
119119

120+
// delete folder
121+
if(isset($_POST['delete_dir'])) {
122+
$del_dir = se_filter_filepath($_POST['delete_dir']);
123+
delete_folder($del_dir);
124+
header( "HX-Trigger: update_directories, update_uploads_list");
125+
}
126+
127+
120128
/**
121129
* rebase the database
122130
* we check if there are files which are not in the database
@@ -138,6 +146,16 @@
138146
$scan_files = se_scandir_recursive("$files_dir");
139147
$images_and_files = array_merge($scan_images, $scan_files);
140148

149+
$cnt_images_on_disk = 0;
150+
if(is_array($scan_images)) {
151+
$cnt_images_on_disk = count($scan_images);
152+
}
153+
154+
$cnt_files_on_disk = 0;
155+
if(is_array($scan_files)) {
156+
$cnt_files_on_disk = count($scan_files);
157+
}
158+
141159
foreach ($images_and_files as $key => $value) {
142160
if(str_contains("$value","index.html")) { continue; }
143161
$all_files[] = str_replace('assets/', '../', $value);
@@ -150,8 +168,7 @@
150168

151169
foreach($all_files as $filename) {
152170
if(!in_array($filename, $mediaData)) {
153-
// filename is not in database, mak an entry
154-
171+
// filename is not in database, make an entry
155172
$file_src = str_replace("../","assets/",$filename);
156173
$filesize = filesize($file_src);
157174
$filemtime = filemtime($file_src);
@@ -183,9 +200,10 @@
183200
}
184201
}
185202

186-
203+
echo '<p>Found <code>'.$cnt_images_on_disk.'</code> images and <code>'.$cnt_files_on_disk.'</code> files.</p>';
187204
echo '<p><code>'.$stats_files_to_db.'</code> were added to the database<br>';
188205
echo '<code>'.$stats_files_from_db.'</code> were removed from the database</p>';
206+
header( "HX-Trigger: update_uploads_list");
189207

190208
}
191209

acp/core/uploads/functions.php

+21
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,25 @@ function show_sort_arrow() {
88
$ic = '<span title="'.$lang['descending'].'"><i class="bi bi-caret-down-fill"></i></span>';
99
}
1010
return $ic;
11+
}
12+
13+
function delete_folder($dir) {
14+
15+
$dir = se_filter_filepath($dir);
16+
17+
$delete_folder = SE_PUBLIC.'/'.$dir;
18+
$files = array_diff(scandir($delete_folder), array('.','..'));
19+
foreach ($files as $file) {
20+
if(is_dir("$dir/$file")) {
21+
delete_folder("$dir/$file");
22+
} else {
23+
unlink("$dir/$file");
24+
$filename = $dir.'/'.$file;
25+
$filename = str_replace(SE_PUBLIC, '', $filename);
26+
$filename = str_replace('assets/', '../', $filename);
27+
se_delete_media_data("$filename");
28+
}
29+
30+
}
31+
return rmdir($delete_folder);
1132
}

acp/core/uploads/index.php

+5-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
$reader_uri = '/admin/uploads/read/';
44
$writer_uri = '/admin/uploads/write/';
55

6-
include 'functions.php';
6+
77

88
$q = pathinfo($_REQUEST['query']);
99

@@ -51,22 +51,7 @@
5151
echo '</div>';
5252
echo '</form>';
5353

54-
if(isset($_SESSION['uploads_text_filter']) AND $_SESSION['uploads_text_filter'] != "") {
55-
unset($all_filter);
56-
$all_filter = explode(" ", $_SESSION['uploads_text_filter']);
57-
58-
foreach($all_filter as $f) {
59-
if($_REQUEST['rm_keyword'] == "$f") { continue; }
60-
if($f == "") { continue; }
61-
$btn_remove_keyword .= '<button class="btn btn-sm btn-default" name="rmkey" value="'.$f.'" hx-post="'.$writer_uri.'" hx-swap="none" hx-include="[name=\'csrf_token\']">'.$icon['x'].' '.$f.'</button> ';
62-
}
63-
}
64-
65-
if(isset($btn_remove_keyword)) {
66-
echo '<div class="d-inline">';
67-
echo '<p style="padding-top:5px;">' . $btn_remove_keyword . '</p>';
68-
echo '</div><hr>';
69-
}
54+
echo '<div class="pt-1" hx-get="'.$reader_uri.'?action=list_active_searches" hx-trigger="load, changed, update_uploads_list from:body, updated_global_filter from:body"></div>';
7055

7156
echo '</div>';
7257

@@ -89,6 +74,9 @@
8974
echo '<div class="card mt-2">';
9075
echo '<div class="card-header">Database</div>';
9176
echo '<div class="card-body">';
77+
78+
echo '<div class="pt-1" hx-get="'.$reader_uri.'?action=show_stats" hx-trigger="load, changed, update_uploads_list from:body, updated_global_filter from:body"></div>';
79+
9280
echo '<div id="responseRebase"></div>';
9381

9482
echo '<button class="btn btn-default w-100" name="rebase" value="files_to_database" hx-post="'.$writer_uri.'" hx-target="#responseRebase" hx-swap="innerHTML" hx-include="[name=\'csrf_token\']">Rebase DB</button> ';

acp/core/uploads/router.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
require __DIR__.'/functions.php';
4+
35
$subinc = match (true) {
46
str_starts_with($query, 'uploads/edit/') => 'edit',
57
default => 'index'

public/assets/themes/administration/src/js/backend.js

+27-28
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,37 @@ document.addEventListener('DOMContentLoaded', () => {
174174
const toggleAllButton = document.getElementById('toggle-all');
175175

176176
if (toggleAllButton) {
177-
toggleAllButton.addEventListener('click', (event) => {
178-
event.preventDefault(); // Prevent form submission
177+
toggleAllButton.addEventListener('click', (event) => {
178+
event.preventDefault(); // Prevent form submission
179179

180-
document.querySelectorAll('.toggle-item').forEach(container => {
181-
container.classList.toggle('d-none'); // Toggle each container independently
180+
document.querySelectorAll('.toggle-item').forEach(container => {
181+
container.classList.toggle('d-none'); // Toggle each container independently
182+
});
182183
});
183-
});
184184
}
185185

186+
// uploads
187+
const uppy = new Uppy({
188+
debug: false,
189+
autoProceed: false,
190+
})
191+
192+
uppy.use(Form, {
193+
target: '.dropper-form',
194+
})
195+
196+
uppy.use(Dashboard, {
197+
inline: true,
198+
target: '.dropper-form',
199+
})
200+
uppy.use(XHRUpload, {
201+
endpoint: '/admin/upload/'
202+
})
203+
204+
uppy.on('complete', (result) => {
205+
htmx.trigger("body", "update_uploads_list");
206+
});
207+
186208
});
187209

188210
document.addEventListener("keydown", function (event) {
@@ -216,29 +238,6 @@ document.addEventListener("keydown", function (event) {
216238

217239
$(function() {
218240

219-
// observe "sortable_target" (image picker)
220-
// observeContainersForDraggableDivs('.sortable_target');
221-
222-
const uppy = new Uppy({
223-
debug: false,
224-
autoProceed: false,
225-
})
226-
227-
uppy.use(Form, {
228-
target: '.dropper-form',
229-
})
230-
231-
uppy.use(Dashboard, {
232-
inline: true,
233-
target: '.dropper-form',
234-
})
235-
uppy.use(XHRUpload, {
236-
endpoint: '/admin/upload/',
237-
})
238-
239-
240-
//$('[data-bs-toggle="popover"]').popover();
241-
//$('[data-bs-toggle="tooltip"]').tooltip();
242241

243242
setTimeout(function() {
244243
$(".alert-auto-close").slideUp('slow');

0 commit comments

Comments
 (0)