public async void Run(IBackgroundTaskInstance taskInstance) { _deferral = taskInstance.GetDeferral(); // Check for network changes NetworkService.CheckNetworkChange(); // Set the API to use depending on the settings if (SettingsService.LoadSetting(ResourceService.SettingsResources.GetString("SR_UseStagingServer"), false)) { SdkService.MegaSdk.changeApiUrl("https://staging.api.mega.co.nz/"); } else if (SettingsService.LoadSetting(ResourceService.SettingsResources.GetString("SR_UseStagingServerPort444"), false)) { SdkService.MegaSdk.changeApiUrl("https://staging.api.mega.co.nz:444/", true); } else { SdkService.MegaSdk.changeApiUrl("https://g.api.mega.co.nz/"); } // Log message to indicate that the service is invoked LogService.Log(MLogLevel.LOG_LEVEL_INFO, "Service invoked."); // Load the connection upload settings CameraUploadsConnectionType cameraUploadsConnectionType = CameraUploadsConnectionType.EthernetWifiOnly; try { cameraUploadsConnectionType = (CameraUploadsConnectionType)await SettingsService.LoadSettingFromFileAsync <int>("CameraUploadsSettingsHowKey"); } catch (Exception e) { LogService.Log(MLogLevel.LOG_LEVEL_ERROR, "Could not load settings", e); } if (!CheckNetWork(cameraUploadsConnectionType)) { LogService.Log(MLogLevel.LOG_LEVEL_INFO, "Task finished: connection type"); _deferral.Complete(); return; } SdkService.InitializeSdkParams(); var loggedIn = await LoginAsync(); if (loggedIn) { var fetched = await FetchNodesAsync(); if (fetched) { // Add notifications listener var megaGlobalListener = new MegaGlobalListener(); SdkService.MegaSdk.addGlobalListener(megaGlobalListener); // Enable the transfers resumption for the Camera Uploads service await megaGlobalListener.ExecuteAsync(() => SdkService.MegaSdk.enableTransferResumption()); var cameraUploadRootNode = await SdkService.GetCameraUploadRootNodeAsync(); if (cameraUploadRootNode == null) { // No camera upload node found or created // Just finish this run and try again next time LogService.Log(MLogLevel.LOG_LEVEL_ERROR, "No Camera Uploads folder detected/created"); _deferral.Complete(); return; } // Load the file upload settings CameraUploadsFileType cameraUploadsFileType = CameraUploadsFileType.PhotoAndVideo; try { cameraUploadsFileType = (CameraUploadsFileType)await SettingsService.LoadSettingFromFileAsync <int>("CameraUploadsSettingsFileKey"); } catch (Exception e) { LogService.Log(MLogLevel.LOG_LEVEL_ERROR, "Could not load settings", e); } var uploadFolders = new List <StorageFolder>(); switch (cameraUploadsFileType) { case CameraUploadsFileType.PhotoAndVideo: uploadFolders.Add(KnownFolders.PicturesLibrary); uploadFolders.Add(KnownFolders.VideosLibrary); break; case CameraUploadsFileType.PhotoOnly: uploadFolders.Add(KnownFolders.PicturesLibrary); break; case CameraUploadsFileType.VideoOnly: uploadFolders.Add(KnownFolders.VideosLibrary); break; } // Get the IMAGE and/or VIDEO files to upload to MEGA var fileToUpload = await TaskService.GetAvailableUploadAsync( TaskService.ImageDateSetting, uploadFolders.ToArray()); foreach (var storageFile in fileToUpload) { // Skip the current file if it has failed more than the max error count if (await ErrorHandlingService.SkipFileAsync( storageFile.Name, ErrorHandlingService.ImageErrorFileSetting, ErrorHandlingService.ImageErrorCountSetting)) { continue; } if (!CheckNetWork(cameraUploadsConnectionType)) { break; } // Calculate time for fingerprint check and upload ulong mtime = TaskService.CalculateMtime(storageFile.DateCreated.DateTime); try { using (var fs = await storageFile.OpenStreamForReadAsync()) { var isUploaded = SdkService.IsAlreadyUploaded(storageFile, fs, cameraUploadRootNode, mtime); if (isUploaded) { await TaskService.SaveLastUploadDateAsync(storageFile, TaskService.ImageDateSetting); continue; } await UploadAsync(storageFile, fs, cameraUploadRootNode, mtime); // No error, clear error storage await ErrorHandlingService.ClearAsync(ErrorHandlingService.ImageErrorFileSetting, ErrorHandlingService.ImageErrorCountSetting); } } catch (OutOfMemoryException e) { // Something went wrong (could be memory limit) // Just finish this run and try again next time LogService.Log(MLogLevel.LOG_LEVEL_ERROR, "Out of memory while uploading", e); break; } catch (Exception e) { LogService.Log(MLogLevel.LOG_LEVEL_ERROR, "Error uploading item", e); await ErrorHandlingService.SetFileErrorAsync(storageFile.Name, ErrorHandlingService.ImageErrorFileSetting, ErrorHandlingService.ImageErrorCountSetting); } } } else { LogService.Log(MLogLevel.LOG_LEVEL_ERROR, "Failed to fetch nodes"); } } else { LogService.Log(MLogLevel.LOG_LEVEL_ERROR, "Failed to login"); } _deferral.Complete(); }