-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
628 additions
and
124 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
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,240 @@ | ||
import multer from "multer" | ||
|
||
const APILLON_STORAGE_ENDOINT = "https://api.apillon.io/storage" | ||
|
||
const API_KEY = "94b3fc0f-fb40-494a-8fef-c416421842ae" | ||
Check failure Code scanning / CodeQL Hard-coded credentials Critical
The hard-coded value "94b3fc0f-fb40-494a-8fef-c416421842ae" is used as
authorization header Error loading related location Loading |
||
const API_SECRET = "6wlRtA8BxQId" | ||
Check failure Code scanning / CodeQL Hard-coded credentials Critical
The hard-coded value "6wlRtA8BxQId" is used as
authorization header Error loading related location Loading |
||
const CREDENTIALS = `${API_KEY}:${API_SECRET}` | ||
|
||
const storage = multer.memoryStorage() | ||
const upload = multer({ | ||
storage: storage, | ||
limits: { fileSize: 5 * 1024 * 1024 }, | ||
}) | ||
|
||
const toObject = (data: any) => { | ||
return JSON.parse(JSON.stringify(data)) | ||
} | ||
|
||
async function callAPI( | ||
method: "POST" | "GET", | ||
url: string, | ||
data: any = null, | ||
): Promise<any> { | ||
const options: RequestInit = { | ||
method, | ||
headers: { | ||
Authorization: `Basic ${btoa(CREDENTIALS)}`, | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
|
||
if (method === "POST" && data) { | ||
options.body = JSON.stringify(data) | ||
} else if (method === "GET" && data) { | ||
const params = new URLSearchParams(data).toString() | ||
url = `${url}?${params}` | ||
} | ||
|
||
try { | ||
const response = await fetch(url, options) | ||
const statusCode = response.status | ||
const result = await response.json() | ||
|
||
switch (statusCode) { | ||
case 200: | ||
case 201: | ||
// Success or creation successful | ||
break | ||
case 400: | ||
return toObject({ | ||
error: "Bad request. Check the request data and try again.", | ||
}) | ||
case 401: | ||
return toObject({ | ||
error: "Unauthorized. Invalid API key or API key secret.", | ||
}) | ||
case 403: | ||
return toObject({ | ||
error: | ||
"Forbidden. Insufficient permissions or unauthorized access to record.", | ||
}) | ||
case 404: | ||
return toObject({ | ||
error: "Path not found. Invalid endpoint or resource.", | ||
}) | ||
case 422: | ||
return toObject({ | ||
error: "Data validation failed. Invalid or missing fields.", | ||
}) | ||
case 500: | ||
return toObject({ | ||
error: "Internal server error. Please try again later.", | ||
}) | ||
default: | ||
return toObject({ error: `Received HTTP code ${statusCode}` }) | ||
} | ||
|
||
return toObject({ | ||
id: result.id ?? null, | ||
status: statusCode, | ||
data: result.data ?? null, | ||
}) | ||
} catch (error) { | ||
return toObject({ error: error.message }) | ||
} | ||
} | ||
|
||
type FileDataInput = { fileName: string; contentType: string; file: Buffer } | ||
|
||
type FileResponse = { | ||
path?: string | ||
fileName: string | ||
contentType: string | ||
fileUuid: string | ||
} | ||
|
||
// Upload to bucket | ||
async function uploadToBucket( | ||
fileData: FileDataInput[], | ||
bucketUuid: string, | ||
wrapWithDirectory = false, | ||
directoryPath = "", | ||
) { | ||
const responseJson: { | ||
status: string | ||
message: string | ||
data: FileResponse[] | ||
} = { | ||
status: "", | ||
message: "", | ||
data: [], | ||
} | ||
|
||
const files = fileData.map((data) => ({ | ||
fileName: data.fileName, | ||
contentType: data.contentType, | ||
})) | ||
|
||
if (files.length === 0) { | ||
responseJson.status = "error" | ||
responseJson.message = "No files to upload." | ||
return responseJson | ||
} | ||
|
||
const url = `${APILLON_STORAGE_ENDOINT}/buckets/${bucketUuid}/upload` | ||
const postData = { files } | ||
|
||
const response = await callAPI("POST", url, postData) | ||
|
||
if (response.error) { | ||
responseJson.status = "error" | ||
responseJson.message = `API error: ${response.error}` | ||
return responseJson | ||
} | ||
|
||
if (response.data?.sessionUuid) { | ||
const sessionUuid = response.data.sessionUuid | ||
const uploadUrls = response.data.files | ||
|
||
const uploadedFiles: { | ||
path?: string | ||
fileName: string | ||
contentType: string | ||
fileUuid: string | ||
}[] = [] | ||
|
||
// Perform file uploads | ||
for (const [index, data] of fileData.entries()) { | ||
const { url, ...fileProps } = uploadUrls[index] | ||
const file = data.file | ||
|
||
try { | ||
await fetch(url, { | ||
method: "PUT", | ||
body: file, | ||
}) | ||
|
||
uploadedFiles.push(fileProps) | ||
} catch (error) { | ||
responseJson.status = "error" | ||
responseJson.message = `File upload error: ${error.message}` | ||
return responseJson | ||
} | ||
} | ||
|
||
await endUploadSession( | ||
sessionUuid, | ||
bucketUuid, | ||
wrapWithDirectory, | ||
directoryPath, | ||
) | ||
|
||
responseJson.status = "success" | ||
responseJson.message = "Files uploaded successfully" | ||
responseJson.data = uploadedFiles | ||
} else { | ||
responseJson.status = "error" | ||
responseJson.message = "Failed to start upload session." | ||
} | ||
|
||
return responseJson | ||
} | ||
|
||
async function endUploadSession( | ||
sessionUuid: string, | ||
bucketUuid: string, | ||
wrapWithDirectory = false, | ||
directoryPath = "", | ||
) { | ||
const url = `${APILLON_STORAGE_ENDOINT}/buckets/${bucketUuid}/upload/${sessionUuid}/end` | ||
const data = { | ||
wrapWithDirectory, | ||
directoryPath, | ||
} | ||
|
||
return callAPI("POST", url, data) | ||
} | ||
|
||
export const POST = async (req, res) => { | ||
upload.single("file")(req, res, async (err) => { | ||
if (err) { | ||
return res.status(500).send({ status: "error", message: err.message }) | ||
} | ||
|
||
const bucketUuid = req.body.bucketUuid | ||
|
||
if (!bucketUuid) { | ||
return res.status(400).send({ | ||
status: "error", | ||
message: "Missing required field: bucketUuid", | ||
}) | ||
} | ||
|
||
const fileArr = req.file | ||
? [ | ||
{ | ||
fileName: req.body.fileName || req.file.originalname, | ||
contentType: req.file.mimetype, | ||
file: req.file.buffer, | ||
}, | ||
] | ||
: [] | ||
|
||
if (!fileArr.length) { | ||
return res.status(400).send({ | ||
status: "error", | ||
message: "No files to upload", | ||
}) | ||
} | ||
|
||
const response = await uploadToBucket(fileArr, bucketUuid) | ||
|
||
storage._removeFile(req, req.file, (err) => { | ||
if (err) { | ||
return res.status(500).send({ status: "error", message: err.message }) | ||
} | ||
res.send(response) | ||
}) | ||
}) | ||
} |
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,3 @@ | ||
export const GET = async (req, res) => { | ||
res.send({ status: "success", message: "Hello from the foo endpoint" }) | ||
} |
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,20 @@ | ||
import cors from "cors" | ||
import { HandlerHook } from "vite-plugin-api-routes/model" | ||
|
||
const allowedOrigins = ["https://app.hydration.net", "https://app.hydradx.io"] | ||
|
||
const corsOptions = { | ||
origin: (origin, callback) => { | ||
callback(null, true) | ||
return | ||
if (allowedOrigins.indexOf(origin) !== -1) { | ||
callback(null, true) | ||
} else { | ||
callback(new Error("Access denied")) | ||
} | ||
}, | ||
} | ||
|
||
export const handlerBefore: HandlerHook = (handler) => { | ||
handler.use(cors(corsOptions)) | ||
} |
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
Oops, something went wrong.