Reduce media bandwidth: compress uploads, add long-lived cache headers, gzip API
- Resize/re-encode every uploaded image server-side (posts, avatars, stories, shop products/logo, chat messages) via a shared sharp-based optimizer — caps dimensions to what's actually displayed and re-compresses in place, cutting a typical raw camera photo by ~90% with no visible quality loss - Cap uploaded video resolution/bitrate (re-encode only when above 1280px/ ~2.5Mbps) alongside the existing faststart step; apply faststart+cap to story and chat videos too (previously stored completely raw) - Add long-lived Cache-Control to all /storage static routes so clients stop re-downloading unchanged media on every request - Add gzip/brotli compression for JSON API responses (feed/explore/chat) Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,13 @@ const ChatRoomModel = require('../../../models/ChatRoomModel')
|
||||
const ChatRoomMessageModel = require('../../../models/ChatRoomMessageModel')
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const { ProvinceModel, CityModel } = require('../../../models/StateCity')
|
||||
const { optimizeImageFile } = require('../../../utils/imageOptimize')
|
||||
let optimizeVideoFaststart = async () => {}
|
||||
try {
|
||||
({ optimizeVideoFaststart } = require('../../../utils/postVideoProcessing'))
|
||||
} catch (error) {
|
||||
console.warn('[chatRoomController] postVideoProcessing unavailable:', error.message)
|
||||
}
|
||||
const {
|
||||
slugifyTitle,
|
||||
userCanSeeRoom,
|
||||
@@ -227,6 +234,7 @@ const createRoom = async (req, res, next) => {
|
||||
const uniqueFileName = `${Date.now()}-${crypto.randomUUID()}${path.extname(imageFile.name || '')}`
|
||||
const filePath = path.join(uploadDir, uniqueFileName)
|
||||
await fs.move(imageFile.path, filePath)
|
||||
await optimizeImageFile(filePath, { maxDimension: 800 })
|
||||
imagePath = `/chat-rooms/${uniqueFileName}`
|
||||
}
|
||||
|
||||
@@ -397,6 +405,13 @@ const sendRoomMessage = async (req, res, next) => {
|
||||
const uniqueFileName = `${Date.now()}-${crypto.randomUUID()}${path.extname(uploaded.name || '')}`
|
||||
const filePath = path.join(uploadDir, uniqueFileName)
|
||||
await fs.move(uploaded.path, filePath)
|
||||
if (resolvedFileType === 'image' || (uploaded.type || '').startsWith('image/')) {
|
||||
await optimizeImageFile(filePath)
|
||||
} else if (resolvedFileType === 'video' || (uploaded.type || '').startsWith('video/')) {
|
||||
optimizeVideoFaststart(filePath).catch((err) => {
|
||||
console.error('[chatRoomController] video optimize background error:', err.message)
|
||||
})
|
||||
}
|
||||
fileUrl = `/chat-rooms/messages/${uniqueFileName}`
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user