From 4b7b7d994e451ae08dac0d89d71fb71acadb729c Mon Sep 17 00:00:00 2001 From: mbaharip <62494292+mbahArip@users.noreply.github.com> Date: Thu, 10 Aug 2023 22:21:51 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=20=3F=3F=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/setup/FinalStep.md | 26 ----- public/setup/GoogleCloudStep.md | 47 -------- public/setup/googleCloud.md | 0 .../api/banner/[encryptedFileId]/route.ts | 0 src/app/(api)/api/banner/route.ts | 105 ------------------ .../api/thumbnail/[encryptedFileId]/route.ts | 0 src/app/(api)/api/thumbnail/route.ts | 0 7 files changed, 178 deletions(-) delete mode 100644 public/setup/FinalStep.md delete mode 100644 public/setup/GoogleCloudStep.md create mode 100644 public/setup/googleCloud.md create mode 100644 src/app/(api)/api/banner/[encryptedFileId]/route.ts delete mode 100644 src/app/(api)/api/banner/route.ts create mode 100644 src/app/(api)/api/thumbnail/[encryptedFileId]/route.ts create mode 100644 src/app/(api)/api/thumbnail/route.ts diff --git a/public/setup/FinalStep.md b/public/setup/FinalStep.md deleted file mode 100644 index 405dcec..0000000 --- a/public/setup/FinalStep.md +++ /dev/null @@ -1,26 +0,0 @@ -To finishing the setup, you need to do the following steps: - -## API Config -The API Config file are located at `src/config/api.ts`. You need to fill in the following information: -```js -module.exports = { - client_id: "lorem", - client_secret: "ipsum", - refresh_token: "dolor", -} -``` - -## Environment Config -The environment variables are differ for each hosting service. If you are using Vercel, you can add the environment variables on the project settings page. -```dotenv -ENCRYPTION_KEY="Encryption" -``` - -**NOTE:** Make sure to redeploy the site after adding the environment variables. - -## Customizing the Site -The site can be customized by editing the `src/config/site.ts` file. You can change the site title, description, and other information. -All the explanation also included in the file. - - -And that's it, you're done with the setup. You can now start using the site. \ No newline at end of file diff --git a/public/setup/GoogleCloudStep.md b/public/setup/GoogleCloudStep.md deleted file mode 100644 index 5425a23..0000000 --- a/public/setup/GoogleCloudStep.md +++ /dev/null @@ -1,47 +0,0 @@ -# Google Cloud Setup - -Table of Contents: -- [Making a Google Cloud Project and enabling Google Drive API](#making-a-google-cloud-project-and-enabling-google-drive-api) -- [Creating Oauth Consent Screen](#creating-oauth-consent-screen) -- [Creating Oauth Client ID](#creating-oauth-client-id) -- [Getting Refresh Token](#getting-refresh-token) - -## Making a Google Cloud Project and enabling Google Drive API -1. Open [Google Cloud Console](https://console.cloud.google.com/). -2. Click on the project selector dropdown in the top left corner and select or create a new project. -![google-cloud-1.png](../images/setup/google-cloud-1.png) -3. Activate the [Google Drive API](https://console.cloud.google.com/apis/library/drive.googleapis.com) for your project. -![google-cloud-3.png](../images/setup/google-cloud-3.png) -4. After activating the API, you will be redirected to the API overview page. For creating Oauth Client ID, you need to create Oauth consent first, you can do this by clicking on the "Oauth Consent Screen" menu. - -## Creating Oauth Consent Screen -1. On the Oauth consent screen page, select "External" as the user type and click on the "Create" button. -![google-cloud-5.png](../images/setup/google-cloud-5.png) -2. On the next page, fill in the required information and click on the "Save" button. -(In my case I'm only filling `App name`, `User support email`, and `Developer contact information`.) -3. On the **Scopes** Page, select the following scopes and click on the "Save" button, then click on the "Continue" button. -![google-cloud-6.png](../images/setup/google-cloud-6.png) -4. On the **Test Users** Page, click on the "Add Users" button and add your email address or any other email address that you want to use. Then click on the "Save and Continue" button. -5. And you're done with the Oauth consent screen. Now you can create the Oauth Client ID by clicking on the "Credentials" menu. -**NOTE: We don't need to publish the app, since we're only using it for our own purpose.** - -## Creating Oauth Client ID -1. On the **Credentials** page, click on the "Create Credentials" button and select "Oauth Client ID". -![google-cloud-4.png](../images/setup/google-cloud-4.png) -2. On the next page, select "Web Application" as the application type, and fill in the required information. -3. In the **Authorized redirect URIs** section, add the following redirect URI: `https://developers.google.com/oauthplayground`, then click on the "Create" button. -4. After creating the Oauth Client ID, you will be redirected to the **Credentials** page. Then copy the Client ID, and Client Secret. (You will need them later on the bottom of this page, and to fetch refresh token.) - -## Getting Refresh Token -1. Open [Google OAuth Playground](https://developers.google.com/oauthplayground/). -2. Click on the gear icon on the upper right corner and check the box next to "Use your own OAuth credentials" and paste the Client ID and Client Secret that you copied earlier. -![google-cloud-7.png](../images/setup/google-cloud-7.png) -3. Select the following scopes and click on the "Authorize APIs" button. -![google-cloud-8.png](../images/setup/google-cloud-8.png) -4. Google will ask you to log in to your Google account, you need to log in using the account that specified on Test User (Creating Oauth Consent Screen - step 4). -5. It will show you a warning that the app is not verified, simply click on "Continue" button, and it will redirect you back to the Playground. -![google-cloud-9.png](../images/setup/google-cloud-9.png) -6. Click on the "Exchange authorization code for tokens" button, and it will show you the refresh token. Copy the refresh token and paste it on the bottom of this page. -![google-cloud-10.png](../images/setup/google-cloud-10.png) -7. You can close the Playground tab now. -8. You're done with the Google Cloud Setup. \ No newline at end of file diff --git a/public/setup/googleCloud.md b/public/setup/googleCloud.md new file mode 100644 index 0000000..e69de29 diff --git a/src/app/(api)/api/banner/[encryptedFileId]/route.ts b/src/app/(api)/api/banner/[encryptedFileId]/route.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/app/(api)/api/banner/route.ts b/src/app/(api)/api/banner/route.ts deleted file mode 100644 index 7cb7f76..0000000 --- a/src/app/(api)/api/banner/route.ts +++ /dev/null @@ -1,105 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; - -import createErrorPayload from "utils/apiHelper/createErrorPayload"; -import gdrive from "utils/apiHelper/gdrive"; -import getSearchParams from "utils/apiHelper/getSearchParams"; -import shortEncryption from "utils/encryptionHelper/shortEncryption"; -import ExtendedError from "utils/generalHelper/extendedError"; - -import { Constant } from "types/general/constant"; - -import apiConfig from "config/api.config"; - -export async function GET(request: NextRequest) { - const _start = Date.now(); - try { - const { id } = getSearchParams(request.url, ["id"]); - if (!id) { - throw new ExtendedError( - Constant.apiBadRequest, - 400, - "badRequest", - "Missing id", - ); - } - - const getMetadata = gdrive.files.get({ - fileId: shortEncryption.decrypt(id), - fields: "id, name, mimeType, webContentLink", - }); - const getStream = gdrive.files.get( - { - fileId: shortEncryption.decrypt(id), - alt: "media", - }, - { responseType: "stream" }, - ); - - const [metadata, stream] = await Promise.all([ - getMetadata, - getStream, - ]); - if (!metadata || metadata.data.trashed) { - const msg = metadata.data.trashed - ? "File has been deleted" - : "File not found"; - throw new ExtendedError( - Constant.apiFileNotFound, - 404, - "notFound", - msg, - ); - } - - if ( - Number(metadata.data.size) > - apiConfig.files.download.maxFileSize && - apiConfig.files.download.maxFileSize > 0 - ) { - return NextResponse.redirect( - metadata.data.webContentLink as string, - { status: 302 }, - ); - } - - const arrayBuffer = await new Promise( - (resolve, reject) => { - const chunks: Buffer[] = []; - stream.data.on("data", (chunk) => - chunks.push(chunk), - ); - stream.data.on("end", () => { - const buffer = Buffer.concat(chunks); - resolve( - buffer.buffer.slice( - buffer.byteOffset, - buffer.byteOffset + buffer.byteLength, - ), - ); - }); - stream.data.on("error", reject); - }, - ); - - return new NextResponse(arrayBuffer, { - status: 200, - headers: { - "Content-Type": - metadata.data.mimeType || - "application/octet-stream", - "Cache-Control": apiConfig.cacheControl, - "Content-Disposition": `inline; filename="${metadata.data.name}"`, - }, - }); - } catch (error: any) { - const payload = createErrorPayload( - error, - "GET /api/banner", - _start, - ); - - return NextResponse.json(payload, { - status: payload.code || 500, - }); - } -} diff --git a/src/app/(api)/api/thumbnail/[encryptedFileId]/route.ts b/src/app/(api)/api/thumbnail/[encryptedFileId]/route.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/app/(api)/api/thumbnail/route.ts b/src/app/(api)/api/thumbnail/route.ts new file mode 100644 index 0000000..e69de29