-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from TuringCup/feature_backend
胡乱完成了项目,目前可以上传了
- Loading branch information
Showing
10 changed files
with
295 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,38 @@ | ||
import {NextRequest} from "next/server"; | ||
import {NextResponse} from "next/server"; | ||
|
||
export async function GET(req: NextRequest){ | ||
if(!req.cookies.has("token")) { | ||
const url = req.nextUrl.clone() | ||
url.pathname = '/user/login' | ||
return NextResponse.redirect(url) | ||
}else{ | ||
const url = req.nextUrl.clone() | ||
url.pathname = '/user/upload' | ||
return NextResponse.redirect(url) | ||
export async function GET(req: NextRequest) { | ||
try { | ||
if (!req.cookies.has("token")) { | ||
const url = req.nextUrl.clone() | ||
url.pathname = '/user/login' | ||
return NextResponse.redirect(url) | ||
} else { | ||
|
||
let reqBody = new FormData(); | ||
let token = req.cookies.get("token")?.value; | ||
reqBody.append("token", token as string); | ||
|
||
let api_response = await fetch("http://49.234.15.205:5001/api/validtoken", { | ||
method: "POST", | ||
body: reqBody | ||
}).then(e => e.json()) as { | ||
errorCode: number, | ||
errorMsg: string | ||
}; | ||
|
||
if (api_response.errorCode === 200) { | ||
const url = req.nextUrl.clone() | ||
url.pathname = '/user/upload' | ||
return NextResponse.redirect(url) | ||
} else { | ||
const url = req.nextUrl.clone() | ||
url.pathname = '/user/login' | ||
return NextResponse.redirect(url) | ||
} | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
return NextResponse.redirect("/504"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ const config = { | |
}, | ||
upload:{ | ||
Upload: "上传", | ||
UploadConfirm: "您确认要上传{}吗?" | ||
} | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {NextApiRequest, NextApiResponse} from "next"; | ||
import {formidable} from "formidable"; | ||
import {readFileSync} from "fs"; | ||
|
||
type ResponseData = { | ||
success: boolean, | ||
response: string, // 当且仅当success是false的时候才是string | ||
} | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<ResponseData> | ||
) { | ||
if (req.method?.toLowerCase() !== "post") { | ||
res.status(404).redirect("/404"); | ||
return; | ||
} | ||
|
||
try { | ||
let [,{file: files}] = await formidable().parse(req,); | ||
if(files == undefined){ | ||
throw "No file uploaded"; | ||
} | ||
let file = files[0]; | ||
let file_data = new Blob([readFileSync(file.filepath)]); | ||
let reqBody = new FormData(); | ||
let token = req.cookies["token"]; | ||
reqBody.append("token", token as string); | ||
reqBody.append("file", file_data, req.query["name"] as string | undefined ?? "upload.zip"); | ||
|
||
let api_response = await fetch("http://49.234.15.205:5001/api/user/upload", { | ||
method: "POST", | ||
body: reqBody | ||
}).then(e => e.json()) as { | ||
}; | ||
|
||
res.status(200).json({ | ||
success: true, | ||
response: api_response as any | ||
}) | ||
|
||
} catch (e: any) { | ||
res.status(200).json({ | ||
success: false, | ||
response: "Exception occurred: " + e.toString() | ||
}) | ||
} | ||
} | ||
|
||
export const config = { | ||
api: { | ||
bodyParser: false | ||
} | ||
} |
Oops, something went wrong.