From 279a08ec01aa0260639ba5a56c1cf221bd59e5d6 Mon Sep 17 00:00:00 2001 From: Lody Borgers Date: Mon, 31 Jul 2023 15:51:12 +0200 Subject: [PATCH] Instruct replicator to use a private key as authentication, and gracefully handled image upload error on server --- app/models/images.server.ts | 2 ++ app/routes/api.images.upload.ts | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/models/images.server.ts b/app/models/images.server.ts index 65d0982..4073caf 100644 --- a/app/models/images.server.ts +++ b/app/models/images.server.ts @@ -1,6 +1,7 @@ import { unstable_createFileUploadHandler } from '@remix-run/node'; import dns from 'dns'; import Client from 'ssh2-sftp-client'; +import fs from 'fs'; import * as Sentry from '@sentry/node'; export const uploadHandler = unstable_createFileUploadHandler({ @@ -28,6 +29,7 @@ export async function replicateImageAcrossApps(image: string) { host: ip, port: 3022, username: process.env.REPLICATOR_SSH_USER, + privateKey: fs.readFileSync('/root/.ssh/id_rsa'), }); await sftp.put(`./images/${image}`, `/weddingfest/images/${image}`); diff --git a/app/routes/api.images.upload.ts b/app/routes/api.images.upload.ts index f284aba..bd25291 100644 --- a/app/routes/api.images.upload.ts +++ b/app/routes/api.images.upload.ts @@ -7,6 +7,7 @@ import { uploadHandler, } from '~/models/images.server'; import type { APIResponse, ImageUploadResponse } from '~/types/Responses'; +import { serverError } from 'remix-utils'; export async function action({ request }: ActionArgs) { await requireAdmin(request); @@ -32,7 +33,9 @@ export async function action({ request }: ActionArgs) { } if (process.env.NODE_ENV === 'production') { - await replicateImageAcrossApps(image.name); + await replicateImageAcrossApps(image.name).catch((e) => { + return serverError(e); + }); } return json({ location: `/image/${image.name}` });