Skip to content

Commit

Permalink
Upload api fix (#111)
Browse files Browse the repository at this point in the history
* fix(upload): 🐛 token error debug

* fix: 🐛 remomved blur hash

* fix(upload api): 🐛 added back caption and blur hash

* fix(upload api): 🚧 removed upload api logic

* fix(upload): 🐛 removed logic , fixes ts issues

* fix(upload api): 🚧 removed logic fixed TS issues

* fix(upload api): 🚧 added seperate blur generator

* fix(upload): 🚧 added logs

* fix(upload): 🚧 added more logs

* fix: 🚧 added more logs

* fix(upload): 🐛 added correct access token access

* fix(upload): 🐛 fixed upload api issue

* refactor(upload): ♻️ removed commented code
  • Loading branch information
abhishekmg authored Jan 4, 2024

Verified

This commit was signed with the committer’s verified signature.
oliviertassinari Olivier Tassinari
1 parent 0d98a0c commit 184f74b
Showing 4 changed files with 163 additions and 27 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -88,6 +88,7 @@
"@ariakit/react-core": "0.3.1",
"@headlessui/react": "1.7.13",
"@heroicons/react": "1.0.6",
"@img/sharp-linux-x64": "0.33.1",
"@sendgrid/mail": "^8.0.0",
"@sentry/nextjs": "^7.73.0",
"@supabase/auth-helpers-nextjs": "0.5.6",
@@ -107,6 +108,7 @@
"date-fns": "2.29.3",
"form-data": "^4.0.0",
"formidable": "^3.0.0",
"image-size": "1.0.2",
"jsonwebtoken": "9.0.0",
"jwt-decode": "3.1.2",
"lodash": "4.17.21",
@@ -125,6 +127,7 @@
"react-spinners": "0.13.8",
"react-stately": "3.21.0",
"react-toastify": "9.1.2",
"sharp": "0.32.6",
"slugify": "1.6.6",
"tailwind-merge": "1.12.0",
"uniqid": "^5.4.0",
63 changes: 52 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 19 additions & 16 deletions src/pages/api/file/upload-file.ts
Original file line number Diff line number Diff line change
@@ -4,17 +4,16 @@ import { log } from "console";
import fs, { promises as fileSystem } from "fs";
import { type NextApiRequest, type NextApiResponse } from "next";
import { decode } from "base64-arraybuffer";
import { blurhashFromURL } from "blurhash-from-url";
import { IncomingForm } from "formidable";
import jwtDecode from "jwt-decode";
import isNil from "lodash/isNil";
import fetch from "node-fetch";

import {
type ImgMetadataType,
type UploadFileApiResponse,
} from "../../../types/apiTypes";
import { FILES_STORAGE_NAME, MAIN_TABLE_NAME } from "../../../utils/constants";
import { blurhashFromURL } from "../../../utils/getBlurHash";
import { isUserInACategory } from "../../../utils/helpers";
import {
apiSupabaseClient,
@@ -71,42 +70,44 @@ export default async (
})) as {
fields: { access_token?: string; category_id?: string };
files: {
file?: { filepath?: string; mimetype: string; originalFilename?: string };
file?: Array<{
filepath?: string;
mimetype: string;
originalFilename?: string;
}>;
};
};

const { error: _error } = verifyAuthToken(
data?.fields?.access_token as string,
);
const accessToken = data?.fields?.access_token?.[0];

const { error: _error } = verifyAuthToken(accessToken as string);

if (_error) {
response.status(500).json({ success: false, error: _error });
throw new Error("ERROR: token error");
throw new Error(`ERROR: token error ${_error.message}`, _error);
}

const categoryId = data?.fields?.category_id;
const categoryId = data?.fields?.category_id?.[0];

const categoryIdLogic = categoryId
? isUserInACategory(categoryId)
? categoryId
: 0
: 0;

const tokenDecode: { sub: string } = jwtDecode(
data?.fields?.access_token as string,
);
const tokenDecode: { sub: string } = jwtDecode(accessToken as string);
const userId = tokenDecode?.sub;

let contents;

if (data?.files?.file?.filepath) {
contents = await fileSystem.readFile(data?.files?.file?.filepath, {
if (data?.files?.file && data?.files?.file[0]?.filepath) {
contents = await fileSystem.readFile(data?.files?.file[0]?.filepath, {
encoding: "base64",
});
}

const fileName = data?.files?.file?.originalFilename;
const fileType = data?.files?.file?.mimetype;
const fileName = data?.files?.file?.[0]?.originalFilename;
const fileType = data?.files?.file?.[0]?.mimetype;

if (contents) {
const storagePath = `public/${userId}/${fileName}`;
@@ -134,7 +135,9 @@ export default async (
const isVideo = fileType?.includes("video");

if (!isVideo) {
const imageCaption = await query(data?.files?.file?.filepath as string);
const imageCaption = await query(
data?.files?.file?.[0]?.filepath as string,
);

const jsonResponse = (await imageCaption?.json()) as Array<{
generated_text: string;
Loading

1 comment on commit 184f74b

@vercel
Copy link

@vercel vercel bot commented on 184f74b Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.