diff --git a/server/index.js b/server/index.js index 1470398..daf5753 100755 --- a/server/index.js +++ b/server/index.js @@ -584,8 +584,15 @@ app.post('/api/create-folder', authenticateToken, async (req, res) => { } catch (err) { // Folder doesn't exist, which is what we want } - await fs.promises.mkdir(targetPath, { recursive: false }); - res.json({ success: true, path: targetPath }); + try { + await fs.promises.mkdir(targetPath, { recursive: false }); + res.json({ success: true, path: targetPath }); + } catch (mkdirError) { + if (mkdirError.code === 'EEXIST') { + return res.status(409).json({ error: 'Folder already exists' }); + } + throw mkdirError; + } } catch (error) { console.error('Error creating folder:', error); res.status(500).json({ error: 'Failed to create folder' }); diff --git a/server/routes/projects.js b/server/routes/projects.js index 7e3c7a1..863fd7f 100644 --- a/server/routes/projects.js +++ b/server/routes/projects.js @@ -238,16 +238,29 @@ router.post('/create-workspace', async (req, res) => { const repoName = normalizedUrl.split('/').pop() || 'repository'; const clonePath = path.join(absolutePath, repoName); + // Check if clone destination already exists to prevent data loss + try { + await fs.access(clonePath); + return res.status(409).json({ + error: 'Directory already exists', + details: `The destination path "${clonePath}" already exists. Please choose a different location or remove the existing directory.` + }); + } catch (err) { + // Directory doesn't exist, which is what we want + } + // Clone the repository into a subfolder try { await cloneGitHubRepository(githubUrl, clonePath, githubToken); } catch (error) { - // Clean up created directory on failure + // Only clean up if clone created partial data (check if dir exists and is empty or partial) try { - await fs.rm(clonePath, { recursive: true, force: true }); + const stats = await fs.stat(clonePath); + if (stats.isDirectory()) { + await fs.rm(clonePath, { recursive: true, force: true }); + } } catch (cleanupError) { - console.error('Failed to clean up directory after clone failure:', cleanupError); - // Continue to throw original error + // Directory doesn't exist or cleanup failed - ignore } throw new Error(`Failed to clone repository: ${error.message}`); } @@ -352,6 +365,16 @@ router.get('/clone-progress', async (req, res) => { const repoName = normalizedUrl.split('/').pop() || 'repository'; const clonePath = path.join(absolutePath, repoName); + // Check if clone destination already exists to prevent data loss + try { + await fs.access(clonePath); + sendEvent('error', { message: `Directory "${repoName}" already exists. Please choose a different location or remove the existing directory.` }); + res.end(); + return; + } catch (err) { + // Directory doesn't exist, which is what we want + } + let cloneUrl = githubUrl; if (githubToken) { try {