Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display results #5

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions app/src/pages/api/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ import os from "os";
import path from "path";

import BlurryDetector from "../../utils/blur-detector";
import type { BlurryDetectorReport } from "../../utils/blur-detector";

export const config = {
api: {
bodyParser: false,
},
};

type ResponseData = {
type BlurryDetectorResults = Array<{
status: "fulfilled" | "rejected";
value?: BlurryDetectorReport;
reason?: BlurryDetectorReport;
}>;

export type ResponseData = {
message: string;
results?: unknown;
results?: BlurryDetectorResults;
};

// The threshold is based on a few articles about this approach
Expand All @@ -31,7 +38,7 @@ export default async function handler(
const form = formidable({});
const [, files] = await form.parse(req);

const results = await Promise.allSettled(
const results: BlurryDetectorResults = await Promise.allSettled(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea what the requirements are around persisting files? Would we need these at some other point in the future for the user to reference? If so then Vercel Blob storage or another 3rd party storage solution might be a good fit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GeorgeCodes19 not sure what those reqs will be... For now we're just writing to local filesystem as a placeholder — no current need to retrieve the files later. I think it drops them in a tmp folder that gets wiped regularly.

If there's a need for it beyond proof of concept, definitely would like to look into something like Vercel's blob storage. I deployed on Netlify because that's what I'm familiar with, but would love to try out Vercel's server offering.

Object.values(files).map(async (filelist = []) => {
const [file] = filelist;
if (file) {
Expand Down
33 changes: 32 additions & 1 deletion app/src/pages/upload/confirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@ import {
ProcessListItem,
} from "@trussworks/react-uswds";
import type { NextPage } from "next";
import { useRouter } from "next/router";
import Layout from "src/components/Layout";

import type { ResponseData } from "../api/upload";

const Confirmation: NextPage = () => {
const router = useRouter();
const results = router.query?.results;
let parsedResults: ResponseData = { message: "" };

if (results) {
parsedResults = JSON.parse(results.toString() || "") as ResponseData;
}

return (
<Layout>
<div className="margin-top-5">
Expand All @@ -16,8 +27,28 @@ const Confirmation: NextPage = () => {
#8450001171
</span>
<p className="usa-intro measure-5">
Thank you for uploading your documents.
Thank you for uploading your documents. Here are the results:
</p>
<ul>
{parsedResults?.results?.map((result, idx) => (
<li key={idx}>
{result.value && (
<>
{result.value.imagePath}:{" "}
{result.value.isBlurry ? "blurry!" : "sharp"} (
{result.value.score})
</>
)}
{result.reason && (
<>
{result.reason.imagePath}:{" "}
{result.reason.isBlurry ? "blurry!" : "sharp"} (
{result.reason.score})
</>
)}
</li>
))}
</ul>
<ProcessList>
<ProcessListItem>
<ProcessListHeading type="h4">Check your email</ProcessListHeading>
Expand Down
11 changes: 8 additions & 3 deletions app/src/utils/blur-detector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import sharp from "sharp";

export type BlurryDetectorReport = {
imagePath: string;
isBlurry: boolean;
score: number;
};

// https://github.com/puntorigen/blurry-detector/blob/main/index.js
class BlurryDetector {
threshold: number;
Expand Down Expand Up @@ -38,9 +44,7 @@ class BlurryDetector {
return variance;
}

async analyse(
imagePath: string
): Promise<{ isBlurry: boolean; score: number }> {
async analyse(imagePath: string): Promise<BlurryDetectorReport> {
let variance = -1;
try {
variance = await this.computeLaplacianVariance(imagePath);
Expand All @@ -49,6 +53,7 @@ class BlurryDetector {
}

return {
imagePath,
isBlurry: variance < this.threshold,
score: variance,
};
Expand Down
Loading