upd: api mobile

Deskripsi:
- percobaan upload gambar by reactnative

No Issues
This commit is contained in:
amel
2025-05-19 10:25:06 +08:00
parent 85d13f766a
commit f8c949e691
5 changed files with 102 additions and 43 deletions

3
.gitignore vendored
View File

@@ -43,4 +43,5 @@ next-env.d.ts
/public/file/
certificates
test.png
test.png
test2.png

BIN
bun.lockb

Binary file not shown.

View File

@@ -39,18 +39,24 @@
"@tiptap/pm": "^2.11.7",
"@tiptap/react": "^2.4.0",
"@tiptap/starter-kit": "^2.4.0",
"@types/busboy": "^1.5.4",
"@types/lodash": "^4.17.6",
"@types/multer": "^1.4.12",
"@types/web-push": "^3.6.3",
"busboy": "^1.6.0",
"dayjs": "^1.11.11",
"echarts": "^5.5.1",
"echarts-for-react": "^3.0.2",
"elysia": "^1.3.1",
"embla-carousel-autoplay": "^7.1.0",
"embla-carousel-react": "^7.1.0",
"from": "^0.1.7",
"import": "^0.0.6",
"iron-session": "^8.0.2",
"jose": "^5.9.2",
"lodash": "^4.17.21",
"moment": "^2.30.1",
"multer": "^1.4.5-lts.2",
"next": "14.2.4",
"pdfjs-dist": "^4.6.82",
"prettier": "^3.3.2",

View File

@@ -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";
const Api = new Elysia({
prefix: "/api/v2"
})
.onError((c) => {
console.log(c)
return c
})
// send a file
.post("/test", async (c) => {
try {
// const { file } = c.body
import { swagger } from "@elysiajs/swagger";
import Elysia from "elysia";
import { existsSync } from "fs";
import fs from "fs/promises";
const UPLOAD_DIR = "./uploads";
console.log("terima file")
// const buffer = Buffer.from(await file.arrayBuffer());
// 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(),
}),
const Api = new Elysia({ prefix: "/api/v2" })
.onError(({ error }) => {
})
.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()
.use(cors({
origin: "*",
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"]
}))
.use(swagger({
path: "/api/v2/docs"
}))
.use(Api)
.use(swagger({ path: "/api/v2/docs" }))
.use(Api);
export const GET = ApiServer.handle
export const POST = ApiServer.handle
export const GET = ApiServer.handle;
export const POST = ApiServer.handle;

View 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' },
});
}
}