// /core/js/drivers_files/sftpnewfile.js // Handles new file creation via the SFTP PHP backend. export async function createFile(path, content = "") { try { // Get current connection credentials if (!window._currentSFTPConnection) { throw new Error("No active SFTP connection"); } const conn = window._currentSFTPConnection; console.log("[sftpnewfile] Creating file with connection:", conn.host); const res = await fetch("/core/php/sftp/newfile.php", { method: "POST", headers: { "Content-Type": "application/json" }, credentials: "include", // ensures PHP session cookies are sent body: JSON.stringify({ path, content, // Include connection credentials host: conn.host, port: conn.port, username: conn.username, password: conn.password }) }); // If HTTP not 200, try to capture any response text if (!res.ok) { const text = await res.text(); console.error("[sftpnewfile] HTTP error status:", res.status); console.error("[sftpnewfile] HTTP error response (raw):", text); console.error("[sftpnewfile] Response headers:", [...res.headers.entries()]); // Try to parse as JSON in case PHP returned an error object try { const errorData = JSON.parse(text); throw new Error(`HTTP ${res.status}: ${errorData.message || text}`); } catch (parseErr) { // Not JSON, show raw text throw new Error(`HTTP ${res.status}: ${text.substring(0, 500)}`); } } // Read raw text to handle invalid JSON safely const text = await res.text(); console.log("[sftpnewfile] Response:", text); let data; try { data = JSON.parse(text); } catch { console.error("[sftpnewfile] Invalid JSON response:", text); throw new Error("Server returned invalid JSON (check PHP output)"); } if (!data.success) { throw new Error(data.message || "Unknown error"); } console.log("[sftpnewfile] createFile() success:", data); return data; } catch (err) { console.error("[sftpnewfile] createFile() error:", err); return { success: false, message: err.message }; } }