mirror of
https://github.com/Nezumi-2711/next-gdrive-index.git
synced 2026-09-22 20:01:36 +00:00
119 lines
2.6 KiB
TypeScript
119 lines
2.6 KiB
TypeScript
import drive from "@utils/driveClient";
|
|
import config from "@config/site.config";
|
|
import { verifyHash } from "@utils/hashHelper";
|
|
import { TFileParent } from "@/types/googleapis";
|
|
|
|
export function buildQuery({
|
|
id,
|
|
extraQuery,
|
|
globalSearch = false,
|
|
}: {
|
|
id?: string;
|
|
extraQuery?: string[];
|
|
globalSearch?: boolean;
|
|
}) {
|
|
const query = [
|
|
"name != '.password'",
|
|
"'me' in owners",
|
|
"trashed = false",
|
|
// "mimeType != 'application/vnd.google-apps.shortcut'",
|
|
];
|
|
|
|
if (id && !globalSearch) {
|
|
query.unshift(`'${id}' in parents`);
|
|
}
|
|
if (!id && !globalSearch) {
|
|
query.unshift(`'${config.files.rootFolder}' in parents`);
|
|
}
|
|
|
|
if (extraQuery) {
|
|
query.unshift(...extraQuery);
|
|
}
|
|
|
|
return query.join(" and ");
|
|
}
|
|
|
|
export async function _checkProtected(id: string) {
|
|
try {
|
|
const files = await drive.files.list({
|
|
q: id ? buildQuery({ id }) : buildQuery({}),
|
|
fields: "files(id)",
|
|
});
|
|
|
|
return {
|
|
protected: files.data.files?.length,
|
|
id: files.data.files?.[0].id || null,
|
|
};
|
|
} catch (error: any) {
|
|
return {
|
|
protected: false,
|
|
id: null,
|
|
};
|
|
}
|
|
}
|
|
|
|
export async function _validateFolderPassword(
|
|
passwordFileId: string,
|
|
password: string,
|
|
) {
|
|
try {
|
|
const folderPassword = await drive.files.get(
|
|
{
|
|
fileId: passwordFileId,
|
|
alt: "media",
|
|
},
|
|
{ responseType: "text" },
|
|
);
|
|
|
|
return folderPassword.data === password;
|
|
} catch (error: any) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function validateProtected(
|
|
fileId: string | TFileParent[],
|
|
passwordHash: string,
|
|
): Promise<{ isProtected: boolean; valid?: boolean }> {
|
|
const fetchPassword = await drive.files.list({
|
|
q: `name = '.password' and 'me' in owners and trashed = false`,
|
|
fields: "files(id, name, parents)",
|
|
pageSize: 1000,
|
|
});
|
|
let passwordFile;
|
|
if (typeof fileId === "string") {
|
|
passwordFile = fetchPassword.data.files?.find(
|
|
(file) => file.parents?.[0] === fileId,
|
|
);
|
|
}
|
|
if (Array.isArray(fileId)) {
|
|
const parentsIdMap = fileId.map((parent) => parent.id);
|
|
passwordFile = fetchPassword.data.files?.find((file) =>
|
|
parentsIdMap.includes(file.parents?.[0] as string),
|
|
);
|
|
}
|
|
|
|
console.log(fetchPassword.data.files);
|
|
|
|
if (!passwordFile) return { isProtected: false };
|
|
|
|
const getPassword = await drive.files.get(
|
|
{
|
|
fileId: passwordFile.id as string,
|
|
alt: "media",
|
|
},
|
|
{ responseType: "text" },
|
|
);
|
|
|
|
if (!passwordHash)
|
|
return {
|
|
isProtected: true,
|
|
valid: false,
|
|
};
|
|
|
|
return {
|
|
isProtected: true,
|
|
valid: verifyHash(getPassword.data as string, passwordHash),
|
|
};
|
|
}
|