mirror of
https://github.com/Nezumi-2711/next-gdrive-index.git
synced 2026-09-23 04:10:03 +00:00
update: split all actions based on usage
This commit is contained in:
+26
-3
@@ -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 {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
message: "Token created",
|
message: "Content found",
|
||||||
data: "",
|
data: data as string,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ export async function GetFilePaths(fileName: string, parentId?: string): Promise
|
|||||||
const { data } = await gdrive.files.get({
|
const { data } = await gdrive.files.get({
|
||||||
fileId: parentIdTemp,
|
fileId: parentIdTemp,
|
||||||
fields: "id,name,parents",
|
fields: "id,name,parents",
|
||||||
|
supportsAllDrives: config.apiConfig.isTeamDrive,
|
||||||
});
|
});
|
||||||
if (!data.name) break;
|
if (!data.name) break;
|
||||||
|
|
||||||
|
|||||||
@@ -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,
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user