'Method not allowed']); exit; } $sourceId = (int)($_POST['source_id'] ?? 0); $targetId = (int)($_POST['target_id'] ?? 0); if (!$sourceId || !$targetId) { http_response_code(400); echo json_encode(['error' => 'Paramètres manquants']); exit; } // Récupère le compte destination $target = db()->prepare('SELECT * FROM accounts WHERE id = ? AND active = 1'); $target->execute([$targetId]); $targetAccount = $target->fetch(); if (!$targetAccount) { http_response_code(404); echo json_encode(['error' => 'Compte destination introuvable']); exit; } // Récupère les fichiers à transférer $files = db()->prepare('SELECT * FROM files WHERE source_file_id = ?'); $files->execute([$sourceId]); $fileList = $files->fetchAll(); $destDir = $targetAccount['smb_path']; if (!is_dir($destDir)) mkdir($destDir, 0750, true); $moved = 0; foreach ($fileList as $file) { $newPath = $destDir . '/' . basename($file['path']); // Évite collision if (file_exists($newPath)) { $ext = pathinfo($newPath, PATHINFO_EXTENSION); $base = pathinfo($newPath, PATHINFO_FILENAME); $newPath = $destDir . '/' . $base . '_' . time() . '.' . $ext; } if (file_exists($file['path'])) { rename($file['path'], $newPath); db()->prepare('UPDATE files SET path = ? WHERE id = ?') ->execute([$newPath, $file['id']]); $moved++; } } // Met à jour le compte source_file db()->prepare('UPDATE source_file SET account_id = ? WHERE id = ?') ->execute([$targetId, $sourceId]); echo json_encode([ 'success' => true, 'moved' => $moved, 'target' => $targetAccount['username'], ]);