mirror of
https://github.com/screentinker/screentinker.git
synced 2026-08-13 22:03:13 -06:00
* fix(content): rotation-aware media dimensions — portrait no longer stored landscape (#170) Ingest recorded CODED width/height and ignored rotation, so a portrait phone video (coded 1920x1080 + 90° Display-Matrix) or a portrait photo (EXIF orientation 6) was stored LANDSCAPE. The player then rendered it wrong-aspect and letterboxed — the "portrait content degraded + blue bar at the bottom" symptom in #170. The reporter's workaround (pre-rotate + mark Landscape) is exactly what this bug forces. - lib/media-orientation.js (new): pure, unit-tested display-dimension helpers = single source of truth for ingest AND the backfill. videoDisplayDims() reads the modern Display-Matrix side_data rotation (falls back to the legacy tags.rotate, sign-normalized); imageDisplayDims() honors EXIF orientation 5..8. Odd quarter-turns swap W/H. - lib/content-ingest.js: use the helpers for stored dims; add sharp .rotate() so image THUMBNAILS are auto-oriented too (video thumbs were already auto-rotated by ffmpeg). - scripts/backfill-rotation-dims.js (new): idempotent, dry-run-by-default maintenance to correct already-uploaded portrait media (re-probe -> fix dims -> regenerate image thumbs). - test/media-orientation.test.js: 5 bites (tag + Display-Matrix, sign/normalize, EXIF 5..8, the blue-bar landscape->portrait case, null-safety). Scopes #170 to its residual-on-1.9.4 issues; the 1.9.3 "never displays" slice was #162 + the remote_url-null download fix, already shipped in 1.9.4. The slow low-res/orientation- cycling first load is tracked separately in #170 pending repro data. Refs #170. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix(android): honor EXIF orientation in ImageLoader so portrait photos render upright (#170) Completes the rotation-aware media fix on the PLAYER side. The server ingest fix (this branch) corrects stored dimensions + auto-orients the thumbnail, but the panel draws the full-res original via BitmapFactory, which ignores EXIF — so a portrait photo (landscape pixels tagged "rotate 90") still rendered sideways on the screen. QA root-cause pass on #170 caught this gap: the Android player reads no stored dims and applied no EXIF. ImageLoader now reads the EXIF orientation (from the file for cached content, from the byte stream for remote_url images — ExifInterface(stream) is API 24+, minSdk is 24) and rotates/ flips the decoded bitmap via a Matrix (all 8 orientations). NORMAL/UNDEFINED is a no-op (no extra allocation); a transformed copy recycles the source; OOM falls back to the source rather than crashing. Videos were already correct (ExoPlayer honors the rotation matrix). Verified: :app:compileDebugKotlin clean. Refs #170. Rides with the server rotation-dims fix on this branch. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
81 lines
3.9 KiB
JavaScript
81 lines
3.9 KiB
JavaScript
'use strict';
|
|
|
|
// #73: shared content-ingest core. Extracted from routes/content.js POST / so the agency
|
|
// upload (routes/agency.js) produces BYTE-IDENTICAL first-class content (same thumbnail/
|
|
// dimensions/duration/insert) - an agency asset is indistinguishable from a dashboard
|
|
// upload. routes/content.js POST / is now a thin caller; behavior is unchanged (its
|
|
// existing tests are the regression guard).
|
|
|
|
const path = require('path');
|
|
const { v4: uuidv4 } = require('uuid');
|
|
const { db } = require('../db/database');
|
|
const config = require('../config');
|
|
const { sanitizeString } = require('../middleware/sanitize');
|
|
const { videoDisplayDims, imageDisplayDims } = require('./media-orientation');
|
|
|
|
// Multer takes file.originalname from the multipart header, bypassing sanitizeBody, so
|
|
// HTML-escape here (renders as text in every UI sink). .normalize('NFC') first: macOS
|
|
// sends NFD-decomposed names; Linux/renderers expect NFC. Single point - every filename
|
|
// storage site flows through here.
|
|
function safeFilename(name) {
|
|
return sanitizeString((name || '').normalize('NFC'));
|
|
}
|
|
|
|
// Process a multer-uploaded file (thumbnail + dimensions + duration) and insert a content
|
|
// row. Returns the content row. Throws on a hard failure (the caller maps to 500);
|
|
// thumbnail/metadata failures are best-effort (logged, non-fatal) exactly as before.
|
|
async function ingestUploadedFile({ file, userId, workspaceId, folderId = null }) {
|
|
const id = uuidv4();
|
|
const filepath = file.filename;
|
|
let width = null, height = null, durationSec = null, thumbnailPath = null;
|
|
|
|
try {
|
|
if (file.mimetype.startsWith('image/')) {
|
|
const sharp = require('sharp');
|
|
const metadata = await sharp(file.path).metadata();
|
|
// #170: honor EXIF orientation so a portrait photo isn't stored as landscape.
|
|
({ width, height } = imageDisplayDims(metadata));
|
|
thumbnailPath = `thumb_${filepath}`;
|
|
await sharp(file.path)
|
|
.rotate() // #170: auto-orient per EXIF (and strip the tag) so the thumbnail matches
|
|
.resize(config.thumbnailWidth)
|
|
.jpeg({ quality: 70 })
|
|
.toFile(path.join(config.contentDir, thumbnailPath));
|
|
} else if (file.mimetype.startsWith('video/')) {
|
|
try {
|
|
const { execFileSync } = require('child_process');
|
|
const probe = execFileSync('ffprobe', ['-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', file.path],
|
|
{ timeout: 15000 }
|
|
).toString();
|
|
const info = JSON.parse(probe);
|
|
if (info.format?.duration) durationSec = parseFloat(info.format.duration);
|
|
const videoStream = info.streams?.find(s => s.codec_type === 'video');
|
|
if (videoStream) {
|
|
// #170: honor the rotation/Display-Matrix so a portrait video isn't stored landscape.
|
|
// (ffmpeg auto-rotates the thumbnail below by default, so only the dims need fixing.)
|
|
({ width, height } = videoDisplayDims(videoStream));
|
|
}
|
|
thumbnailPath = `thumb_${filepath.replace(/\.[^.]+$/, '.jpg')}`;
|
|
try {
|
|
execFileSync('ffmpeg', ['-y', '-i', file.path, '-ss', '2', '-vframes', '1', '-vf', `scale=${config.thumbnailWidth}:-1`, path.join(config.contentDir, thumbnailPath)],
|
|
{ timeout: 15000 }
|
|
);
|
|
} catch { thumbnailPath = null; }
|
|
} catch (e) {
|
|
console.warn('ffprobe failed:', e.message);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.warn('Thumbnail/metadata generation failed:', e.message);
|
|
}
|
|
|
|
db.prepare(`
|
|
INSERT INTO content (id, user_id, workspace_id, filename, filepath, mime_type, file_size, duration_sec, thumbnail_path, width, height, folder_id)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
`).run(id, userId, workspaceId, safeFilename(file.originalname), filepath, file.mimetype, file.size, durationSec, thumbnailPath, width, height, folderId || null);
|
|
|
|
return db.prepare('SELECT * FROM content WHERE id = ?').get(id);
|
|
}
|
|
|
|
module.exports = { ingestUploadedFile, safeFilename };
|