Add an admin page to manage uploaded files
authorrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 3 Jul 2017 21:16:01 +0000 (21:16 +0000)
committerrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 3 Jul 2017 21:16:01 +0000 (21:16 +0000)
https://bugs.webkit.org/show_bug.cgi?id=174089

Reviewed by Andreas Kling.

Add an admin page to see the disk usage per user as well as the total, and to prune any zombie files (ones marked
as deleted but aren't actually deleted in the filesystem).

* public/admin/files.php: Added.
(format_size): Added.
* public/include/admin-header.php:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@219093 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Websites/perf.webkit.org/ChangeLog
Websites/perf.webkit.org/public/admin/files.php [new file with mode: 0644]
Websites/perf.webkit.org/public/include/admin-header.php

index 5882c06..07cc7c9 100644 (file)
@@ -1,5 +1,19 @@
 2017-07-03  Ryosuke Niwa  <rniwa@webkit.org>
 
+        Add an admin page to manage uploaded files
+        https://bugs.webkit.org/show_bug.cgi?id=174089
+
+        Reviewed by Andreas Kling.
+
+        Add an admin page to see the disk usage per user as well as the total, and to prune any zombie files (ones marked
+        as deleted but aren't actually deleted in the filesystem).
+
+        * public/admin/files.php: Added.
+        (format_size): Added.
+        * public/include/admin-header.php:
+
+2017-07-03  Ryosuke Niwa  <rniwa@webkit.org>
+
         Roots uploaded by bots don't get author specified properly
         https://bugs.webkit.org/show_bug.cgi?id=174087
 
diff --git a/Websites/perf.webkit.org/public/admin/files.php b/Websites/perf.webkit.org/public/admin/files.php
new file mode 100644 (file)
index 0000000..56d5271
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+
+include('../include/admin-header.php');
+include('../include/uploaded-file-helpers.php');
+
+if ($db) {
+
+    if ($action == 'prune-zombie-files') {
+        $uploaded_file_rows = $db->select_rows('uploaded_files', 'file', array('deleted_at' => NULL));
+        $file_by_id = array();
+        foreach ($uploaded_file_rows as &$row)
+            $file_by_id[$row['file_id']] = &$row;
+
+        $file_name_list = scandir(config_path('uploadDirectory', ''));
+        foreach ($file_name_list as $file_name) {
+            if ($file_name == '.' || $file_name == '..')
+                continue;
+            if (!preg_match('/^(\d+)((\.[A-Za-z0-9]{1,5}){1,2})$/', $file_name, $matches)) {
+                echo 'Unknown file: ' . htmlspecialchars($file_name) . '<br>';
+                continue;
+            }
+            $file_name = $matches[0];
+            $file_path = config_path('uploadDirectory', $file_name);
+            $file_id = $matches[1];
+            if (!array_key_exists($file_id, $file_by_id)) {
+                echo "Deleting a zombie file: $file_name...";
+                if (file_exists($file_path) && !unlink($file_path))
+                    echo "Failed";
+                else
+                    echo "Deleted";
+                echo "<br>";
+            }
+        }
+    }
+
+    $files_per_user = $db->query_and_fetch_all('SELECT file_author AS "author", SUM(file_size) AS "usage", COUNT(file_id) AS "count"
+        FROM uploaded_files GROUP BY file_author');
+
+    echo <<< END
+<table>
+    <thead>
+        <tr>
+            <td>User</td>
+            <td>Number of Files</td>
+            <td>Disk Usage</td>
+        </tr>
+    </thead>
+    <tbody>
+END;
+
+    function format_size($usage, $quota) {
+        $megabytes = round($usage / MEGABYTES);
+        $percent = round(10000 * $usage / $quota) / 100;
+        return "$megabytes MB ($percent%)";
+    }
+
+    $quota_per_user = config('uploadUserQuotaInMB');
+    $total_disk_usage = 0;
+    $total_file_count = 0;
+
+    foreach ($files_per_user as $row) {
+        $user_name = $row['author'] ? $row['author'] : 'anonymous';
+        $file_count = $row['count'];
+        $disk_usage = format_size($row['usage'], $quota_per_user * MEGABYTES);
+        echo "    <tr><td>$user_name</td><td>$file_count</td><td>$disk_usage</td></tr>";
+        $total_disk_usage += $row['usage'];
+        $total_file_count += $file_count;
+    }
+
+    $total_disk_usage = format_size($total_disk_usage, config('uploadTotalQuotaInMB') * MEGABYTES);
+    echo "    <tr><td>Total</td><td>$total_file_count</td><td>$total_disk_usage</td>";
+
+    echo <<< END
+    </tbody>
+</table>
+<form method="POST"><button name="action" value="prune-zombie-files" type="submit">Prune zombie files (slow)</button></form>
+END;
+
+}
+
+include('../include/admin-footer.php');
+
+?>
index 6a5f1ce..c4d37bf 100644 (file)
@@ -22,6 +22,7 @@ require_once('manifest-generator.php');
     <li><a href="/admin/triggerables">Triggerables</a></li>
     <li><a href="/admin/repositories">Repositories</a></li>
     <li><a href="/admin/bug-trackers">Bug Trackers</a></li>
+    <li><a href="/admin/files">Files</a></li>
 </ul>
 </header>