adjust chunk merging logic
This commit is contained in:
41
upload.php
41
upload.php
@@ -19,7 +19,32 @@ if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if this is a chunked upload (Resumable.js sends "resumableChunkNumber").
|
/*
|
||||||
|
* Handle test chunk requests.
|
||||||
|
* When testChunks is enabled in Resumable.js, the client sends GET requests with a "resumableTest" parameter.
|
||||||
|
*/
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['resumableTest'])) {
|
||||||
|
$chunkNumber = intval($_GET['resumableChunkNumber']);
|
||||||
|
$resumableIdentifier = $_GET['resumableIdentifier'];
|
||||||
|
$folder = isset($_GET['folder']) ? trim($_GET['folder']) : 'root';
|
||||||
|
// Determine the base upload directory.
|
||||||
|
$baseUploadDir = UPLOAD_DIR;
|
||||||
|
if ($folder !== 'root') {
|
||||||
|
$baseUploadDir = rtrim(UPLOAD_DIR, '/\\') . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR;
|
||||||
|
}
|
||||||
|
$tempDir = $baseUploadDir . 'resumable_' . $resumableIdentifier . DIRECTORY_SEPARATOR;
|
||||||
|
$chunkFile = $tempDir . $chunkNumber;
|
||||||
|
if (file_exists($chunkFile)) {
|
||||||
|
http_response_code(200);
|
||||||
|
} else {
|
||||||
|
http_response_code(404);
|
||||||
|
}
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------
|
||||||
|
// Chunked upload handling (POST requests)
|
||||||
|
// ---------------------
|
||||||
if (isset($_POST['resumableChunkNumber'])) {
|
if (isset($_POST['resumableChunkNumber'])) {
|
||||||
// ------------- Chunked Upload Handling -------------
|
// ------------- Chunked Upload Handling -------------
|
||||||
$chunkNumber = intval($_POST['resumableChunkNumber']); // current chunk (1-indexed)
|
$chunkNumber = intval($_POST['resumableChunkNumber']); // current chunk (1-indexed)
|
||||||
@@ -61,8 +86,13 @@ if (isset($_POST['resumableChunkNumber'])) {
|
|||||||
|
|
||||||
// Check if all chunks have been uploaded.
|
// Check if all chunks have been uploaded.
|
||||||
$uploadedChunks = glob($tempDir . "*");
|
$uploadedChunks = glob($tempDir . "*");
|
||||||
if (count($uploadedChunks) >= $totalChunks) {
|
if (count($uploadedChunks) < $totalChunks) {
|
||||||
// All chunks uploaded. Merge chunks.
|
// More chunks remain – respond and let the client continue.
|
||||||
|
echo json_encode(["status" => "chunk uploaded"]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// All chunks are present. Merge chunks.
|
||||||
$targetPath = $baseUploadDir . $resumableFilename;
|
$targetPath = $baseUploadDir . $resumableFilename;
|
||||||
if (!$out = fopen($targetPath, "wb")) {
|
if (!$out = fopen($targetPath, "wb")) {
|
||||||
echo json_encode(["error" => "Failed to open target file for writing"]);
|
echo json_encode(["error" => "Failed to open target file for writing"]);
|
||||||
@@ -120,11 +150,6 @@ if (isset($_POST['resumableChunkNumber'])) {
|
|||||||
|
|
||||||
echo json_encode(["success" => "File uploaded successfully"]);
|
echo json_encode(["success" => "File uploaded successfully"]);
|
||||||
exit;
|
exit;
|
||||||
} else {
|
|
||||||
// Chunk successfully uploaded, but more chunks remain.
|
|
||||||
echo json_encode(["status" => "chunk uploaded"]);
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// ------------- Full Upload (Non-chunked) -------------
|
// ------------- Full Upload (Non-chunked) -------------
|
||||||
|
|||||||
Reference in New Issue
Block a user