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

Small fixes #5

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ yarn-error.log*

# typescript
*.tsbuildinfo

tmp/
Binary file added bin/pdf-localisator
Binary file not shown.
Binary file added bin/pdftotext
Binary file not shown.
3 changes: 3 additions & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ await import("./src/env.mjs");

/** @type {import("next").NextConfig} */
const config = {
experimental: {
serverActions: true,
},
reactStrictMode: true,
};

Expand Down
121 changes: 121 additions & 0 deletions src/app/musings/pdf-localisator/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { execFile } from "child_process";
import fs from "fs";
import os from "os";
import { redirect } from "next/navigation";
import { FileLike } from "openai/uploads";
import path from "path";

const envPath = process.env.PATH;
const binPath = path.join(process.cwd(), "bin");

const binaryPath = path.join(binPath, "pdf-localisator");

process.env.PATH = `${binPath}:${envPath}`;

// async function onSubmit(form: FormData) {
// "use server";
//
// const file = form.get("file") as FileLike;
//
// const tempPath = path.join(os.tmpdir(), "temp.pdf");
//
// const b = Buffer.from(await file.text());
//
// fs.createWriteStream(tempPath).write(b);
//
// console.log("tempPath", tempPath, "saved");
//
// const fileName = await new Promise<string>((resolve, reject) => {
// execFile(
// binaryPath,
// [tempPath, "bulgarian"],
// {
// cwd: path.dirname(binaryPath),
// env: {
// ...process.env,
// },
// },
// (error, stdout) => {
// if (error) {
// console.error("Error:", error);
// reject();
// } else {
// const resultFileName = file.name.replace(".pdf", ".txt");
//
// fs.writeFileSync(path.join("tmp", resultFileName), stdout);
//
// resolve(resultFileName);
// }
// },
// );
// });
//
// redirect(`/musings/pdf-localisator/preview/${fileName}`);
// }
async function onSubmit() {
"use server";

console.log("Binary path", binaryPath);

console.log("Calling pdftotext");

try {
await new Promise((resolve, reject) => {
execFile(path.join(binPath, "pdftotext"), ["--help"], (error, stdout) => {
console.log("pdftotext", error, stdout);
if (error) {
reject(error);
} else {
resolve(stdout);
}
});
});
} catch (e: unknown) {
console.log("OMG", e);
}

console.log("Calling pdf-localisator");
try {
await new Promise((resolve, reject) => {
execFile(
binaryPath,
["omg", "bulgarian"],
{
cwd: path.dirname(binaryPath),
env: {
...process.env,
},
},
(error, stdout) => {
console.log("Pdf-localisator", error, stdout);

if (error) {
console.error("Error:", error);
reject();
} else {
// const resultFileName = file.name.replace(".pdf", ".txt");
//
// fs.writeFileSync(path.join("tmp", resultFileName), stdout);
//
resolve(stdout);
}
},
);
});
} catch (e: unknown) {
console.log("OMG1", e);
}
}

export default function PdfLocalisator() {
return (
<div className="flex min-h-screen flex-col items-center justify-center bg-gradient-to-b from-[#2e026d] to-[#15162c]">
{/* eslint-disable-next-line @typescript-eslint/no-misused-promises*/}
<form action={onSubmit}>
<input type="file" name="file" />

<button type="submit"> Submit</button>
</form>
</div>
);
}
29 changes: 29 additions & 0 deletions src/app/musings/pdf-localisator/preview/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fs from "fs";
import path from "path";

export default async function PreviewTextPage(props: {
params: {
id: string;
};
}) {
console.log("OMG", props);

const fileContents = await new Promise<string>((resolve, reject) =>
fs.readFile(path.join("tmp", props.params.id), "utf8", (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
}),
);

console.log("props", props);
return (
<div>
<div>{props.params.id}</div>

<div>{fileContents}</div>
</div>
);
}