From eccc4e5460b11055dceea2c830d51a48efcb9c37 Mon Sep 17 00:00:00 2001 From: nheek Date: Fri, 25 Oct 2024 00:40:15 +0200 Subject: [PATCH] refactors --- .github/workflows/deploy.yml | 7 ++++--- Dockerfile | 22 -------------------- Dockerfile.dev | 13 ------------ docker-compose.yml | 4 ---- src/app/api/config.ts | 1 - src/app/api/files/route.ts | 1 - src/app/api/register/route.ts | 1 - src/app/api/upload-cropped/route.ts | 6 +----- src/app/api/upload/route.ts | 5 +---- src/app/components/FileUpload/FileUpload.tsx | 2 -- src/app/components/Index/Index.tsx | 6 +++--- src/app/components/Modal/Modal.tsx | 4 ++-- src/app/components/MyFiles/MyFiles.tsx | 4 +--- src/app/components/Register/Register.tsx | 4 ++-- src/app/layout.tsx | 2 +- 15 files changed, 15 insertions(+), 67 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index ad9addd..4d94931 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -51,12 +51,13 @@ jobs: echo "DOCKERHUB_USERNAME=${{ secrets.DOCKERHUB_USERNAME }}" >> .env echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" >> .env echo "DOMAIN=${{ secrets.DOMAIN }}" >> .env - curl -H "Authorization: token ${{ secrets.PERSONAL_ACCESS_TOKEN }}" -O -L https://raw.githubusercontent.com/nheek/uppy/main/Dockerfile - curl -H "Authorization: token ${{ secrets.PERSONAL_ACCESS_TOKEN }}" -O -L https://raw.githubusercontent.com/nheek/uppy/main/docker-compose.yml + curl -O -L https://raw.githubusercontent.com/nheek/uppy/main/Dockerfile + curl -O -L https://raw.githubusercontent.com/nheek/uppy/main/docker-compose.yml mkdir .sql_data cd .sql_data - curl -H "Authorization: token ${{ secrets.PERSONAL_ACCESS_TOKEN }}" -O -L https://raw.githubusercontent.com/nheek/uppy/main/.sql_data/create_database.sql + curl -O -L https://raw.githubusercontent.com/nheek/uppy/main/.sql_data/create_database.sql + cd .. docker compose pull docker compose up -d && curl -d "uppy deployed succesfully 🚀" https://ntfy.nheek.com/${{ secrets.NTFY_TOPIC }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 137eed7..7bfff8a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,37 +1,15 @@ -# Stage 1: Build Stage FROM node:21-alpine AS build - -# Set the working directory inside the container WORKDIR /usr/src/app - -# Copy package.json and package-lock.json to the working directory COPY package*.json ./ - -# Install dependencies RUN npm install - -# Copy the rest of the application code to the working directory COPY . . - -# Build the Next.js app RUN npm run build -# Stage 2: Production Stage FROM node:21-alpine AS production - -# Set the working directory inside the container WORKDIR /usr/src/app - -# Install PostgreSQL client RUN apk --no-cache add postgresql-client - -# Install bash and wait-for-it script RUN apk --no-cache add bash \ && wget -O /usr/local/bin/wait-for-it.sh https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh \ && chmod +x /usr/local/bin/wait-for-it.sh - -# Copy built assets from the build stage COPY --from=build /usr/src/app . - -# Start PostgreSQL client and wait for it to be ready, then run the app CMD ["sh", "-c", "pg_isready -h postgres && npm run start"] \ No newline at end of file diff --git a/Dockerfile.dev b/Dockerfile.dev index 426766d..b6cba87 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -1,21 +1,8 @@ -# Stage 1: Development Stage FROM node:21-alpine - -# Set the working directory inside the container WORKDIR /usr/src/app - -# Install dependencies COPY package*.json ./ RUN npm install - -# Copy the rest of the application code to the working directory COPY . . - -# Expose the port Next.js runs on EXPOSE 3000 - -# Set environment variables to enable hot-reloading ENV CHOKIDAR_USEPOLLING=true - -# Start the Next.js app in development mode CMD ["npm", "run", "dev"] diff --git a/docker-compose.yml b/docker-compose.yml index 6cbc199..ef899a2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,8 +8,6 @@ services: - postgres env_file: - .env - environment: - - PLACEHOLDER=YES image: "nheek/uppy:latest" volumes: - uploads:/usr/src/app/public/uploads @@ -19,8 +17,6 @@ services: restart: unless-stopped env_file: - .env - environment: - PLACEHOLDER: "YES" volumes: - postgres_db_data:/var/lib/postgresql/data - .sql_data:/docker-entrypoint-initdb.d/ diff --git a/src/app/api/config.ts b/src/app/api/config.ts index cdfea66..dcd8a6e 100644 --- a/src/app/api/config.ts +++ b/src/app/api/config.ts @@ -4,7 +4,6 @@ const { Pool } = pkg; dotenv.config(); -// Create a PostgreSQL pool using environment variables export const pool = new Pool({ connectionString: process.env.POSTGRES_URL, }); diff --git a/src/app/api/files/route.ts b/src/app/api/files/route.ts index 6d0f9cf..2471b8e 100644 --- a/src/app/api/files/route.ts +++ b/src/app/api/files/route.ts @@ -6,7 +6,6 @@ const JWT_SECRET = process.env.JWT_SECRET; export async function GET(request: Request) { try { - // Verify the JWT token const authHeader = request.headers.get("authorization"); const token = authHeader?.split(" ")[1]; diff --git a/src/app/api/register/route.ts b/src/app/api/register/route.ts index c70fd1f..afb8de1 100644 --- a/src/app/api/register/route.ts +++ b/src/app/api/register/route.ts @@ -4,7 +4,6 @@ import { NextRequest, NextResponse } from "next/server"; const saltRounds = 10; -// POST method handler export async function POST(req: NextRequest) { const { username, password, confirmPassword } = await req.json(); // Use .json() to parse the request body diff --git a/src/app/api/upload-cropped/route.ts b/src/app/api/upload-cropped/route.ts index 34173fc..9e896dc 100644 --- a/src/app/api/upload-cropped/route.ts +++ b/src/app/api/upload-cropped/route.ts @@ -5,13 +5,9 @@ import path from "path"; export const POST = async (request: Request) => { try { const data = await request.json(); - const { croppedImage, savedName } = data; // assuming these are sent from the frontend - - // Decode the base64 image string + const { croppedImage, savedName } = data; const base64Data = croppedImage.replace(/^data:image\/\w+;base64,/, ""); const buffer = Buffer.from(base64Data, "base64"); - - // Define the upload directory const uploadDir = path.join(process.cwd(), "public/uploads"); const filePath = path.join(uploadDir, savedName); diff --git a/src/app/api/upload/route.ts b/src/app/api/upload/route.ts index c8ce94c..f4b1c19 100644 --- a/src/app/api/upload/route.ts +++ b/src/app/api/upload/route.ts @@ -9,7 +9,6 @@ const JWT_SECRET = process.env.JWT_SECRET; export async function POST(request: Request) { try { - // Verify the JWT token const authHeader = request.headers.get("authorization"); const token = authHeader?.split(" ")[1]; @@ -28,8 +27,6 @@ export async function POST(request: Request) { } const decoded = jwt.verify(token, JWT_SECRET) as { id: number }; - - // Parse formData from the request const formData = await request.formData(); const file = formData.get("file") as File | null; @@ -57,7 +54,7 @@ export async function POST(request: Request) { } // Validate file size (10 MB limit) - const MAX_SIZE = 10 * 1024 * 1024; // 10 MB + const MAX_SIZE = 10 * 1024 * 1024; if (file.size > MAX_SIZE) { return NextResponse.json( { message: "File size exceeds the 10 MB limit" }, diff --git a/src/app/components/FileUpload/FileUpload.tsx b/src/app/components/FileUpload/FileUpload.tsx index fb497d8..ebe9496 100644 --- a/src/app/components/FileUpload/FileUpload.tsx +++ b/src/app/components/FileUpload/FileUpload.tsx @@ -18,14 +18,12 @@ const FileUpload = () => { setIsModalVisible(true); }; - // Check if user is logged in useEffect(() => { const token = Cookies.get("token"); setIsLoggedIn(!!token); setLoading(false); }, []); - // Render a loading spinner while checking login status if (loading) { return (
diff --git a/src/app/components/Index/Index.tsx b/src/app/components/Index/Index.tsx index 764321a..994321b 100644 --- a/src/app/components/Index/Index.tsx +++ b/src/app/components/Index/Index.tsx @@ -1,9 +1,9 @@ -"use client"; // Ensure this is included +"use client"; import { useEffect, useState } from "react"; import Link from "next/link"; import Cookies from "js-cookie"; -import { useRouter } from "next/navigation"; // Use next/navigation for client components +import { useRouter } from "next/navigation"; const Index = () => { const [isAuthenticated, setIsAuthenticated] = useState(false); @@ -17,7 +17,7 @@ const Index = () => { const handleLogout = () => { Cookies.remove("token"); setIsAuthenticated(false); - router.push("/"); // Redirect to homepage + router.push("/"); }; return ( diff --git a/src/app/components/Modal/Modal.tsx b/src/app/components/Modal/Modal.tsx index 2be0346..b1db1c6 100644 --- a/src/app/components/Modal/Modal.tsx +++ b/src/app/components/Modal/Modal.tsx @@ -5,8 +5,8 @@ interface ModalProps { title: string; message: string; onClose: () => void; - children?: ReactNode; // Optional children prop - closeBtn?: boolean; // Optional closeBtn prop + children?: ReactNode; + closeBtn?: boolean; } const Modal: React.FC = ({ diff --git a/src/app/components/MyFiles/MyFiles.tsx b/src/app/components/MyFiles/MyFiles.tsx index 51c9879..f1f7d22 100644 --- a/src/app/components/MyFiles/MyFiles.tsx +++ b/src/app/components/MyFiles/MyFiles.tsx @@ -58,15 +58,13 @@ const MyFiles = () => { setIsLoggedIn(true); fetchFiles(); } - setLoading(false); // Once token check is done, stop loading + setLoading(false); }, []); - // Render a loading spinner or nothing while checking login status if (loading) { return
Loading...
; } - // Render a message if the user is not logged in if (!isLoggedIn) { return (
diff --git a/src/app/components/Register/Register.tsx b/src/app/components/Register/Register.tsx index 009dd0c..bcee028 100644 --- a/src/app/components/Register/Register.tsx +++ b/src/app/components/Register/Register.tsx @@ -4,7 +4,7 @@ import { useRouter } from "next/navigation"; const Register = () => { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); - const [confirmPassword, setConfirmPassword] = useState(""); // New state for confirm password + const [confirmPassword, setConfirmPassword] = useState(""); const router = useRouter(); const handleRegister = async (e: React.FormEvent) => { @@ -21,7 +21,7 @@ const Register = () => { headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ username, password, confirmPassword }), // Include confirmPassword in the request + body: JSON.stringify({ username, password, confirmPassword }), }); if (response.ok) { diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 842da04..967018a 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -11,7 +11,7 @@ export default function RootLayout({ - Uppy + Uppy - File hosting/sharing platform {children}