Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

fix: check only if permission granted before chunking

Prateek Sunalff3864a09a64
diff --git a/mobile/apps/photos/lib/services/machine_learning/compute_controller.dart b/mobile/apps/photos/lib/services/machine_learning/compute_controller.dart
index 14700efef8..9536bf9636 100644
--- a/mobile/apps/photos/lib/services/machine_learning/compute_controller.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/compute_controller.dart
@@ -10,7 +10,7 @@ import "package:photos/core/event_bus.dart";
import "package:photos/events/compute_control_event.dart";
import "package:thermal/thermal.dart";
-enum _ComputeRunState {
+enum ComputeRunState {
idle,
runningML,
generatingStream,
@@ -35,7 +35,7 @@ class ComputeController {
bool interactionOverride = false;
late Timer _userInteractionTimer;
- _ComputeRunState _currentRunState = _ComputeRunState.idle;
+ ComputeRunState _currentRunState = ComputeRunState.idle;
bool _waitingToRunML = false;
bool get isDeviceHealthy => _isDeviceHealthy;
@@ -70,8 +70,12 @@ class ComputeController {
_logger.info('init done ');
}
- bool requestCompute({bool ml = false, bool stream = false, bool bypassInteractionCheck = false}) {
- _logger.info("Requesting compute: ml: $ml, stream: $stream, bypassInteraction: $bypassInteractionCheck");
+ bool requestCompute(
+ {bool ml = false,
+ bool stream = false,
+ bool bypassInteractionCheck = false}) {
+ _logger.info(
+ "Requesting compute: ml: $ml, stream: $stream, bypassInteraction: $bypassInteractionCheck");
if (!_isDeviceHealthy) {
_logger.info("Device not healthy, denying request.");
return false;
@@ -91,13 +95,17 @@ class ComputeController {
return result;
}
+ ComputeRunState get computeState {
+ return _currentRunState;
+ }
+
bool _requestML() {
- if (_currentRunState == _ComputeRunState.idle) {
- _currentRunState = _ComputeRunState.runningML;
+ if (_currentRunState == ComputeRunState.idle) {
+ _currentRunState = ComputeRunState.runningML;
_waitingToRunML = false;
_logger.info("ML request granted");
return true;
- } else if (_currentRunState == _ComputeRunState.runningML) {
+ } else if (_currentRunState == ComputeRunState.runningML) {
return true;
}
_logger.info(
@@ -108,9 +116,9 @@ class ComputeController {
}
bool _requestStream() {
- if (_currentRunState == _ComputeRunState.idle && !_waitingToRunML) {
+ if (_currentRunState == ComputeRunState.idle && !_waitingToRunML) {
_logger.info("Stream request granted");
- _currentRunState = _ComputeRunState.generatingStream;
+ _currentRunState = ComputeRunState.generatingStream;
return true;
}
_logger.info(
@@ -125,13 +133,13 @@ class ComputeController {
);
if (ml) {
- if (_currentRunState == _ComputeRunState.runningML) {
- _currentRunState = _ComputeRunState.idle;
+ if (_currentRunState == ComputeRunState.runningML) {
+ _currentRunState = ComputeRunState.idle;
}
_waitingToRunML = false;
} else if (stream) {
- if (_currentRunState == _ComputeRunState.generatingStream) {
- _currentRunState = _ComputeRunState.idle;
+ if (_currentRunState == ComputeRunState.generatingStream) {
+ _currentRunState = ComputeRunState.idle;
}
}
}
diff --git a/mobile/apps/photos/lib/services/video_preview_service.dart b/mobile/apps/photos/lib/services/video_preview_service.dart
index 74cd868473..f4c229f594 100644
--- a/mobile/apps/photos/lib/services/video_preview_service.dart
+++ b/mobile/apps/photos/lib/services/video_preview_service.dart
@@ -32,6 +32,7 @@ import "package:photos/service_locator.dart";
import "package:photos/services/file_magic_service.dart";
import "package:photos/services/filedata/model/file_data.dart";
import "package:photos/services/isolated_ffmpeg_service.dart";
+import "package:photos/services/machine_learning/compute_controller.dart";
import "package:photos/ui/notification/toast.dart";
import "package:photos/utils/exif_util.dart";
import "package:photos/utils/file_key.dart";
@@ -51,14 +52,14 @@ class VideoPreviewService {
bool get _hasQueuedFile => fileQueue.isNotEmpty;
VideoPreviewService._privateConstructor()
- : serviceLocator = ServiceLocator.instance,
- filesDB = FilesDB.instance,
- uploadLocksDB = UploadLocksDB.instance,
- ffmpegService = IsolatedFfmpegService.instance,
- fileMagicService = FileMagicService.instance,
- cacheManager = DefaultCacheManager(),
- videoCacheManager = VideoCacheManager.instance,
- config = Configuration.instance;
+ : serviceLocator = ServiceLocator.instance,
+ filesDB = FilesDB.instance,
+ uploadLocksDB = UploadLocksDB.instance,
+ ffmpegService = IsolatedFfmpegService.instance,
+ fileMagicService = FileMagicService.instance,
+ cacheManager = DefaultCacheManager(),
+ videoCacheManager = VideoCacheManager.instance,
+ config = Configuration.instance;
VideoPreviewService(
this.config,
@@ -120,8 +121,9 @@ class VideoPreviewService {
if (file.uploadedFileID == null) return false;
// Check if already in queue
- final bool alreadyInQueue =
- await uploadLocksDB.isInStreamQueue(file.uploadedFileID!);
+ final bool alreadyInQueue = await uploadLocksDB.isInStreamQueue(
+ file.uploadedFileID!,
+ );
if (alreadyInQueue) {
return false; // Indicates file was already in queue
}
@@ -221,8 +223,9 @@ class VideoPreviewService {
// If total is empty then mark all as processed else compute the ratio
// of processed files and total remote video files
// netProcessedItems = processed / total
- final double netProcessedItems =
- total.isEmpty ? 1 : (processed.length / total.length).clamp(0, 1);
+ final double netProcessedItems = total.isEmpty
+ ? 1
+ : (processed.length / total.length).clamp(0, 1);
// Store the data and return it
final status = netProcessedItems;
@@ -254,7 +257,7 @@ class VideoPreviewService {
bool forceUpload = false,
bool isManual = false,
]) async {
- final canStream = isManual ? _allowManualStream() : _allowStream();
+ final canStream = _isPermissionGranted();
if (!canStream) {
_logger.info(
"Pause preview due to disabledSteaming($isVideoStreamingEnabled) or computeController permission) - isManual: $isManual",
@@ -328,8 +331,9 @@ class VideoPreviewService {
_items[enteFile.uploadedFileID!] = PreviewItem(
status: PreviewItemStatus.compressing,
file: enteFile,
- retryCount:
- forceUpload ? 0 : _items[enteFile.uploadedFileID!]?.retryCount ?? 0,
+ retryCount: forceUpload
+ ? 0
+ : _items[enteFile.uploadedFileID!]?.retryCount ?? 0,
collectionID: enteFile.collectionID ?? 0,
);
_fireVideoPreviewStateChange(
@@ -359,9 +363,11 @@ class VideoPreviewService {
? (fileSize * 8) / props!.duration!.inSeconds
: null;
- final colorTransfer =
- videoData["color_transfer"]?.toString().toLowerCase();
- final isHDR = colorTransfer != null &&
+ final colorTransfer = videoData["color_transfer"]
+ ?.toString()
+ .toLowerCase();
+ final isHDR =
+ colorTransfer != null &&
(colorTransfer == "smpte2084" || colorTransfer == "arib-std-b67");
// create temp file & directory for preview generation
@@ -433,17 +439,17 @@ class VideoPreviewService {
final playlistGenResult = await ffmpegService
.runFfmpeg(
- // input file path
- '-i "${file.path}" ' +
- // main params for streaming
- command +
- // output file path
- '$prefix/output.m3u8',
- )
+ // input file path
+ '-i "${file.path}" ' +
+ // main params for streaming
+ command +
+ // output file path
+ '$prefix/output.m3u8',
+ )
.onError((error, stackTrace) {
- _logger.warning("FFmpeg command failed", error, stackTrace);
- return {};
- });
+ _logger.warning("FFmpeg command failed", error, stackTrace);
+ return {};
+ });
final playlistGenReturnCode = playlistGenResult["returnCode"] as int?;
@@ -475,16 +481,16 @@ class VideoPreviewService {
// Fetch resolution of generated stream by decrypting a single frame
final playlistFrameResult = await ffmpegService
.runFfmpeg(
- '-allowed_extensions ALL -i "$prefix/output.m3u8" -frames:v 1 -c copy "$prefix/frame.ts"',
- )
+ '-allowed_extensions ALL -i "$prefix/output.m3u8" -frames:v 1 -c copy "$prefix/frame.ts"',
+ )
.onError((error, stackTrace) {
- _logger.warning(
- "FFmpeg command failed for frame",
- error,
- stackTrace,
- );
- return {};
- });
+ _logger.warning(
+ "FFmpeg command failed for frame",
+ error,
+ stackTrace,
+ );
+ return {};
+ });
final playlistFrameReturnCode =
playlistFrameResult["returnCode"] as int?;
int? width, height;
@@ -577,8 +583,9 @@ class VideoPreviewService {
Future<void> _removeFromLocks(EnteFile enteFile) async {
final bool isFailurePresent =
_failureFiles?.contains(enteFile.uploadedFileID!) ?? false;
- final bool isInManualQueue =
- await uploadLocksDB.isInStreamQueue(enteFile.uploadedFileID!);
+ final bool isInManualQueue = await uploadLocksDB.isInStreamQueue(
+ enteFile.uploadedFileID!,
+ );
if (isFailurePresent) {
await uploadLocksDB.deleteStreamUploadErrorEntry(
@@ -656,16 +663,13 @@ class VideoPreviewService {
try {
final encryptionKey = getFileKey(file);
final playlistContent = playlist.readAsStringSync();
- final result = await gzipAndEncryptJson(
- {
- "playlist": playlistContent,
- 'type': 'hls_video',
- 'width': width,
- 'height': height,
- 'size': objectSize,
- },
- encryptionKey,
- );
+ final result = await gzipAndEncryptJson({
+ "playlist": playlistContent,
+ 'type': 'hls_video',
+ 'width': width,
+ 'height': height,
+ 'size': objectSize,
+ }, encryptionKey);
final _ = await serviceLocator.enteDio.put(
"/files/video-data",
data: {
@@ -784,8 +788,7 @@ class VideoPreviewService {
}
final videoFile = (await videoCacheManager.getFileFromCache(
_getVideoPreviewKey(objectID),
- ))
- ?.file;
+ ))?.file;
if (videoFile == null) {
previewURLResult = previewURLResult ?? await _getPreviewUrl(file);
if (size != null && size < _maxPreviewSizeLimitForCache) {
@@ -886,8 +889,9 @@ class VideoPreviewService {
"${config.getHttpEndpoint()}/public-collection/files/data/preview",
queryParameters: {
"fileID": file.uploadedFileID,
- "type":
- file.fileType == FileType.video ? "vid_preview" : "img_preview",
+ "type": file.fileType == FileType.video
+ ? "vid_preview"
+ : "img_preview",
},
options: Options(
headers: collectionsService.publicCollectionHeaders(
@@ -901,8 +905,9 @@ class VideoPreviewService {
"/files/data/preview",
queryParameters: {
"fileID": file.uploadedFileID,
- "type":
- file.fileType == FileType.video ? "vid_preview" : "img_preview",
+ "type": file.fileType == FileType.video
+ ? "vid_preview"
+ : "img_preview",
},
);
url = (response.data["url"] as String);
@@ -1027,12 +1032,14 @@ class VideoPreviewService {
}
// First try to find the file in the 60-day list
- var queueFile =
- files.firstWhereOrNull((f) => f.uploadedFileID == queueFileId);
+ var queueFile = files.firstWhereOrNull(
+ (f) => f.uploadedFileID == queueFileId,
+ );
// If not found in 60-day list, fetch it individually
- queueFile ??=
- await filesDB.getAnyUploadedFile(queueFileId).catchError((e) => null);
+ queueFile ??= await filesDB
+ .getAnyUploadedFile(queueFileId)
+ .catchError((e) => null);
if (queueFile == null) {
await uploadLocksDB
@@ -1128,10 +1135,21 @@ class VideoPreviewService {
bool _allowManualStream() {
return isVideoStreamingEnabled &&
- computeController.requestCompute(stream: true, bypassInteractionCheck: true);
+ computeController.requestCompute(
+ stream: true,
+ bypassInteractionCheck: true,
+ );
+ }
+
+ bool _isPermissionGranted() {
+ return isVideoStreamingEnabled &&
+ computeController.computeState == ComputeRunState.generatingStream;
}
- void queueFiles({Duration duration = const Duration(seconds: 5), bool isManual = false}) {
+ void queueFiles({
+ Duration duration = const Duration(seconds: 5),
+ bool isManual = false,
+ }) {
Future.delayed(duration, () async {
if (_hasQueuedFile) return;