mirror of
https://github.com/Nezumi-2711/next-gdrive-index.git
synced 2026-09-22 13:38:38 +00:00
Add download function for files api route
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user