Merge pull request #13 from mbahArip/hf/teamdrive

Fix team drive / shared drive
This commit is contained in:
Arief Rachmawan
2024-04-24 20:22:45 +07:00
committed by GitHub
7 changed files with 113 additions and 24 deletions
+1
View File
@@ -87,6 +87,7 @@
"@types/jsonwebtoken": "^9.0.1",
"@types/mime-types": "^2.1.1",
"@types/node": "^20",
"@types/nprogress": "^0.2.3",
"@types/react": "^18",
"@types/react-dom": "^18",
"@types/three": "^0.150.2",
+6 -1
View File
@@ -11,6 +11,8 @@ import { decryptData } from "~/utils/encryptionHelper/hash";
import gdrive from "~/utils/gdriveInstance";
import { getFileType } from "~/utils/previewHelper";
import config from "~/config/gIndex.config";
import FileBrowser from "../@explorer";
import Header from "../@header";
import HeaderButton from "../@header.button";
@@ -84,7 +86,9 @@ export default async function RestPage({ params: { rest } }: Props) {
if (!unlocked.success) {
if (!unlocked.path)
throw new Error("No path returned from password checking");
throw new Error(
`No path returned from password checking, ${unlocked.message}`,
);
return (
<Password
path={unlocked.path}
@@ -102,6 +106,7 @@ export default async function RestPage({ params: { rest } }: Props) {
const { data: file } = await gdrive.files.get({
fileId: await decryptData(encryptedId),
fields: "mimeType, fileExtension",
supportsAllDrives: config.apiConfig.isTeamDrive,
});
if (!file.mimeType?.includes("folder")) {
promise.push(GetFile(encryptedId));
+83 -19
View File
@@ -120,6 +120,11 @@ export async function CheckPaths(paths: string[]): Promise<
}
> {
try {
let decryptedSharedDrive;
if (config.apiConfig.isTeamDrive && config.apiConfig.sharedDrive) {
decryptedSharedDrive = await decryptData(config.apiConfig.sharedDrive);
}
const promises = [];
for (const path of paths) {
promises.push(
@@ -127,8 +132,12 @@ export async function CheckPaths(paths: string[]): Promise<
.list({
q: `name = '${decodeURIComponent(path)}' and trashed = false`,
fields: "files(id, name, mimeType, parents)",
supportsAllDrives: config.apiConfig.isTeamDrive,
includeItemsFromAllDrives: config.apiConfig.isTeamDrive,
...(decryptedSharedDrive && {
supportsAllDrives: true,
includeItemsFromAllDrives: true,
driveId: decryptedSharedDrive,
corpora: "drive",
}),
})
.then(({ data }) => {
if (!data.files?.length) return null;
@@ -155,9 +164,13 @@ export async function CheckPaths(paths: string[]): Promise<
const decryptedRootId = await decryptData(config.apiConfig.rootFolder);
for (const [index, path] of data.entries()) {
if (!valid) break;
// if first path, check if it's in root
if (index === 0) {
if (path?.data[0].parents === decryptedRootId) break;
if (
path?.data[0].parents === decryptedRootId ||
path?.data[0].parents === decryptedSharedDrive
)
break;
} else {
if (path?.data[0].parents === data[index - 1]?.data[0].id) break;
}
@@ -207,18 +220,31 @@ export async function CheckPassword(
for (const path of paths) {
ids.push(await decryptData(path.id));
}
let decryptedSharedDrive;
if (config.apiConfig.isTeamDrive && config.apiConfig.sharedDrive) {
decryptedSharedDrive = await decryptData(config.apiConfig.sharedDrive);
}
const query: string[] = [
`name = '${config.apiConfig.specialFile.password}'`,
`trashed = false`,
`(${ids.map((id) => `'${id}' in parents`).join(" or ")})`, // Filter by paths id
];
const { data: password } = await gdrive.files.list({
q: query.join(" and "),
fields: "files(id, name, mimeType, parents)",
pageSize: 1000,
supportsAllDrives: config.apiConfig.isTeamDrive,
includeItemsFromAllDrives: config.apiConfig.isTeamDrive,
});
const { data: password } = await gdrive.files
.list({
q: query.join(" and "),
fields: "files(id, name, mimeType, parents)",
pageSize: 1000,
...(decryptedSharedDrive && {
supportsAllDrives: true,
includeItemsFromAllDrives: true,
driveId: decryptedSharedDrive,
corpora: "drive",
}),
})
.catch((error) => {
console.error(error);
throw new Error("Failed to get password file");
});
// To save processing time, skip if password file not found
if (!password.files?.length) return { success: true };
@@ -250,6 +276,7 @@ export async function CheckPassword(
{
fileId: protectedFolder.id as string,
alt: "media",
supportsAllDrives: config.apiConfig.isTeamDrive,
},
{
responseType: "text",
@@ -338,6 +365,11 @@ export async function GetFiles({
if (id) decryptedId = await decryptData(id);
else decryptedId = await decryptData(config.apiConfig.rootFolder);
let decryptedSharedDrive;
if (config.apiConfig.isTeamDrive && config.apiConfig.sharedDrive) {
decryptedSharedDrive = await decryptData(config.apiConfig.sharedDrive);
}
const filterName = config.apiConfig.hiddenFiles
.map((item) => `not name = '${item}'`)
.join(" and ");
@@ -353,8 +385,12 @@ export async function GetFiles({
orderBy: config.apiConfig.defaultOrder,
pageSize: config.apiConfig.itemsPerPage,
pageToken: pageToken,
supportsAllDrives: config.apiConfig.isTeamDrive,
includeItemsFromAllDrives: config.apiConfig.isTeamDrive,
...(decryptedSharedDrive && {
supportsAllDrives: true,
includeItemsFromAllDrives: true,
driveId: decryptedSharedDrive,
corpora: "drive",
}),
});
const encryptedData: z.infer<typeof Schema_File>[] = [];
@@ -458,6 +494,11 @@ export async function SearchFile(
nextPageToken?: string;
}> {
try {
let decryptedSharedDrive;
if (config.apiConfig.isTeamDrive && config.apiConfig.sharedDrive) {
decryptedSharedDrive = await decryptData(config.apiConfig.sharedDrive);
}
const query: string[] = [
...config.apiConfig.defaultQuery,
`name contains '${keyword}'`,
@@ -471,8 +512,12 @@ export async function SearchFile(
orderBy: "name_natural desc",
pageSize: config.apiConfig.searchResult,
pageToken: nextPageToken,
supportsAllDrives: config.apiConfig.isTeamDrive,
includeItemsFromAllDrives: config.apiConfig.isTeamDrive,
...(decryptedSharedDrive && {
supportsAllDrives: true,
includeItemsFromAllDrives: true,
driveId: decryptedSharedDrive,
corpora: "drive",
}),
});
const encryptedData: z.infer<typeof Schema_File>[] = [];
if (!data.data.files?.length)
@@ -582,6 +627,11 @@ export async function GetReadme(
if (encryptedId) decryptedId = await decryptData(encryptedId);
else decryptedId = await decryptData(config.apiConfig.rootFolder);
let decryptedSharedDrive;
if (config.apiConfig.isTeamDrive && config.apiConfig.sharedDrive) {
decryptedSharedDrive = await decryptData(config.apiConfig.sharedDrive);
}
const query: string[] = [
...config.apiConfig.defaultQuery,
`name = '${config.apiConfig.specialFile.readme}'`,
@@ -593,8 +643,12 @@ export async function GetReadme(
orderBy: config.apiConfig.defaultOrder,
pageSize: config.apiConfig.itemsPerPage,
pageToken: undefined,
supportsAllDrives: config.apiConfig.isTeamDrive,
includeItemsFromAllDrives: config.apiConfig.isTeamDrive,
...(decryptedSharedDrive && {
supportsAllDrives: true,
includeItemsFromAllDrives: true,
driveId: decryptedSharedDrive,
corpora: "drive",
}),
});
if (!data.files?.length) return null;
@@ -602,6 +656,7 @@ export async function GetReadme(
{
fileId: data.files[0].id as string,
alt: "media",
supportsAllDrives: config.apiConfig.isTeamDrive,
},
{
responseType: "text",
@@ -623,6 +678,11 @@ export async function GetBanner(
if (encryptedId) decryptedId = await decryptData(encryptedId);
else decryptedId = await decryptData(config.apiConfig.rootFolder);
let decryptedSharedDrive;
if (config.apiConfig.isTeamDrive && config.apiConfig.sharedDrive) {
decryptedSharedDrive = await decryptData(config.apiConfig.sharedDrive);
}
const query: string[] = [
...config.apiConfig.defaultQuery,
`name contains '${config.apiConfig.specialFile.banner}'`,
@@ -634,8 +694,12 @@ export async function GetBanner(
orderBy: config.apiConfig.defaultOrder,
pageSize: config.apiConfig.itemsPerPage,
pageToken: undefined,
supportsAllDrives: config.apiConfig.isTeamDrive,
includeItemsFromAllDrives: config.apiConfig.isTeamDrive,
...(decryptedSharedDrive && {
supportsAllDrives: true,
includeItemsFromAllDrives: true,
driveId: decryptedSharedDrive,
corpora: "drive",
}),
});
if (!data.files?.length) return null;
@@ -1,4 +1,5 @@
import { NextRequest, NextResponse } from "next/server";
import {
CheckDownloadToken,
CheckPassword,
+19 -3
View File
@@ -6,7 +6,7 @@ const config: z.input<typeof Schema_Config> = {
* If possible, please don't change this value
* Even if you're creating a PR, just let me change it myself
*/
version: "2.0.1",
version: "2.0.2",
/**
* Base path of the app, used for generating links
*
@@ -48,9 +48,25 @@ const config: z.input<typeof Schema_Config> = {
* You need to create a new folder and share it with the service account
* Then, copy the folder id and paste it here
*/
// rootFolder:
// "c760fc0eae9990d4accbc2134af21e45a378d412af2c78020070a9f9ac548b98fe61c4f6be953a8d7be6a035e6f7766c",
rootFolder:
"c760fc0eae9990d4accbc2134af21e45a378d412af2c78020070a9f9ac548b98fe61c4f6be953a8d7be6a035e6f7766c",
isTeamDrive: false, // Set this to true if you're using Team Drive
"b76c7c22083307a3aa99c28ab7cc69851d682f5a250d995679d4be5276cab16ab6c37f4d5b7ad1a9b93fb9bf768e752c",
/**
* If your root folder inside a shared drive, set this to true
* If not, set this to false
*
* You need to set the shared drive id to make it work
* Where to get the shared drive id?
* Go to your Shared Drives -> Click on the shared drive -> Copy the id from the url
* ex: https://drive.google.com/drive/u/0/folders/:shared_drive_id
*
* Then encrypt it using `/api/internal/encrypt?q=:shared_drive_id` route
*/
isTeamDrive: true,
sharedDrive:
"77bfa156c9c9d159112fcb0494ed8545bdaf7a3d567cd760ba2e2e2cd33fcbfc",
defaultQuery: [
"trashed = false",
"(not mimeType contains 'google-apps' or mimeType contains 'folder')",
+1
View File
@@ -42,6 +42,7 @@ export const Schema_Config = z.object({
apiConfig: z.object({
rootFolder: z.string(),
isTeamDrive: z.boolean(),
sharedDrive: z.string().optional(),
defaultQuery: z.array(z.string()),
defaultField: z.string(),
defaultOrder: z.string(),
+2 -1
View File
@@ -1924,7 +1924,7 @@ __metadata:
languageName: node
linkType: hard
"@types/nprogress@npm:^0.2.2":
"@types/nprogress@npm:^0.2.2, @types/nprogress@npm:^0.2.3":
version: 0.2.3
resolution: "@types/nprogress@npm:0.2.3"
checksum: 10c0/cac0fe73aca79bc1472a1556f56303df72026f2fcd8dbe311fe84b1f46e454b9040c1e69223d94e3bf156a5986382170c52ebda19aa70f555f1b6855d8f744a6
@@ -6160,6 +6160,7 @@ __metadata:
"@types/jsonwebtoken": "npm:^9.0.1"
"@types/mime-types": "npm:^2.1.1"
"@types/node": "npm:^20"
"@types/nprogress": "npm:^0.2.3"
"@types/react": "npm:^18"
"@types/react-dom": "npm:^18"
"@types/three": "npm:^0.150.2"