Skip to content

Commit

Permalink
Instruct replicator to use a private key as authentication, and grace…
Browse files Browse the repository at this point in the history
…fully handled image upload error on server
  • Loading branch information
Lody Borgers committed Jul 31, 2023
1 parent bb74850 commit 279a08e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/models/images.server.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -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}`);
Expand Down
5 changes: 4 additions & 1 deletion app/routes/api.images.upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<ImageUploadResponse>({ location: `/image/${image.name}` });
Expand Down

0 comments on commit 279a08e

Please sign in to comment.