Merge pull request #447 from bipproduction/amalia/16-mei-25
upd: api mobile
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -44,3 +44,4 @@ next-env.d.ts
|
|||||||
certificates
|
certificates
|
||||||
|
|
||||||
test.png
|
test.png
|
||||||
|
test2.png
|
||||||
@@ -39,18 +39,24 @@
|
|||||||
"@tiptap/pm": "^2.11.7",
|
"@tiptap/pm": "^2.11.7",
|
||||||
"@tiptap/react": "^2.4.0",
|
"@tiptap/react": "^2.4.0",
|
||||||
"@tiptap/starter-kit": "^2.4.0",
|
"@tiptap/starter-kit": "^2.4.0",
|
||||||
|
"@types/busboy": "^1.5.4",
|
||||||
"@types/lodash": "^4.17.6",
|
"@types/lodash": "^4.17.6",
|
||||||
|
"@types/multer": "^1.4.12",
|
||||||
"@types/web-push": "^3.6.3",
|
"@types/web-push": "^3.6.3",
|
||||||
|
"busboy": "^1.6.0",
|
||||||
"dayjs": "^1.11.11",
|
"dayjs": "^1.11.11",
|
||||||
"echarts": "^5.5.1",
|
"echarts": "^5.5.1",
|
||||||
"echarts-for-react": "^3.0.2",
|
"echarts-for-react": "^3.0.2",
|
||||||
"elysia": "^1.3.1",
|
"elysia": "^1.3.1",
|
||||||
"embla-carousel-autoplay": "^7.1.0",
|
"embla-carousel-autoplay": "^7.1.0",
|
||||||
"embla-carousel-react": "^7.1.0",
|
"embla-carousel-react": "^7.1.0",
|
||||||
|
"from": "^0.1.7",
|
||||||
|
"import": "^0.0.6",
|
||||||
"iron-session": "^8.0.2",
|
"iron-session": "^8.0.2",
|
||||||
"jose": "^5.9.2",
|
"jose": "^5.9.2",
|
||||||
"lodash": "^4.17.21",
|
"lodash": "^4.17.21",
|
||||||
"moment": "^2.30.1",
|
"moment": "^2.30.1",
|
||||||
|
"multer": "^1.4.5-lts.2",
|
||||||
"next": "14.2.4",
|
"next": "14.2.4",
|
||||||
"pdfjs-dist": "^4.6.82",
|
"pdfjs-dist": "^4.6.82",
|
||||||
"prettier": "^3.3.2",
|
"prettier": "^3.3.2",
|
||||||
|
|||||||
@@ -1,51 +1,36 @@
|
|||||||
import Elysia from "elysia";
|
|
||||||
import { swagger } from "@elysiajs/swagger";
|
|
||||||
import { t } from "elysia";
|
|
||||||
import fs from "fs/promises";
|
|
||||||
import cors from "@elysiajs/cors";
|
import cors from "@elysiajs/cors";
|
||||||
const Api = new Elysia({
|
import { swagger } from "@elysiajs/swagger";
|
||||||
prefix: "/api/v2"
|
import Elysia from "elysia";
|
||||||
})
|
import { existsSync } from "fs";
|
||||||
.onError((c) => {
|
import fs from "fs/promises";
|
||||||
console.log(c)
|
const UPLOAD_DIR = "./uploads";
|
||||||
return c
|
|
||||||
})
|
|
||||||
// send a file
|
|
||||||
.post("/test", async (c) => {
|
|
||||||
try {
|
|
||||||
// const { file } = c.body
|
|
||||||
|
|
||||||
console.log("terima file")
|
const Api = new Elysia({ prefix: "/api/v2" })
|
||||||
// const buffer = Buffer.from(await file.arrayBuffer());
|
.onError(({ error }) => {
|
||||||
// await fs.writeFile("./test.png", buffer);
|
|
||||||
|
|
||||||
return {
|
|
||||||
success: true,
|
|
||||||
message: "File uploaded successfully",
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(JSON.stringify(error));
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
message: "File uploaded failed",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
body: t.Object({
|
|
||||||
name: t.String(),
|
|
||||||
file: t.File(),
|
|
||||||
}),
|
|
||||||
})
|
})
|
||||||
|
.post("/test", async ({ request }) => {
|
||||||
|
const formData = await request.formData()
|
||||||
|
const file = formData.get("file") as File;
|
||||||
|
const buffer = Buffer.from(await file.arrayBuffer());
|
||||||
|
await fs.writeFile("./test2.png", buffer);
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: "success",
|
||||||
|
message: "File uploaded successfully",
|
||||||
|
data: {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
const ApiServer = new Elysia()
|
const ApiServer = new Elysia()
|
||||||
.use(cors({
|
.use(cors({
|
||||||
origin: "*",
|
origin: "*",
|
||||||
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"]
|
||||||
}))
|
}))
|
||||||
.use(swagger({
|
.use(swagger({ path: "/api/v2/docs" }))
|
||||||
path: "/api/v2/docs"
|
.use(Api);
|
||||||
}))
|
|
||||||
.use(Api)
|
|
||||||
|
|
||||||
export const GET = ApiServer.handle
|
export const GET = ApiServer.handle;
|
||||||
export const POST = ApiServer.handle
|
export const POST = ApiServer.handle;
|
||||||
|
|||||||
67
src/app/api/v2/uploads/route.ts
Normal file
67
src/app/api/v2/uploads/route.ts
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
// app/api/upload/route.ts
|
||||||
|
import { NextRequest } from 'next/server';
|
||||||
|
|
||||||
|
// Since using Multer directly is difficult with Next.js App Router,
|
||||||
|
// we'll create a custom implementation using busboy which multer uses internally
|
||||||
|
|
||||||
|
export async function POST(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
// Check content type
|
||||||
|
const contentType = request.headers.get('content-type') || '';
|
||||||
|
if (!contentType.includes('multipart/form-data')) {
|
||||||
|
return new Response(JSON.stringify({ error: 'Invalid content-type' }), {
|
||||||
|
status: 400,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use formData directly instead of trying to make multer work
|
||||||
|
// This is the recommended approach for Next.js App Router
|
||||||
|
const formData = await request.formData();
|
||||||
|
const file = formData.get('file') as File;
|
||||||
|
|
||||||
|
if (!file) {
|
||||||
|
return new Response(JSON.stringify({ error: 'No file uploaded' }), {
|
||||||
|
status: 400,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const bytes = await file.arrayBuffer();
|
||||||
|
const buffer = Buffer.from(bytes);
|
||||||
|
|
||||||
|
// Here you can add file validation, processing, or storage
|
||||||
|
// similar to what multer would do
|
||||||
|
|
||||||
|
// Example: check file type or size
|
||||||
|
if (buffer.length > 5 * 1024 * 1024) { // 5MB limit
|
||||||
|
return new Response(JSON.stringify({ error: 'File too large' }), {
|
||||||
|
status: 400,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Example: you can save the file to disk or cloud storage here
|
||||||
|
// const fs = require('fs');
|
||||||
|
// fs.writeFileSync(`./uploads/${file.name}`, buffer);
|
||||||
|
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
message: 'Upload successful',
|
||||||
|
name: file.name,
|
||||||
|
type: file.type,
|
||||||
|
size: buffer.length,
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
status: 200,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('Upload error:', err);
|
||||||
|
return new Response(JSON.stringify({ error: err.message }), {
|
||||||
|
status: 500,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user