update: split all actions based on usage

This commit is contained in:
mbaharip
2025-01-21 21:59:09 +07:00
parent 3ce78a7278
commit bc0467cd97
3 changed files with 98 additions and 3 deletions
+26 -3
View File
@@ -342,10 +342,33 @@ export async function GetBanner(id: string | null = null): Promise<ActionRespons
};
}
export async function CreateFileToken(id: string): Promise<ActionResponseSchema<string>> {
/**
* Get content of text file
* @param id - File ID to fetch
*/
export async function GetContent(id: string): Promise<ActionResponseSchema<string>> {
const decryptedId = await encryptionService.decrypt(id);
const { data, status, statusText } = await gdrive.files.get(
{
fileId: decryptedId,
alt: "media",
supportsAllDrives: config.apiConfig.isTeamDrive,
},
{
responseType: "text",
},
);
if (status !== 200)
return {
success: false,
message: "Failed to fetch content",
error: statusText,
};
return {
success: true,
message: "Token created",
data: "",
message: "Content found",
data: data as string,
};
}
+1
View File
@@ -36,6 +36,7 @@ export async function GetFilePaths(fileName: string, parentId?: string): Promise
const { data } = await gdrive.files.get({
fileId: parentIdTemp,
fields: "id,name,parents",
supportsAllDrives: config.apiConfig.isTeamDrive,
});
if (!data.name) break;
+71
View File
@@ -0,0 +1,71 @@
"use server";
import { type z } from "zod";
import { type ActionResponseSchema } from "~/types";
import { encryptionService } from "~/lib/utils.server";
import { type Schema_File, Schema_FileToken } from "~/types/schema";
import config from "config";
/**
* Create a token to download a file
* @param file - File data
* @param expiredIn - Token expiration time in milliseconds (default to configuration)
*/
export async function CreateFileToken(
file: z.infer<typeof Schema_File>,
expiredIn: number = 3600 * 1000 * config.apiConfig.temporaryTokenDuration,
): Promise<ActionResponseSchema<string>> {
const tokenObject = {
id: file.encryptedId,
exp: Date.now() + expiredIn,
iat: Date.now(),
};
const parsedTokenObject = Schema_FileToken.safeParse(tokenObject);
if (!parsedTokenObject.success)
return {
success: false,
message: "Failed to create token",
error: parsedTokenObject.error.message,
};
const token = await encryptionService.encrypt(JSON.stringify(parsedTokenObject.data));
return {
success: true,
message: "Token created",
data: token,
};
}
/**
* Validate a file token and return the token data
* @param token - The token to validate
*/
export async function ValidateFileToken(
token: string,
): Promise<ActionResponseSchema<z.infer<typeof Schema_FileToken>>> {
const decryptedToken = await encryptionService.decrypt(token);
const parsedToken = Schema_FileToken.safeParse(JSON.parse(decryptedToken));
if (!parsedToken.success)
return {
success: false,
message: "Failed to validate token",
error: parsedToken.error.message,
};
const currentTime = Date.now();
if (parsedToken.data.exp < currentTime)
return {
success: false,
message: "Token expired",
error: "Download URL expired",
};
return {
success: true,
message: "Token validated",
data: parsedToken.data,
};
}