diff --git a/src/config/api.config.js b/src/config/api.config.js index 65d51d2..0c2e98d 100644 --- a/src/config/api.config.js +++ b/src/config/api.config.js @@ -19,8 +19,8 @@ module.exports = { // Use 'root' to use My Drive as starting point // Or use folder id to use a specific folder as starting point // TODO: Change when final - rootFolder: "root", - // rootFolder: "1KgPV6QB1GYT8fmn2uTfbtr9rDXqcRR0j", // Test folder + // rootFolder: "root", + rootFolder: "1KgPV6QB1GYT8fmn2uTfbtr9rDXqcRR0j", // Test folder }, // Cache control header diff --git a/src/config/site.config.js b/src/config/site.config.js index 0f20997..130aff1 100644 --- a/src/config/site.config.js +++ b/src/config/site.config.js @@ -41,26 +41,9 @@ const config = { // {year} {footerText} - Powered by guDora-index {version} ❤️ footerText: "gDrive Linker", - /* Config for files render */ - files: { - // How many files to show per page - itemsPerPage: 500, - // Max number of files to show in search result - searchResult: 10, - // Starting point of the drive - // Use 'root' to use My Drive as starting point - // Or use folder id to use a specific folder as starting point - // rootFolder: "root", // Root - // rootFolder: "1KgPV6QB1GYT8fmn2uTfbtr9rDXqcRR0j", // Test folder - rootFolder: "1p6znx1BKPsqFnyOPw49uhoc8FNglfYnD", // Protected folder - // If this set to true, any user can download or view protected files. - // If this set to false, only authorized users can download or view protected files. - // The authorized users URL will have a token in it that valid for 1 hour. - // Only used on API routes. - // - // This take too much time to load, so I disabled it for now. - // Unless someone can help me to make it faster. - allowDownloadProtectedWithoutAccess: true, + /* Experimental: Render Banner */ + banner: { + render: true, }, /* Config for readme file render */ readme: { diff --git a/src/pages/api/files/[id]/index.ts b/src/pages/api/files/[id]/index.ts index cf357ea..79c6259 100644 --- a/src/pages/api/files/[id]/index.ts +++ b/src/pages/api/files/[id]/index.ts @@ -1,4 +1,5 @@ //Accepted id is Name#PartialID +// [filename]:[partialId] import { NextApiRequest, NextApiResponse } from "next"; import { ErrorResponse, FileResponse, FilesResponse } from "types/googleapis"; @@ -15,7 +16,7 @@ export default initMiddleware(async function handler( const _start = Date.now(); try { - const { id, pageToken } = request.query; + const { id, pageToken, download } = request.query; const [name, partialId] = (id as string).split(":"); @@ -41,10 +42,40 @@ export default initMiddleware(async function handler( if (!file) { throw new ExtendedError("File not found.", 404, "notFound"); } + if (file.id === apiConfig.files.rootFolder) { + return response.status(301).redirect("/api/files"); + } response.setHeader("Cache-Control", apiConfig.cache); if (file.mimeType !== "application/vnd.google-apps.folder") { + if (download === "1") { + // Check size + if (Number(file.size as string) > apiConfig.maxResponseSize) { + return response.status(301).redirect(file.webContentLink as string); + } + + const fileStream = await driveClient.files.get( + { + fileId: file.id as string, + alt: "media", + }, + { responseType: "stream" }, + ); + + response.setHeader( + "Content-Type", + file.mimeType || "application/octet-stream", + ); + response.setHeader( + "Content-Disposition", + `attachment; filename=${encodeURIComponent(file.name as string)}`, + ); + response.setHeader("Content-Length", file.size as string); + + return response.status(200).send(fileStream.data); + } + const payload: FileResponse = { success: true, timestamp: new Date().toISOString(), diff --git a/src/pages/api/legacy/files/[id]/index.ts b/src/pages/api/legacy/files/[id]/index.ts index 45ae644..c7c4552 100644 --- a/src/pages/api/legacy/files/[id]/index.ts +++ b/src/pages/api/legacy/files/[id]/index.ts @@ -3,11 +3,11 @@ import { FileResponse, FilesResponse, TFileParent, -} from "@/types/googleapis"; -import drive from "@/utils/driveClient"; -import { buildQuery, validateProtected } from "@/utils/driveHelper"; +} from "types/googleapis"; +import drive from "utils/driveClient"; +import {} from "utils/driveHelper"; import { NextApiRequest, NextApiResponse } from "next"; -import config from "@config/site.config"; +import config from "config/site.config"; export default async function handler( request: NextApiRequest, diff --git a/src/utils/driveClient.ts b/src/utils/driveClient.ts index bc49f00..1e96279 100644 --- a/src/utils/driveClient.ts +++ b/src/utils/driveClient.ts @@ -1,7 +1,6 @@ import apiConfig from "config/api.config"; import { drive_v3, google } from "googleapis"; import { decrypt } from "utils/encryptionHelper"; -import { Redis } from "@upstash/redis"; const decryptedSecret: string = decrypt( apiConfig.client_secret, @@ -12,11 +11,6 @@ const decryptedRefreshToken: string = decrypt( process.env.ENCRYPTION_KEY as string, ); -export const redis = new Redis({ - url: process.env.REDIS_HOST as string, - token: process.env.REDIS_TOKEN as string, -}); - const oauth2Client = new google.auth.OAuth2( apiConfig.client_id, decryptedSecret,