From cfa60aa971877a83968dab9da48bb271f2334fb3 Mon Sep 17 00:00:00 2001 From: Mauricio Siu Date: Fri, 3 Apr 2026 17:29:24 -0600 Subject: [PATCH] refactor(upload): simplify file upload process to Docker container - Consolidated the file upload logic for both remote and local servers into a single command. - Removed redundant temporary file handling and streamlined error management. - Improved code readability by reducing complexity in the uploadFileToContainer function. --- packages/server/src/services/docker.ts | 75 +++++--------------------- 1 file changed, 13 insertions(+), 62 deletions(-) diff --git a/packages/server/src/services/docker.ts b/packages/server/src/services/docker.ts index 52abb5166..db6099d69 100644 --- a/packages/server/src/services/docker.ts +++ b/packages/server/src/services/docker.ts @@ -1,6 +1,3 @@ -import { mkdtemp, rm, writeFile } from "node:fs/promises"; -import { join } from "node:path"; -import { tmpdir } from "node:os"; import { execAsync, execAsyncRemote, @@ -526,67 +523,21 @@ export const uploadFileToContainer = async ( ? destinationPath : `/${destinationPath}`; - if (serverId) { - // Remote server: transfer file via base64 encoding using heredoc - const base64Content = fileBuffer.toString("base64"); - const tempFileName = `dokploy-upload-${Date.now()}-${fileName.replace(/[^a-zA-Z0-9.-]/g, "_")}`; - const tempPath = `/tmp/${tempFileName}`; + const base64Content = fileBuffer.toString("base64"); + const tempFileName = `dokploy-upload-${Date.now()}-${fileName.replace(/[^a-zA-Z0-9.-]/g, "_")}`; + const tempPath = `/tmp/${tempFileName}`; - try { - // Create temp directory and write file on remote server using heredoc - // This handles large files and special characters better than echo - const writeCommand = `cat << 'EOF' | base64 -d > "${tempPath}" -${base64Content} -EOF -`; + const command = `echo '${base64Content}' | base64 -d > "${tempPath}" && docker cp "${tempPath}" "${containerId}:${normalizedPath}" ; rm -f "${tempPath}"`; - await execAsyncRemote(serverId, writeCommand); - - // Copy file into container - const copyCommand = `docker cp "${tempPath}" "${containerId}:${normalizedPath}"`; - await execAsyncRemote(serverId, copyCommand); - - // Clean up temp file - const cleanupCommand = `rm -f "${tempPath}"`; - await execAsyncRemote(serverId, cleanupCommand); - } catch (error) { - // Try to clean up on error - try { - await execAsyncRemote(serverId, `rm -f "${tempPath}"`); - } catch { - // Ignore cleanup errors - } - throw new Error( - `Failed to upload file to container: ${error instanceof Error ? error.message : String(error)}`, - ); - } - } else { - // Local server: use temp directory - const tempDir = await mkdtemp(join(tmpdir(), "dokploy-upload-")); - const tempFilePath = join(tempDir, fileName); - - try { - // Write file to temp directory - await writeFile(tempFilePath, fileBuffer); - - // Copy file into container - const copyCommand = `docker cp "${tempFilePath}" "${containerId}:${normalizedPath}"`; - await execAsync(copyCommand); - - // Clean up temp directory - await rm(tempFilePath, { force: true }); - await rm(tempDir, { recursive: true, force: true }); - } catch (error) { - // Try to clean up on error - try { - await rm(tempFilePath, { force: true }); - await rm(tempDir, { recursive: true, force: true }); - } catch { - // Ignore cleanup errors - } - throw new Error( - `Failed to upload file to container: ${error instanceof Error ? error.message : String(error)}`, - ); + try { + if (serverId) { + await execAsyncRemote(serverId, command); + } else { + await execAsync(command); } + } catch (error) { + throw new Error( + `Failed to upload file to container: ${error instanceof Error ? error.message : String(error)}`, + ); } };