Skip to content

Commit

Permalink
Support for heif format in the Manage Sources feature
Browse files Browse the repository at this point in the history
  • Loading branch information
GwonHyeok committed Feb 10, 2025
1 parent c4db890 commit 3213df4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"cuid": "^3.0.0",
"dayjs": "^1.11.13",
"file-type": "^20.1.0",
"gm": "^1.25.0",
"langchain": "^0.3.15",
"lucide-react": "^0.473.0",
"next": "15.1.6",
Expand All @@ -48,6 +49,7 @@
"devDependencies": {
"@eslint/eslintrc": "^3",
"@tailwindcss/typography": "^0.5.16",
"@types/gm": "^1.25.4",
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
Expand Down
39 changes: 32 additions & 7 deletions src/app/api/health-data/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {NextRequest, NextResponse} from "next/server";
import fs from 'fs'
import {parseHealthDataFromPDF} from "@/lib/health-data/parser/pdf";
import crypto from "node:crypto";
import {fileTypeFromBuffer} from "file-type";
import gm from "gm";

export interface HealthData extends Prisma.HealthDataGetPayload<{
select: {
Expand Down Expand Up @@ -63,15 +65,38 @@ export async function POST(

// Save files
if (file instanceof File) {
const fileBuffer = Buffer.from(await file.arrayBuffer())
const result = await fileTypeFromBuffer(fileBuffer)
if (!result) return NextResponse.json({error: 'Failed to determine file type'}, {status: 400})

// Get file hash
const hash = crypto.createHash('md5')
hash.update(Buffer.from(await file.arrayBuffer()))
hash.update(fileBuffer)
const fileHash = hash.digest('hex')
const extension = file.name.split('.').pop()
const filename = `${fileHash}.${extension}`;
fs.writeFileSync(`./public/uploads/${filename}`, Buffer.from(await file.arrayBuffer()))
fileType = file.type
filePath = `/uploads/${filename}`
baseData = {fileName: file.name}

const {mime} = result
if (mime.startsWith('image/')) {
const imageMagick = gm.subClass({imageMagick: true});
const outputBuffer = await new Promise<Buffer>((resolve, reject) => {
imageMagick(fileBuffer)
.toBuffer('PNG', (err, buffer) => {
if (err) reject(err);
else resolve(buffer);
});
});
const filename = `${fileHash}.png`;
fs.writeFileSync(`./public/uploads/${filename}`, outputBuffer)
fileType = file.type
filePath = `/uploads/${filename}`
baseData = {fileName: file.name}
} else {
const extension = file.name.split('.').pop()
const filename = `${fileHash}.${extension}`;
fs.writeFileSync(`./public/uploads/${filename}`, fileBuffer)
fileType = file.type
filePath = `/uploads/${filename}`
baseData = {fileName: file.name}
}
}

// Create parsing data
Expand Down

0 comments on commit 3213df4

Please sign in to comment.