Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
robertotcestari committed Aug 4, 2024
1 parent faa4ca2 commit 1911be8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
39 changes: 3 additions & 36 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,12 @@
import { Hono } from 'hono';
import { getBufferFromPageScreenshot } from './lib/screenshot';
import { S3 } from './lib/s3';
import {
screenshotPutValidator,
screenshotValidator,
} from './lib/hono-validators';
import { bearerAuth } from 'hono/bearer-auth';
import screenshot from './routes/screenshot';

const app = new Hono();
app.use('*', bearerAuth({ token: process.env.TOKEN! }));

app.post('/screenshot', screenshotValidator(), async (c) => {
const { url, fileName } = c.req.valid('json');

try {
const screenshotBuffer = await getBufferFromPageScreenshot(url);
const s3 = new S3();
const { imageUrl } = await s3.uploadImage(fileName, screenshotBuffer);

return c.json({ message: 'Screenshot uploaded to S3', imageUrl });
} catch (e: any) {
return c.json({ message: e.message }, 500);
}
});

app.put('/screenshot', screenshotPutValidator(), async (c) => {
const { url, fileName, oldFilename } = c.req.valid('json');

try {
const screenshotBuffer = await getBufferFromPageScreenshot(url);
const s3 = new S3();
const { imageUrl } = await s3.uploadImage(fileName, screenshotBuffer);

// delete the old image
await s3.deleteImage(oldFilename);
app.use('*', bearerAuth({ token: process.env.TOKEN! }));

return c.json({ message: 'Screenshot updated on S3', imageUrl });
} catch (e: any) {
return c.json({ message: e.message }, 500);
}
});
app.route('/screenshot', screenshot);

export default {
port: 3012,
Expand Down
42 changes: 42 additions & 0 deletions src/routes/screenshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Hono } from 'hono';
import {
screenshotPutValidator,
screenshotValidator,
} from '../lib/hono-validators';
import { getBufferFromPageScreenshot } from '../lib/screenshot';
import { S3 } from '../lib/s3';

const app = new Hono();

app.post('/', screenshotValidator(), async (c) => {
const { url, fileName } = c.req.valid('json');

try {
const screenshotBuffer = await getBufferFromPageScreenshot(url);
const s3 = new S3();
const { imageUrl } = await s3.uploadImage(fileName, screenshotBuffer);

return c.json({ message: 'Screenshot uploaded to S3', imageUrl });
} catch (e: any) {
return c.json({ message: e.message }, 500);
}
});

app.put('/', screenshotPutValidator(), async (c) => {
const { url, fileName, oldFilename } = c.req.valid('json');

try {
const screenshotBuffer = await getBufferFromPageScreenshot(url);
const s3 = new S3();
const { imageUrl } = await s3.uploadImage(fileName, screenshotBuffer);

// delete the old image
await s3.deleteImage(oldFilename);

return c.json({ message: 'Screenshot updated on S3', imageUrl });
} catch (e: any) {
return c.json({ message: e.message }, 500);
}
});

export default app;

0 comments on commit 1911be8

Please sign in to comment.