file_metadata.json and refactoring
This commit is contained in:
97
upload.php
97
upload.php
@@ -1,75 +1,48 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once 'config.php';
|
require_once 'config.php';
|
||||||
require_once 'auth.php';
|
session_start();
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
// Function to convert size to bytes
|
if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) {
|
||||||
function convertToBytes($size) {
|
echo json_encode(["error" => "Unauthorized"]);
|
||||||
$number = substr($size, 0, -1);
|
exit;
|
||||||
switch (strtoupper(substr($size, -1))) {
|
|
||||||
case 'G':
|
|
||||||
return $number * 1024 * 1024 * 1024;
|
|
||||||
case 'M':
|
|
||||||
return $number * 1024 * 1024;
|
|
||||||
case 'K':
|
|
||||||
return $number * 1024;
|
|
||||||
default:
|
|
||||||
return $size;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to get the total size of files in the directory
|
$uploadDir = UPLOAD_DIR;
|
||||||
function getDirectorySize($dir) {
|
$metadataFile = "file_metadata.json";
|
||||||
$size = 0;
|
|
||||||
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $file) {
|
if (!file_exists($uploadDir)) {
|
||||||
if ($file->isFile()) {
|
mkdir($uploadDir, 0775, true);
|
||||||
$size += $file->getSize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the form was submitted
|
// Load existing metadata
|
||||||
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
$metadata = file_exists($metadataFile) ? json_decode(file_get_contents($metadataFile), true) : [];
|
||||||
// Get the username and password
|
$metadataChanged = false;
|
||||||
$username = $_POST['username'];
|
|
||||||
$password = $_POST['password'];
|
|
||||||
$fileDateTime = isset($_POST['fileDateTime']) ? (int)$_POST['fileDateTime'] / 1000 : time();
|
|
||||||
|
|
||||||
// Validate the credentials using the Flask backend
|
foreach ($_FILES["file"]["name"] as $index => $fileName) {
|
||||||
if (authenticate($username, $password)) {
|
$filePath = $uploadDir . basename($fileName);
|
||||||
// Check if a file was uploaded
|
|
||||||
if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) {
|
|
||||||
$uploadFile = UPLOAD_DIR . basename($_FILES['file']['name']);
|
|
||||||
$tmpFile = $_FILES['file']['tmp_name'];
|
|
||||||
|
|
||||||
// Get the total upload limit from config and convert to bytes
|
if (move_uploaded_file($_FILES["file"]["tmp_name"][$index], $filePath)) {
|
||||||
$totalUploadLimit = convertToBytes(TOTAL_UPLOAD_SIZE);
|
// Store "Uploaded Date" and "Uploader" only if not already stored
|
||||||
// Get the current size of the upload directory
|
if (!isset($metadata[$fileName])) {
|
||||||
$currentDirSize = getDirectorySize(UPLOAD_DIR);
|
$uploadedDate = date(DATE_TIME_FORMAT); // Store only the first upload time
|
||||||
// Get the size of the new file
|
$uploader = $_SESSION['username'] ?? "Unknown";
|
||||||
$fileSize = $_FILES['file']['size'];
|
$metadata[$fileName] = [
|
||||||
|
"uploaded" => $uploadedDate,
|
||||||
// Check if adding the new file exceeds the total upload limit
|
"uploader" => $uploader
|
||||||
if (($currentDirSize + $fileSize) > $totalUploadLimit) {
|
];
|
||||||
echo "Upload denied. Total upload limit exceeded.";
|
$metadataChanged = true;
|
||||||
} else {
|
|
||||||
// Move the uploaded file to the specified directory
|
|
||||||
if (move_uploaded_file($tmpFile, $uploadFile)) {
|
|
||||||
// Preserve the original file modification time
|
|
||||||
touch($uploadFile, $fileDateTime);
|
|
||||||
echo "File is valid, and was successfully uploaded.";
|
|
||||||
} else {
|
|
||||||
echo "File upload failed! ";
|
|
||||||
print_r(error_get_last());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
echo "No file uploaded or file upload error!";
|
|
||||||
echo "Error code: " . $_FILES['file']['error'];
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
echo "Invalid username or password!";
|
echo json_encode(["error" => "Error uploading file"]);
|
||||||
|
exit;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
echo "Invalid request method!";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save metadata only if modified
|
||||||
|
if ($metadataChanged) {
|
||||||
|
file_put_contents($metadataFile, json_encode($metadata, JSON_PRETTY_PRINT));
|
||||||
|
}
|
||||||
|
|
||||||
|
echo json_encode(["success" => "Files uploaded successfully"]);
|
||||||
|
?>
|
||||||
|
|||||||
Reference in New Issue
Block a user