Spaces:
Paused
Paused
Download services/imageWaypointService.js from Adhyasta/gcs-backend-server: direct link, hf CLI and curl.
- Browser
- Download file 6.05 kB
-
https://huggingface.co/spaces/Adhyasta/gcs-backend-server/resolve/main/services/imageWaypointService.js
- Command line
-
hf download hf://spaces/Adhyasta/gcs-backend-server/services/imageWaypointService.js
-
curl -L -o imageWaypointService.js https://huggingface.co/spaces/Adhyasta/gcs-backend-server/resolve/main/services/imageWaypointService.js
6.05 kB
| const express = require('express') | |
| const multer = require('multer') | |
| const path = require('path') | |
| const fs = require('fs') | |
| const { insertFireMissionMetadata, getFireMissionMetadata, deleteFireMissionMetadata } = require('./models/fireMissionModel') | |
| const router = express.Router() | |
| const IMAGES_WAYPOINTS_DIR = path.join(__dirname, '..', 'image_wp_result') | |
| if (!fs.existsSync(IMAGES_WAYPOINTS_DIR)) { | |
| fs.mkdirSync(IMAGES_WAYPOINTS_DIR, { recursive: true }) | |
| } | |
| const imageWaypointStorage = multer.diskStorage({ | |
| destination: function (req, file, cb) { | |
| cb(null, IMAGES_WAYPOINTS_DIR) | |
| }, | |
| filename: function (req, file, cb) { | |
| const timestamp = Date.now() | |
| const ext = path.extname(file.originalname) || '.jpg' | |
| const filename = `${timestamp}${ext}` | |
| cb(null, filename) | |
| } | |
| }) | |
| const imageWaypointUpload = multer({ | |
| storage: imageWaypointStorage, | |
| limits: { | |
| fileSize: 50 * 1024 * 1024 | |
| }, | |
| fileFilter: function (req, file, cb) { | |
| const allowedTypes = /jpeg|jpg|png/ | |
| const mimetype = allowedTypes.test(file.mimetype) | |
| const extname = allowedTypes.test( | |
| path.extname(file.originalname).toLowerCase() | |
| ) | |
| if (mimetype && extname) { | |
| return cb(null, true) | |
| } | |
| cb(new Error('Only image files like jpeg, jpg, or png that allowed')) | |
| } | |
| }) | |
| router.post('/', imageWaypointUpload.single('file'), async (req, res) => { | |
| try { | |
| if (!req.file) { | |
| return res.status(400).json({ | |
| success: false, | |
| error: 'No file given' | |
| }) | |
| } | |
| let metadata | |
| try { | |
| metadata = JSON.parse(req.body.meta || '{}') | |
| } catch (parseError) { | |
| fs.unlinkSync(req.file.path) | |
| return res.status(400).json({ | |
| success: false, | |
| error: 'Invalid metadata format. Expected JSON' | |
| }) | |
| } | |
| const { lat, lon, timestamp } = metadata | |
| if (!lat || !lon) { | |
| fs.unlinkSync(req.file.path) | |
| return res.status(400).json({ | |
| success: false, | |
| error: 'Invalid coordinates. Both lat and lon must be a valid number' | |
| }) | |
| } | |
| const latitude = parseFloat(lat) | |
| const longitude = parseFloat(lon) | |
| if (latitude < -90 || latitude > 90) { | |
| fs.unlinkSync(req.file.path) | |
| return res.status(400).json({ | |
| success: false, | |
| error: `Latitude must be between -90 (Antartica) and 90 (Kutub Utara), your latitude: ${latitude}` | |
| }) | |
| } | |
| if (longitude < -180 || longitude > 180) { | |
| fs.unlinkSync(req.file.path) | |
| return res.status(400).json({ | |
| success: false, | |
| error: `Longitude must be between -180 and 180, your longitude: ${longitude}` | |
| }) | |
| } | |
| const fireId = await insertFireMissionMetadata({ | |
| filename: req.file.filename, | |
| latitude, | |
| longitude, | |
| timestamp: timestamp || Date.now(), | |
| fileSize: req.file.size | |
| }) | |
| const imageData = { | |
| fireId, | |
| filename: req.file.filename, | |
| lat: latitude, | |
| lon: longitude, | |
| timestamp: timestamp || Date.now() | |
| } | |
| const { getSocketIOInstance } = require('../lib/socketio_instance') | |
| const io = getSocketIOInstance() | |
| if (io) { | |
| io.emit('imagewp:new_image', imageData) | |
| } else { | |
| console.log('SocketIO.instance error') | |
| } | |
| res.json({ | |
| success: true, | |
| message: 'Image upload succesfully', | |
| data: imageData | |
| }) | |
| } catch (error) { | |
| console.log('Error uploading image and waypoint') | |
| if (req.file && fs.existsSync(req.file.path)) { | |
| fs.unlinkSync(req.file.path) | |
| } | |
| res.status(500).json({ | |
| success: false, | |
| error: error.message || 'Internal server error' | |
| }) | |
| } | |
| }) | |
| router.get('/list', (req, res) => { | |
| try { | |
| if (!fs.existsSync(IMAGES_WAYPOINTS_DIR)) { | |
| return res.json({ | |
| success: true, | |
| count: 0, | |
| images: [] | |
| }) | |
| } | |
| const files = fs.readdirSync(IMAGES_WAYPOINTS_DIR) | |
| const imageFiles = files.filter((file) => { | |
| return /\.(jpg|jpeg|png)$/i.test(file) | |
| }) | |
| const images = imageFiles.map((filename) => { | |
| const filePath = path.join(IMAGES_WAYPOINTS_DIR, filename) | |
| const stats = fs.statSync(filePath) | |
| const timestampMatch = filename.match(/^(\d+)/) | |
| const timestamp = timestampMatch | |
| ? parseInt(timestampMatch[1]) | |
| : stats.mtimeMs | |
| return { | |
| filename, | |
| timestamp, | |
| lat: null, | |
| lon: null, | |
| size: stats.size, | |
| uploadedAt: stats.mtime.toISOString(), | |
| url: `/api/imagewp/files/${filename}` | |
| } | |
| }) | |
| images.sort((a, b) => b.timestamp - a.timestamp) | |
| const limit = parseInt(req.query.limit) || 100 | |
| const limitedImages = images.slice(0, limit) | |
| res.json({ | |
| success: true, | |
| count: limitedImages.length, | |
| total: images.length, | |
| images: limitedImages | |
| }) | |
| } catch (error) { | |
| console.error('Internal server error: ', error) | |
| res.status(500).json({ | |
| success: false, | |
| error: error.message | |
| }) | |
| } | |
| }) | |
| router.get('/metadata', async (req, res) => { | |
| try { | |
| const limit = parseInt(req.query.limit) || 100 | |
| const metadata = await getFireMissionMetadata(limit) | |
| res.json({ | |
| success: true, | |
| count: metadata.length, | |
| data: metadata | |
| }) | |
| } catch (error) { | |
| console.error('Error fetching metadata:', error) | |
| res.status(500).json({ | |
| success: false, | |
| error: error.message | |
| }) | |
| } | |
| }) | |
| router.delete('/:filename', async (req, res) => { | |
| try { | |
| const { filename } = req.params | |
| const filePath = path.join(IMAGES_WAYPOINTS_DIR, filename) | |
| if (!fs.existsSync(filePath)) { | |
| return res.status(404).json({ | |
| success: false, | |
| error: 'File not found' | |
| }) | |
| } | |
| fs.unlinkSync(filePath) | |
| await deleteFireMissionMetadata(filename) | |
| res.json({ | |
| success: true, | |
| message: 'Image and metadata deleted successfully', | |
| filename | |
| }) | |
| } catch (error) { | |
| console.error('Error deleting image:', error) | |
| res.status(500).json({ | |
| success: false, | |
| error: error.message | |
| }) | |
| } | |
| }) | |
| module.exports = router | |