sojorn/sojorn_app/lib/services/media_sanitizer.dart
Patrick Britton 5b5e89e383 feat: Add overlay_json field for Quip text/sticker decorations
- Go: Add overlay_json to Post model, CreatePost handler, and all post queries
- Flutter: Rename QuipTextOverlay → QuipOverlayItem with type enum (text/sticker)
- QuipOverlayItem: add id field, content replaces text, backward-compat alias
- quip_video_item: parse overlay_json and render non-interactive overlays in feed
- quip_upload_provider: accept overlayJson param and pass to publishPost
- api_service: add overlayJson param to publish
2026-02-17 20:06:53 -06:00

82 lines
2.5 KiB
Dart

import 'dart:io';
import 'package:flutter/services.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:image/image.dart' as img;
import 'media/ffmpeg.dart';
class MediaSanitizer {
static Future<File> sanitizeImage(File rawFile) async {
final tempDir = Directory.systemTemp;
final targetPath =
'${tempDir.path}${Platform.pathSeparator}sanitized_${DateTime.now().microsecondsSinceEpoch}.jpg';
try {
final result = await FlutterImageCompress.compressAndGetFile(
rawFile.absolute.path,
targetPath,
quality: 88,
format: CompressFormat.jpeg,
keepExif: false,
autoCorrectionAngle: true,
);
if (result != null) {
return File(result.path);
}
} on MissingPluginException {
// Fall through to pure Dart fallback when native plugin isn't available.
}
final bytes = await rawFile.readAsBytes();
final decoded = img.decodeImage(bytes);
if (decoded == null) {
throw Exception('Failed to sanitize image');
}
final sanitized = img.encodeJpg(decoded, quality: 88);
final output = File(targetPath);
await output.writeAsBytes(sanitized, flush: true);
return output;
}
static Future<File> sanitizeVideo(File rawFile) async {
if (!await rawFile.exists()) {
throw Exception('Video file does not exist');
}
final fileSize = await rawFile.length();
const maxSize = 50 * 1024 * 1024; // 50MB limit for videos
if (fileSize > maxSize) {
throw Exception('Video size exceeds 50MB limit');
}
final fileName = rawFile.path.split('/').last.toLowerCase();
final extension = fileName.split('.').last;
const validExtensions = {'mp4', 'mov', 'webm'};
if (!validExtensions.contains(extension)) {
throw Exception('Unsupported video format: $extension');
}
// Strip all metadata (GPS, device info, timestamps) via FFmpeg remux — no re-encode.
try {
final tempDir = Directory.systemTemp;
final output = File(
'${tempDir.path}${Platform.pathSeparator}stripped_${DateTime.now().microsecondsSinceEpoch}.mp4',
);
final session = await FFmpegKit.execute(
'-y -i "${rawFile.path}" -map_metadata -1 -c copy "${output.path}"',
);
final rc = await session.getReturnCode();
if (ReturnCode.isSuccess(rc) && await output.exists()) {
return output;
}
} catch (_) {
// FFmpeg unavailable — fall through and return original
}
return rawFile;
}
}