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;
|
||||
}
|
||||
|
||||
// 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'])) {
|
||||
// ------------- Chunked Upload Handling -------------
|
||||
$chunkNumber = intval($_POST['resumableChunkNumber']); // current chunk (1-indexed)
|
||||
@@ -61,8 +86,13 @@ if (isset($_POST['resumableChunkNumber'])) {
|
||||
|
||||
// Check if all chunks have been uploaded.
|
||||
$uploadedChunks = glob($tempDir . "*");
|
||||
if (count($uploadedChunks) >= $totalChunks) {
|
||||
// All chunks uploaded. Merge chunks.
|
||||
if (count($uploadedChunks) < $totalChunks) {
|
||||
// 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;
|
||||
if (!$out = fopen($targetPath, "wb")) {
|
||||
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"]);
|
||||
exit;
|
||||
} else {
|
||||
// Chunk successfully uploaded, but more chunks remain.
|
||||
echo json_encode(["status" => "chunk uploaded"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
} else {
|
||||
// ------------- Full Upload (Non-chunked) -------------
|
||||
|
||||
Reference in New Issue
Block a user