Spaces:
Running
Running
Vyacheslav commited on
Commit ·
676ef7e
1
Parent(s): c67c596
optimization
Browse filesFiles changed (2)
- routes/upload.js+15
- utils/file.js+104-1
10 unmodified lines11121314151670 unmodified lines87888990919249 unmodified lines14214314414514614710 unmodified linesconst { authenticateToken } = require('../middleware/authenticateToken');const { uploadLimiter } = require('../constants/limits');const { st, detectLangReq } = require('../constants/translations');const uploadDir = path.join(os.tmpdir(), '4send_uploads');if (!fs.existsSync(uploadDir)) {70 unmodified linesreturn res.status(400).json({ error: st('Недопустимый формат файла', detectLangReq(req)) });}if (sharpExts.includes(ext)) {try {const tempPath = safePath + '_clean.webp';49 unmodified linescontinue;}if (sharpExts.includes(ext)) {try {const tempPath = safePath + '_clean.webp';10 unmodified lines1112131415161770 unmodified lines88899091929394959697989910049 unmodified lines15015115215315415515615715815916016116210 unmodified linesconst { authenticateToken } = require('../middleware/authenticateToken');const { uploadLimiter } = require('../constants/limits');const { st, detectLangReq } = require('../constants/translations');const { verifyUploadedFile } = require('../utils/file');const uploadDir = path.join(os.tmpdir(), '4send_uploads');if (!fs.existsSync(uploadDir)) {70 unmodified linesreturn res.status(400).json({ error: st('Недопустимый формат файла', detectLangReq(req)) });}if (imgExts.includes(ext) || extList.audio.includes(ext) || extList.video.includes(ext)) {if (!(await verifyUploadedFile(safePath, ext))) {if (fs.existsSync(safePath)) await fsPromises.unlink(safePath);return res.status(400).json({ error: st('Некорректный формат изображения', detectLangReq(req)) });}}if (sharpExts.includes(ext)) {try {const tempPath = safePath + '_clean.webp';49 unmodified linescontinue;}if (imgExts.includes(ext) || extList.audio.includes(ext) || extList.video.includes(ext)) {if (!(await verifyUploadedFile(safePath, ext))) {if (fs.existsSync(safePath)) await fsPromises.unlink(safePath);continue;}}if (sharpExts.includes(ext)) {try {const tempPath = safePath + '_clean.webp';
1234567const extractCloudinaryId = (url) => {if (!url) return null;const match = url.match(/\/v\d+\/(4send_cloud\/[^\.]+)/) || url.match(/(4send_cloud\/[^\.]+)/);return match ? match[1] : null;};module.exports = { extractCloudinaryId };123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110const fs = require('fs');const path = require('path');const signatures = {jpeg: { offset: 0, bytes: [0xFF, 0xD8, 0xFF] },jpg: { offset: 0, bytes: [0xFF, 0xD8, 0xFF] },png: { offset: 0, bytes: [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A] },gif: { offset: 0, bytes: [0x47, 0x49, 0x46, 0x38] },webp: { offset: 0, bytes: [0x52, 0x49, 0x46, 0x46] },bmp: { offset: 0, bytes: [0x42, 0x4D] },tiff: null,tif: null,heic: null,heif: null,mp3: null,wav: { offset: 0, bytes: [0x52, 0x49, 0x46, 0x46] },ogg: { offset: 0, bytes: [0x4F, 0x67, 0x67, 0x53] },flac: { offset: 0, bytes: [0x66, 0x4C, 0x61, 0x43] },opus: { offset: 0, bytes: [0x4F, 0x67, 0x67, 0x53] },m4a: null,mp4: null,webm: { offset: 0, bytes: [0x1A, 0x45, 0xDF, 0xA3] },mov: null,avi: { offset: 0, bytes: [0x52, 0x49, 0x46, 0x46] },mkv: { offset: 0, bytes: [0x1A, 0x45, 0xDF, 0xA3] },wmv: { offset: 0, bytes: [0x30, 0x26, 0xB2, 0x75] },flv: { offset: 0, bytes: [0x46, 0x4C, 0x56, 0x01] },m4v: null};const containerExts = ['.wav', '.webp', '.avi', '.mp3'];const ftypExts = ['.mp4', '.m4a', '.m4v', '.mov'];const tiffExts = ['.tiff', '.tif'];const heicExts = ['.heic', '.heif'];const checkFtyp = (buf) => {if (buf.length < 12) return false;const brand = buf.slice(8, 12).toString().toLowerCase();const compatible = buf.slice(16, 20).toString().toLowerCase();const ftypBrands = ['isom', 'mp42', 'm4v ', 'm4a ', '3gp4', 'mov ', 'avc1', 'mp71'];return ftypBrands.includes(brand) || ftypBrands.includes(compatible);};const checkTiff = (buf) => {if (buf.length < 4) return false;return (buf[0] === 0x49 && buf[1] === 0x49 && buf[2] === 0x2A && buf[3] === 0x00) ||(buf[0] === 0x4D && buf[1] === 0x4D && buf[2] === 0x00 && buf[3] === 0x2A);};const checkHeic = (buf) => {if (buf.length < 12) return false;const brand = buf.slice(8, 12).toString().toLowerCase();return ['heic', 'heix', 'heim', 'heis', 'mif1', 'msf1'].includes(brand);};const checkMp3 = (buf) => {if (buf.length < 3) return false;if (buf[0] === 0x49 && buf[1] === 0x44 && buf[2] === 0x33) return true;if (buf[0] === 0xFF && (buf[1] & 0xE0) === 0xE0) return true;return false;};const checkContainer = (buf, expected) => {if (buf.length < 12) return false;const fourCc = buf.slice(8, 12).toString();return fourCc === expected;};const verifyUploadedFile = async (filePath, ext) => {try {const fd = await fs.promises.open(filePath, 'r');const buf = Buffer.alloc(32);const { bytesRead } = await fd.read(buf, 0, 32, 0);await fd.close();if (bytesRead < 4) return false;const sigExt = ext.replace('.', '');if (containerExts.includes(ext)) {if (ext === '.wav') return checkContainer(buf, 'WAVE');if (ext === '.webp') return checkContainer(buf, 'WEBP');if (ext === '.avi') return checkContainer(buf, 'AVI ');if (ext === '.mp3') return checkMp3(buf);}if (ftypExts.includes(ext)) return checkFtyp(buf);if (tiffExts.includes(ext)) return checkTiff(buf);if (heicExts.includes(ext)) return checkHeic(buf);const sig = signatures[sigExt];if (!sig) return true;for (let i = 0; i < sig.bytes.length; i++) {if (buf[sig.offset + i] !== sig.bytes[i]) return false;}return true;} catch {return false;}};const extractCloudinaryId = (url) => {if (!url) return null;const match = url.match(/\/v\d+\/(4send_cloud\/[^\.]+)/) || url.match(/(4send_cloud\/[^\.]+)/);return match ? match[1] : null;};module.exports = { verifyUploadedFile, extractCloudinaryId };
routes/upload.js+15
utils/file.js+104-1