-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add tokens count for chat completion Signed-off-by: NguyenNguyen205 <s3927220@rmit.edu.vn> * Set up files APIs Signed-off-by: NguyenNguyen205 <s3927220@rmit.edu.vn> * Add check file format on endpoint Signed-off-by: NguyenNguyen205 <s3927220@rmit.edu.vn> * Add get all endpoints, save data to lance db, modify npm run dev Signed-off-by: NguyenNguyen205 <s3927220@rmit.edu.vn> * Add documentation for 2 endpoints Signed-off-by: NguyenNguyen205 <s3927220@rmit.edu.vn> * Add code commenting and fix small bugs Signed-off-by: NguyenNguyen205 <s3927220@rmit.edu.vn> --------- Signed-off-by: NguyenNguyen205 <s3927220@rmit.edu.vn>
- Loading branch information
1 parent
8d4b208
commit a74623e
Showing
9 changed files
with
485 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// coding=utf-8 | ||
|
||
import { randomUUID } from "crypto"; | ||
import { extractAPIKeyFromRequest, validateAPIKey } from "../tools/apiKey.js"; | ||
import * as fs from 'fs'; | ||
import { getAllFilesData, loadFileToDatabase } from "../database/file-handling.js"; | ||
|
||
// Copyright [2024] [SkywardAI] | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/** | ||
* function for upload a file | ||
* @param {Request} req | ||
* @param {Response} res | ||
*/ | ||
|
||
export async function uploadFile(req, res) { | ||
if (!validateAPIKey(extractAPIKeyFromRequest(req))) { | ||
res.status(401).send("Not Authorized!"); | ||
return; | ||
} | ||
const { file } = req; | ||
if (!file) { | ||
res.status(400).send("Input file not specified"); | ||
return; | ||
} | ||
|
||
// Check file size limit (10MB) | ||
if (file.size / 1000000 > 10) { | ||
res.status(400).send("Only accepting file size smaller than 10MB"); | ||
return; | ||
} | ||
|
||
// Check file format | ||
let acceptedFormat = "application/json" | ||
if (acceptedFormat.localeCompare(file.mimetype) != 0) { | ||
res.status(400).send("File format not supported"); | ||
return; | ||
} | ||
|
||
// Load in database | ||
let resBody = { | ||
"id": randomUUID(), | ||
"bytes": file.size, | ||
"created_at": Date.now(), | ||
"filename": file.originalname, | ||
} | ||
|
||
const result = await loadFileToDatabase(resBody); | ||
if (!result) { | ||
res.status(500).send("Can't save to database"); | ||
return; | ||
} | ||
|
||
// load file | ||
const uploadPath = `files/${file.originalname}`; | ||
fs.writeFileSync(uploadPath, file.buffer, (err) => { | ||
if (err) throw err; | ||
console.log("File has been saved"); | ||
}) | ||
|
||
res.status(200).send(resBody); | ||
return; | ||
} | ||
|
||
/** | ||
* function for get all files metadata | ||
* @param {Request} req | ||
* @param {Response} res | ||
*/ | ||
|
||
export async function getAllFiles(req, res) { | ||
if (!validateAPIKey(extractAPIKeyFromRequest(req))) { | ||
res.status(401).send("Not Authorized!"); | ||
return; | ||
} | ||
|
||
let resBody = await getAllFilesData(); | ||
|
||
const completeRes = {}; | ||
completeRes['data'] = resBody; | ||
|
||
res.status(200).send(completeRes); | ||
return; | ||
} |
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,61 @@ | ||
// coding=utf-8 | ||
|
||
// Copyright [2024] [SkywardAI] | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { getTable } from "./index.js"; | ||
import { FILE_TABLE } from "./types.js"; | ||
|
||
/** | ||
* @typedef FileMetadataStructure | ||
* @property {String} id Id of the uploaded file | ||
* @property {Number} bytes Size of the uploaded file | ||
* @property {Number} created_at Date of uploaded, measure in miliseconds | ||
* @property {String} filename Name of the file | ||
*/ | ||
|
||
/** | ||
* Get all files metadata currently in database | ||
* @returns {Promise<FileMetadataStructure[]>} | ||
*/ | ||
export async function getAllFilesData() { | ||
const file_table = await getTable(FILE_TABLE); | ||
|
||
let queryResult = await file_table.query().toArray(); | ||
let result = [] | ||
for (let i in queryResult) { | ||
const batch = queryResult[i]; | ||
const mid = { | ||
id: batch.id, | ||
bytes: batch.bytes, | ||
created_at: Number(batch.created_at), | ||
filename: batch.filename | ||
} | ||
result.push(mid) | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Upload file metadata into database | ||
* @param {FileMetadataStructure} fileData Metadata of the uploaded file | ||
* @returns {Boolean} Success status of storing into database | ||
*/ | ||
export async function loadFileToDatabase(fileData) { | ||
const file_table = await getTable(FILE_TABLE); | ||
|
||
await file_table.add([{ id: fileData.id, bytes: fileData.bytes, created_at: fileData.created_at, filename: fileData.filename }]) | ||
|
||
return true; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Router } from "express"; | ||
import { getAllFiles, uploadFile } from "../actions/file.js"; | ||
import multer from "multer"; | ||
|
||
export default function fileRoute() { | ||
const router = Router(); | ||
const upload = multer(); | ||
|
||
router.post('', upload.single('input'), uploadFile); | ||
router.get('', getAllFiles); | ||
|
||
return router; | ||
} |
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
Oops, something went wrong.