Skip to content

Commit

Permalink
Refactor import paths in codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
salim laimeche committed Jul 27, 2024
1 parent 88ed0b6 commit f5ec3a2
Show file tree
Hide file tree
Showing 71 changed files with 353 additions and 229 deletions.
63 changes: 39 additions & 24 deletions app/api/detection/route.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { NextRequest, NextResponse } from 'next/server'
import { UserView } from '@/app/lib/identity/definition'
import { deleteSession } from '@/app/lib/identity/session-local'
import { jwtVerify } from 'jose'
import { BlobServiceClient, StorageSharedKeyCredential } from '@azure/storage-blob'
import * as dotenv from 'dotenv'
import { generateSasToken } from '@/app/lib/send-detection/action'
import { sendDetection } from '@/app/lib/telegram-bot/action'
import { NextRequest, NextResponse } from "next/server"
import { UserView } from "@/lib/identity/definition"
import { deleteSession } from "@/lib/identity/session-local"
import { jwtVerify } from "jose"
import {
BlobServiceClient,
StorageSharedKeyCredential,
} from "@azure/storage-blob"
import * as dotenv from "dotenv"
import { generateSasToken } from "@/lib/send-detection/action"
import { sendDetection } from "@/lib/telegram-bot/action"

const { v4: uuidv4 } = require('uuid')
const { v4: uuidv4 } = require("uuid")
dotenv.config()

const secretKey = process.env.AUTH_SECRET
const accountName = process.env.AUTH_AZURE_ACCOUNT as string
const accountKey = process.env.AUTH_AZURE_ACCESS_KEY as string

if (!secretKey) throw Error('AUTH_SECRET not found')
if (!accountName) throw Error('Azure Storage accountName not found')
if (!accountKey) throw Error('Azure Storage accountKey not found')
if (!secretKey) throw Error("AUTH_SECRET not found")
if (!accountName) throw Error("Azure Storage accountName not found")
if (!accountKey) throw Error("Azure Storage accountKey not found")

const key = new TextEncoder().encode(secretKey)

Expand All @@ -25,13 +28,16 @@ export async function POST(req: NextRequest) {
const { detected, picture, cookie } = await req.json()

if (!picture || !detected || !cookie) {
return NextResponse.json({ message: 'Image is required' }, { status: 400 })
return NextResponse.json(
{ message: "Image is required" },
{ status: 400 }
)
}

// Process and validate the cookie
const { payload } = await jwtVerify(cookie, key, {
algorithms: ['HS256'],
}).catch((e) => {
algorithms: ["HS256"],
}).catch(e => {
console.error(e)
deleteSession()
throw Error(`Unauthorized user : ${e} ------ ${cookie}`)
Expand All @@ -47,14 +53,20 @@ export async function POST(req: NextRequest) {

// Process the image
// Convertir l'image base64 en Buffer
const buffer = Buffer.from(picture, 'base64')
const buffer = Buffer.from(picture, "base64")

if (!buffer.length) {
return NextResponse.json({ message: 'Failed to process image' }, { status: 400 })
return NextResponse.json(
{ message: "Failed to process image" },
{ status: 400 }
)
}

// Upload the image to Azure Blob Storage
const sharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey)
const sharedKeyCredential = new StorageSharedKeyCredential(
accountName,
accountKey
)
const blobServiceClient = new BlobServiceClient(
`https://${accountName}.blob.core.windows.net`,
sharedKeyCredential
Expand All @@ -64,23 +76,26 @@ export async function POST(req: NextRequest) {
const blockBlobClient = containerClient.getBlockBlobClient(blobName)

await blockBlobClient.upload(buffer, buffer.length, {
blobHTTPHeaders: { blobContentType: 'image/png' },
blobHTTPHeaders: { blobContentType: "image/png" },
})
await blockBlobClient.setMetadata({ class: detected.class })

// Generate SAS token for the image URL
const imageUrl = await generateSasToken(user.container, blobName)
const message = `Detection: ${detected.class}, Confidence: ${detected.score.toPrecision(
2
)} % \n Image: ${imageUrl}`
const message = `Detection: ${
detected.class
}, Confidence: ${detected.score.toPrecision(2)} % \n Image: ${imageUrl}`
await sendDetection(user.chatid, message)

return NextResponse.json({ message: 'Detection sent successfully' })
return NextResponse.json({ message: "Detection sent successfully" })
} catch (e) {
console.error(e)
if (e instanceof Error) {
return NextResponse.json({ message: e.message }, { status: 400 })
}
return NextResponse.json({ message: 'Internal server error' }, { status: 500 })
return NextResponse.json(
{ message: "Internal server error" },
{ status: 500 }
)
}
}
35 changes: 0 additions & 35 deletions app/data/user.ts

This file was deleted.

34 changes: 15 additions & 19 deletions app/historique/page.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import History from "@/components/History";
import { verifySession } from "../lib/identity/session-local";
import { UserView } from "../lib/identity/definition";
import History from "@/components/History"
import { verifySession } from "../../lib/identity/session-local"
import { UserView } from "../../lib/identity/definition"

async function PageHistory (){
const session = await verifySession()
async function PageHistory() {
const session = await verifySession()

const user : UserView = {
id: session?.userId as string,
name: session?.name as string,
surname: session?.surname as string,
chatid : session?.chatid as string,
container : session?.container as string
}

return(
<History
user={user}
/>
)
const user: UserView = {
id: session?.userId as string,
name: session?.name as string,
surname: session?.surname as string,
chatid: session?.chatid as string,
container: session?.container as string,
}

return <History user={user} />
}

export default PageHistory
export default PageHistory
2 changes: 1 addition & 1 deletion app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Metadata } from "next"
import { Inter as FontSans } from "next/font/google"
import "./globals.css"
import { cn } from "@/app/lib/utils"
import { cn } from "@/lib/utils"
import { ThemeProvider } from "../components/theme-provider"
import Header from "@/components/layout/header"
import { Toaster } from "@/components/ui/sonner"
Expand Down
4 changes: 2 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Board from "@/components/Board"
import { verifySession } from "./lib/identity/session-local"
import { UserView } from "./lib/identity/definition"
import { verifySession } from "../lib/identity/session-local"
import { UserView } from "../lib/identity/definition"
import { redirect } from "next/navigation"

async function BoardPage() {
Expand Down
30 changes: 12 additions & 18 deletions app/parameter/telegram/page.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
"use server"

import { UserView } from "@/app/lib/identity/definition"
import { verifySession } from "@/app/lib/identity/session-local"
import { UserView } from "@/lib/identity/definition"
import { verifySession } from "@/lib/identity/session-local"
import ParamTelegram from "@/components/ParamTelegram"

export default async function Page() {
const session = await verifySession()

const session = await verifySession()

const user: UserView = {
id: session?.userId as string,
name: session?.name as string,
surname: session?.surname as string,
chatid: session.chatid && session?.chatid as string,
container: session?.container as string
}
const user: UserView = {
id: session?.userId as string,
name: session?.name as string,
surname: session?.surname as string,
chatid: session.chatid && (session?.chatid as string),
container: session?.container as string,
}


return (
<ParamTelegram
user={user}
/>
)
}
return <ParamTelegram user={user} />
}
4 changes: 2 additions & 2 deletions app/video-inference/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UserView } from "../lib/identity/definition"
import { verifySession } from "../lib/identity/session-local"
import { UserView } from "../../lib/identity/definition"
import { verifySession } from "../../lib/identity/session-local"
import VideoInference from "@/components/VideoInference"

export default async function VideoInferencePage() {
Expand Down
2 changes: 1 addition & 1 deletion components/Board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "@tensorflow-models/coco-ssd"
import * as tf from "@tensorflow/tfjs"
import Webcam from "react-webcam"
import { Detected, sendPicture } from "@/app/lib/send-detection/action"
import { Detected, sendPicture } from "@/lib/send-detection/action"
import { Switch } from "@/components/ui/switch"
import { Skeleton } from "@/components/ui/skeleton"
import { Badge } from "@/components/ui/badge"
Expand Down
6 changes: 3 additions & 3 deletions components/History.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import { useToast } from "./ui/use-toast"
import { DateRange } from "react-day-picker"
import { addDays, format } from "date-fns"
import { ObjectDetection, load } from "@tensorflow-models/coco-ssd"
import { deletePictures, getPictures } from "@/app/lib/send-detection/action"
import { deletePictures, getPictures } from "@/lib/send-detection/action"
import { Avatar, AvatarFallback, AvatarImage } from "./ui/avatar"
import { Badge } from "./ui/badge"
import { cn } from "@/app/lib/utils"
import { cn } from "@/lib/utils"
import { Button } from "./ui/button"
import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover"
import { CalendarIcon, LucideTrash2 } from "lucide-react"
import { Calendar } from "./ui/calendar"
import Image from "next/image"
import { UserView } from "@/app/lib/identity/definition"
import { UserView } from "@/lib/identity/definition"
import { CardBody, CardContainer, CardItem } from "./ui/3d-card"
import { BackgroundGradient } from "./ui/background-gradient"

Expand Down
4 changes: 2 additions & 2 deletions components/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Link from "next/link"
import { Input } from "./ui/input"
import { Button } from "./ui/button"
import { z } from "zod"
import { LoginFormSchema } from "@/app/lib/identity/definition"
import { LoginFormSchema } from "@/lib/identity/definition"
import { zodResolver } from "@hookform/resolvers/zod"
import { useState } from "react"
import { useRouter } from "next/navigation"
Expand All @@ -17,7 +17,7 @@ import {
FormLabel,
FormMessage,
} from "./ui/form"
import { login } from "@/app/lib/identity/auth"
import { login } from "@/lib/identity/auth"
import { AuroraBackground } from "./ui/aurora-background"
import { motion } from "framer-motion"
import { useTheme } from "next-themes"
Expand Down
21 changes: 10 additions & 11 deletions components/LogoutButton.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"use client"

import { logout } from "@/app/lib/identity/auth"
import { logout } from "@/lib/identity/auth"
import { Button } from "./ui/button"

export default function LogoutButton() {
return (
<Button
onClick={async() => {
await logout()
}}
>
Se déconnecter
</Button>
)
}
return (
<Button
onClick={async () => {
await logout()
}}>
Se déconnecter
</Button>
)
}
8 changes: 4 additions & 4 deletions components/ParamTelegram.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use client"

import { UserView } from "@/app/lib/identity/definition"
import { UserView } from "@/lib/identity/definition"
import Link from "next/link"
import { useState } from "react"
import { v4 as uuidv4 } from "uuid"
import { Button } from "./ui/button"
import { getChatId } from "@/app/lib/telegram-bot/action"
import { addChatIdToUser } from "@/app/lib/azure-table/action"
import { logout } from "@/app/lib/identity/auth"
import { getChatId } from "@/lib/telegram-bot/action"
import { addChatIdToUser } from "@/lib/azure-table/action"
import { logout } from "@/lib/identity/auth"
import { useRouter } from "next/navigation"

interface ParamTelegramProps {
Expand Down
Loading

0 comments on commit f5ec3a2

Please sign in to comment.