🐞 fix: ???

This commit is contained in:
mbaharip
2023-08-10 22:21:51 +07:00
parent 97c0448d4f
commit 4b7b7d994e
7 changed files with 0 additions and 178 deletions
-26
View File
@@ -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.
-47
View File
@@ -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.
View File
-105
View File
@@ -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<ArrayBuffer>(
(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,
});
}
}