diff --git a/backend/.env.example b/backend/.env.example deleted file mode 100644 index 0e6ad2d..0000000 --- a/backend/.env.example +++ /dev/null @@ -1,13 +0,0 @@ -# Server config -PORT=5000 - -CLERK_SECRET_KEY=sk_test_your_key_here - -# Database config -# Use LOCAL for local MongoDB, CLOUD for MongoDB Atlas -DB=LOCAL or CLOUD -MONGODB_URI=mongodb://localhost:27017/snapmap -MONGODB_CLOUD_URI=mongodb+srv://username:password@cluster.mongodb.net/snapmap - -AZURE_STORAGE_CONNECTION=DefaultEndpointsProtocol=https;AccountName=your_storage_account;AccountKey=your_account_key;EndpointSuffix=core.windows.net -CONTAINER_NAME= \ No newline at end of file diff --git a/backend/controllers/photoController.js b/backend/controllers/photoController.js index 517f39f..2907fca 100644 --- a/backend/controllers/photoController.js +++ b/backend/controllers/photoController.js @@ -156,3 +156,65 @@ export const testUploadPhoto = async (req, res) => { }); } }; + + +// multiple uploads added here// + +export const uploadPhotos = async(req,res)=>{ + try{ + const{lat,lon} = req.body || {}; + const latitude = parseFloat(lat); + const longitude = parseFloat(lon); + + if(!req.userId){ + return res.status(401).json({ message: "Unauthorized"}); + } + if (!req.files || req.files.length === 0) { + return res.status(400).json({ message: "No photos provided" }); + } + + if (!Number.isFinite(latitude) || !Number.isFinite(longitude)) { + return res.status(400).json({ message: "Invalid or missing lat/lon" }); + } + const user = await User.findOne({ clerkUserId: req.userId }); + if (!user) { + return res.status(404).json({ message: "User not registered" }); + } + + const uploadedPhotos = []; + + for (const file of req.files) { + const fileName = buildFileName(file.originalname, req.userId); + const imageUrl = await uploadToAzure(file.buffer, fileName); + + const photo = await Photo.create({ + userId: user._id, + clerkUserId: req.userId, + imageUrl, + location: { + type: "Point", + coordinates: [longitude, latitude], + }, + timestamp: new Date(), + eventId: null, + }); + + uploadedPhotos.push({ + photoId: photo._id, + imageUrl, + }); + } + + return res.status(201).json({ + status: "success", + count: uploadedPhotos.length, + photos: uploadedPhotos, + }); + } catch (error) { + console.error("Upload multiple photos error:", error); + return res.status(500).json({ + message: "Internal server error: " + error.message, + }); + + } +}; diff --git a/backend/routes/photo.js b/backend/routes/photo.js index 0f20d8f..f454b0d 100644 --- a/backend/routes/photo.js +++ b/backend/routes/photo.js @@ -1,9 +1,14 @@ import express from "express"; import multer from "multer"; import authMiddleware from "../middleware/authentication.js"; -import { uploadPhoto, getAllPhotos, testUploadPhoto } from "../controllers/photoController.js"; +import { uploadPhoto, uploadPhotos, getAllPhotos, testUploadPhoto } from "../controllers/photoController.js"; + const router = express.Router(); +router.get("/ping", (req, res) => { + res.json({ pong: true }); +}); + const upload = multer({ storage: multer.memoryStorage(), @@ -16,8 +21,29 @@ const upload = multer({ }, }); -router.post("/upload-photo", authMiddleware, upload.single("photo"), uploadPhoto); -router.post("/test-upload", upload.single("photo"), testUploadPhoto); // Test route without auth +router.post( + "/upload-photo", + authMiddleware, + upload.single("photo"), + uploadPhoto +); + +router.post( + "/upload-photos", + authMiddleware, + upload.array("photos[]", 10), + uploadPhotos +); + +router.post( + "/test-upload", + upload.single("photo"), + testUploadPhoto +); + router.get("/all-photos", getAllPhotos); + + + export default router diff --git a/backend/utils/azure.js b/backend/utils/azure.js index 007d04e..530803c 100644 --- a/backend/utils/azure.js +++ b/backend/utils/azure.js @@ -19,9 +19,9 @@ async function uploadToAzure(buffer, fileName) { try { await blobServiceClient.getAccountInfo(); - console.log("✅ Azure Blob Storage Connected"); + console.log(" Azure Blob Storage Connected"); } catch (err) { - console.error("❌ Azure Blob Connection Failed:", err.message); + console.error(" Azure Blob Connection Failed:", err.message); throw err; } diff --git a/backend/v1.js b/backend/v1.js index e5a9bf5..87699ac 100644 --- a/backend/v1.js +++ b/backend/v1.js @@ -1,7 +1,7 @@ -import express from "express" -import healthRoute from "../backend/routes/health.js"; -import authRoute from "../backend/routes/auth.js"; -import photoRoute from "../backend/routes/photo.js"; +import express from "express"; +import healthRoute from "./routes/health.js"; +import authRoute from "./routes/auth.js"; +import photoRoute from "./routes/photo.js"; const router = express.Router();