/// <summary>
 /// Updates the current progress and call the <see cref="ProgressChanged"/> event to notify listeners.
 /// </summary>
 private void UpdateProgress(IDownloadProgress progress)
 {
     var changed = ProgressChanged;
     if (changed != null)
     {
         changed(progress);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Helper method to download assets asynchronously. After completion, scene is automatically reloaded
        /// if new assets have been downloaded.
        /// </summary>
        /// <param name="config">Current config</param>
        /// <param name="downloadProgress">Download progress interface - used to indicate download progress</param>
        /// <returns></returns>
        public static async Task DownloadAssetsAsync(Config config, IDownloadProgress downloadProgress = null, bool forceCheck = false)
        {
            // Start background thread
            Task.Run(async() =>
            {
                // Mutual exclusion while loading assets
                await AssetsDownloader.semaphoreSlim.WaitAsync();

                // Attach / detatch JNI. Required for any calls into JNI from background threads.
                AndroidJNI.AttachCurrentThread();

                try
                {
                    // Download assets from repos.
                    AssetsDownloader assetsDownloader = new AssetsDownloader();
                    return(await assetsDownloader.DownloadFromReposAsync(config, downloadProgress, forceCheck));
                }
                finally
                {
                    AssetsDownloader.semaphoreSlim.Release();
                    AndroidJNI.DetachCurrentThread();
                }
            }).ContinueWith((downloadedAssets) =>
            {
                if (downloadedAssets.Result)
                {
                    // We downloaded new assets, so re-load the scene
                    Debug.Log("Downloaded new assets. Re-populating panel");
                    SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().name);
                }
            });
        }
        private async Task ExtractContents(ICollection <ZipArchiveEntry> entries, string destination,
                                           IEntry baseEntry = null, IDownloadProgress progressObserver = null)
        {
            var total  = entries.Count;
            var copied = 0;

            foreach (var entry in entries)
            {
                var filePath = entry.Key.Substring(baseEntry?.Key.Length ?? 0);

                var destFile = Path.Combine(destination, filePath.Replace("/", "\\"));
                var dir      = Path.GetDirectoryName(destFile);
                if (!fileSystemOperations.DirectoryExists(dir))
                {
                    fileSystemOperations.CreateDirectory(dir);
                }

                using (var destStream = File.Open(destFile, FileMode.OpenOrCreate))
                    using (var stream = entry.OpenEntryStream())
                    {
                        await stream.CopyToAsync(destStream);

                        copied++;
                        progressObserver?.Percentage.OnNext(copied / (double)total);
                    }
            }

            progressObserver?.Percentage.OnNext(double.NaN);
        }
Beispiel #4
0
 public async Task Download(string url, string path, IDownloadProgress progressObserver = null, int timeout = 30)
 {
     using (var fileStream = File.OpenWrite(path))
     {
         await Download(url, fileStream, progressObserver, timeout);
     }
 }
Beispiel #5
0
        private void OnMediaDownloaderProgressChanged(IDownloadProgress obj)
        {
            string content;

            switch (obj.Status)
            {
            case DownloadStatus.NotStarted:
                content = "Not Started";
                break;

            case DownloadStatus.Downloading:
                content = "downloading the file...";
                break;

            case DownloadStatus.Failed:
                content = "download failed";
                break;

            case DownloadStatus.Completed:
                content = "download completed";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            MessagingCenter.Send(new DownloadStatusModel()
            {
                DownloadStatus = content
            }, "DownloadStatus");
        }
Beispiel #6
0
        static void Download_ProgressChanged(IDownloadProgress progress)
        {
            switch (progress.Status)
            {
            case DownloadStatus.Downloading:
            {
                Console.WriteLine(progress.BytesDownloaded);
                break;
            }

            case DownloadStatus.Completed:
            {
                Console.WriteLine("Download complete.");

                // Hoàn thành việc tải file xuống MemoryStream thì thực hiện việc chuyển MemoryStream ra FileStream thực tế

                break;
            }

            case DownloadStatus.Failed:
            {
                MessageBox.Show("Download failed. Try again");
                break;
            }
            }
        }
Beispiel #7
0
        private async Task Download(string url, Stream destination, IDownloadProgress progressObserver = null,
                                    int timeout = 30)
        {
            long?totalBytes   = 0;
            long bytesWritten = 0;

            await ObservableMixin.Using(() => client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead),
                                        s =>
            {
                totalBytes = s.Content.Headers.ContentLength;
                if (!totalBytes.HasValue)
                {
                    progressObserver?.Percentage.OnNext(double.PositiveInfinity);
                }
                return(ObservableMixin.Using(() => s.Content.ReadAsStreamAsync(),
                                             contentStream => contentStream.ReadToEndObservable()));
            })
            .Do(bytes =>
            {
                bytesWritten += bytes.Length;
                if (totalBytes.HasValue)
                {
                    progressObserver?.Percentage.OnNext((double)bytesWritten / totalBytes.Value);
                }

                progressObserver?.BytesDownloaded?.OnNext(bytesWritten);
            })
            .Timeout(TimeSpan.FromSeconds(timeout))
            .Select(bytes => Observable.FromAsync(async() =>
            {
                await destination.WriteAsync(bytes, 0, bytes.Length);
                return(Unit.Default);
            }))
            .Merge(1);
        }
        private async Task Run(string args, IDownloadProgress progressObserver)
        {
            var dismName = WindowsCommandLineUtils.Dism;
            ISubject <string> outputSubject         = new Subject <string>();
            IDisposable       stdOutputSubscription = null;

            if (progressObserver != null)
            {
                stdOutputSubscription = outputSubject
                                        .Select(GetPercentage)
                                        .Where(d => !double.IsNaN(d))
                                        .Subscribe(progressObserver.Percentage);
            }

            Log.Verbose("We are about to run DISM: {ExecName} {Parameters}", dismName, args);
            var resultCode = await ProcessUtils.RunProcessAsync(dismName, args, outputSubject);

            progressObserver?.Percentage.OnNext(double.NaN);

            if (resultCode != 0)
            {
                throw new DeploymentException(
                          $"There has been a problem during deployment: DISM exited with code {resultCode}.");
            }

            stdOutputSubscription?.Dispose();
        }
Beispiel #9
0
        public AdvancedViewModel(ISettingsService settingsService, IFileSystemOperations fileSystemOperations,
                                 UIServices uiServices, IProviderBasedWindowsDeployer deployer,
                                 IDiskLayoutPreparer preparer,
                                 IWindowsOptionsProvider optionsProvider, IDeviceProvider deviceProvider, IDownloadProgress progress, StatusViewModel statusViewModel, IPhone phone)
        {
            StatusViewModel      = statusViewModel;
            this.settingsService = settingsService;
            this.uiServices      = uiServices;
            this.deployer        = deployer;
            this.preparer        = preparer;
            this.optionsProvider = optionsProvider;
            this.deviceProvider  = deviceProvider;
            this.progress        = progress;
            this.phone           = phone;

            sizeReservedForWindows =
                this.WhenAnyValue(x => x.GbsReservedForWindows, ByteSize.FromGigaBytes)
                .ToProperty(this, x => x.SizeReservedForWindows);

            DeleteDownloadedWrapper     = new CommandWrapper <Unit, Unit>(this, ReactiveCommand.CreateFromTask(() => DeleteDownloaded(fileSystemOperations)), uiServices.Dialog);
            ForceDualBootWrapper        = new CommandWrapper <Unit, Unit>(this, ReactiveCommand.CreateFromTask(ForceDualBoot), uiServices.Dialog);
            ForceDisableDualBootWrapper = new CommandWrapper <Unit, Unit>(this, ReactiveCommand.CreateFromTask(ForceDisableDualBoot), uiServices.Dialog);

            BackupCommandWrapper  = new CommandWrapper <Unit, Unit>(this, ReactiveCommand.CreateFromTask(Backup), uiServices.Dialog);
            RestoreCommandWrapper = new CommandWrapper <Unit, Unit>(this, ReactiveCommand.CreateFromTask(Restore), uiServices.Dialog);

            IsBusyObservable = Observable.Merge(new []
            {
                DeleteDownloadedWrapper.Command.IsExecuting,
                BackupCommandWrapper.Command.IsExecuting,
                RestoreCommandWrapper.Command.IsExecuting,
                ForceDualBootWrapper.Command.IsExecuting,
                ForceDisableDualBootWrapper.Command.IsExecuting,
            });
        }
Beispiel #10
0
        private static async Task Execute(IEnumerable <string> args, IDownloadProgress progress)
        {
            var optionsProvider = new WindowsDeploymentOptionsProvider();

            var deployer = GetDeployer(optionsProvider, progress);

            var parserResult = Parser.Default
                               .ParseArguments <WindowsDeploymentCmdOptions,
                                                EnableDualBootCmdOptions,
                                                DisableDualBootCmdOptions,
                                                NonWindowsDeploymentCmdOptions>(args);

            await parserResult
            .MapResult(
                (WindowsDeploymentCmdOptions opts) =>
            {
                optionsProvider.Options = new WindowsDeploymentOptions()
                {
                    ImageIndex             = opts.Index,
                    ImagePath              = opts.WimImage,
                    SizeReservedForWindows = ByteSize.FromGigaBytes(opts.ReservedSizeForWindowsInGb),
                    UseCompact             = opts.UseCompact,
                };
                return(deployer.Deploy());
            },
                (EnableDualBootCmdOptions opts) => deployer.ToggleDualBoot(true),
                (DisableDualBootCmdOptions opts) => deployer.ToggleDualBoot(false),
                (NonWindowsDeploymentCmdOptions opts) => deployer.Deploy(),
                HandleErrors);
        }
Beispiel #11
0
 public DeployWindows(IDeviceProvider deviceProvider, IDiskLayoutPreparer preparer, IProviderBasedWindowsDeployer providerBasedWindowsDeployer, IDownloadProgress progressObserver)
 {
     this.deviceProvider = deviceProvider;
     this.preparer       = preparer;
     this.providerBasedWindowsDeployer = providerBasedWindowsDeployer;
     this.progressObserver             = progressObserver;
 }
Beispiel #12
0
        public async Task Capture(string destination, IDownloadProgress progressObserver)
        {
            Log.Information("Preparing for Windows backup...");

            var device = deviceProvider.Device;
            await deployer.Backup(await device.GetWindowsVolume(), destination, progressObserver);
        }
Beispiel #13
0
 private void OnDownloadProgress(
     IDownloadProgress downloadProgress,
     ulong totalSize,
     IGcsFileOperationCallback operation)
 {
     operation.Progress((double)downloadProgress.BytesDownloaded / totalSize);
 }
Beispiel #14
0
        /// <summary>
        /// Download assets from repos. The download manifest is also updated.
        /// </summary>
        /// <param name="manifest">Download manifest</param>
        /// <param name="assetsInfo">Assets to download</param>
        /// <param name="downloadProgress">Download progress interface</param>
        /// <returns></returns>
        private async Task <bool> DownloadFromReposInternalAsync(
            AssetsManifest manifest, Dictionary <string, AssetInfo> assetsInfo, IDownloadProgress downloadProgress = null)
        {
            var downloadedAsset = false;

            foreach (var entry in assetsInfo)
            {
                // Download asset
                var success = await DownloadAssetFromGitHubRepoAsync(entry.Key, entry.Value, downloadProgress);

                if (success)
                {
                    // Update manifest
                    manifest.Metadata[entry.Key] = entry.Value;
                    downloadedAsset = true;
                }
            }

            if (downloadedAsset)
            {
                // Persist manifest
                SaveManifest(manifest);
            }

            return(downloadedAsset);
        }
Beispiel #15
0
        private void Download_ProgressChanged(IDownloadProgress progress)
        {
            label1.Invoke(new Action(() =>
                                     label1.Text = String.Format("{0:0.00} MB", progress.BytesDownloaded / 1048576.0)));

            //progressBar1.Invoke(new Action(() => progressBar1.Value = (int)(((double)progress.BytesDownloaded / size) * 100)));
        }
Beispiel #16
0
        private static IWoaDeployer GetDeployer(WindowsDeploymentOptionsProvider op, IDownloadProgress progress)
        {
            var container = CompositionRoot.CreateContainer(op, progress);

            var deployer = container.Locate <IWoaDeployer>();

            return(deployer);
        }
Beispiel #17
0
        public async Task Deploy(WindowsDeploymentOptions options, IDevice device, IDownloadProgress progressObserver)
        {
            Log.Information("Applying Windows Image");
            progressObserver.Percentage.OnNext(double.NaN);
            await imageService.ApplyImage(await device.GetWindowsVolume(), options.ImagePath, options.ImageIndex, options.UseCompact, progressObserver);

            await MakeBootable(device);
        }
Beispiel #18
0
 private void Download_ProgressChanged(IDownloadProgress progress)
 {
     if (isStarting == true)
     {
         System.Diagnostics.Process.Start("C:\\Img_0226.MOV");
         isStarting = false;
     }
     Console.WriteLine(progress.Status + " " + progress.BytesDownloaded);
 }
Beispiel #19
0
        /// <summary>
        /// Asynchronously download assets from configured repos.
        /// </summary>
        /// <param name="config">Current config</param>
        /// <param name="downloadProgress">Download progress interface</param>
        /// <returns></returns>
        private async Task <bool> DownloadFromReposAsync(Config config, IDownloadProgress downloadProgress = null, bool forceCheck = false)
        {
            if (null == config.downloadRepos)
            {
                // No repos configured, so return
                return(false);
            }

            // Load the download manifest. This is used to compare if we're up-to-date or not.
            AssetsManifest manifest = LoadManifest();

            if (null == manifest)
            {
                manifest = new AssetsManifest();
            }

            // Rate limit update checks to one per couple of minutes, to avoid GitHub's rate limit
            if (!forceCheck && DateTime.Now.Subtract(manifest.LastUpdated).TotalMinutes < RateLimitInMins)
            {
                Debug.LogFormat("Exceeded rate limit of {0} mins - last checked for update on {1}", RateLimitInMins, manifest.LastUpdated);
                return(false);
            }

            // Mark that we've just checked for updates & update manifest
            manifest.LastUpdated = DateTime.Now;
            SaveManifest(manifest);

            if (null != downloadProgress)
            {
                downloadProgress.OnCheckingForUpdates();
            }

            // Download the assets metadata - used to determine whether we are up-to-date or not
            var assetsInfo = await DownloadAssetsMetadata(config, manifest, downloadProgress, forceCheck);

            if (assetsInfo.Count == 0)
            {
                // No updates have been found, so return
                Debug.Log("No updates found");
                if (null != downloadProgress)
                {
                    downloadProgress.OnNoUpdatesAvailable();
                }

                return(false);
            }

            // Download the assets
            var downloadedAssets = await DownloadFromReposInternalAsync(manifest, assetsInfo, downloadProgress);

            if (null != downloadProgress)
            {
                downloadProgress.OnUpdateFinish();
            }

            return(downloadedAssets);
        }
Beispiel #20
0
        /// <summary>
        /// Updates the current progress and call the <see cref="ProgressChanged"/> event to notify listeners.
        /// </summary>
        private void UpdateProgress(IDownloadProgress progress)
        {
            var changed = ProgressChanged;

            if (changed != null)
            {
                changed(progress);
            }
        }
Beispiel #21
0
        public FetchAzureDevOpsArtifact(string descriptor, IAzureDevOpsBuildClient buildClient, IZipExtractor extractor, IDownloader downloader, IDownloadProgress progressObserver)
        {
            ParseDescriptor(descriptor);

            this.buildClient      = buildClient;
            this.extractor        = extractor;
            this.downloader       = downloader;
            this.progressObserver = progressObserver;
        }
Beispiel #22
0
        public async Task <Stream> GetStream(string url, IDownloadProgress progress = null, int timeout = 30)
        {
            var tmpFile = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
            var stream  = File.Create(tmpFile, BufferSize, FileOptions.DeleteOnClose);

            await Download(url, stream, progress, timeout);

            return(stream);
        }
 public async Task ExtractToFolder(Stream stream, string destination, IDownloadProgress progressObserver = null)
 {
     await ExtractCore(stream, destination,
                       zipArchive =>
     {
         var contents = zipArchive.Entries.Where(x => !x.IsDirectory);
         return(contents);
     }, progressObserver : progressObserver);
 }
Beispiel #24
0
        public static string DownloadFileToTemp(string remoteFilename, IDownloadProgress progress)
        {
            var localFilePath = Path.GetTempFileName();

            if (DownloadFile(remoteFilename, localFilePath, progress))
            {
                return(localFilePath);
            }
            return(string.Empty);
        }
Beispiel #25
0
 public Fetch(string url, string destination, IZipExtractor extractor,
              IFileSystemOperations fileSystemOperations, IDownloader downloader, IDownloadProgress progressObserver)
 {
     this.url                  = url;
     this.destination          = destination;
     this.extractor            = extractor;
     this.fileSystemOperations = fileSystemOperations;
     this.downloader           = downloader;
     this.progressObserver     = progressObserver;
 }
Beispiel #26
0
        /// <inheritdoc/>
        public async Task SendDownloadProgressUpdate(IDownloadProgress downloadProgress)
        {
            if (_progressHub?.Clients?.All == null)
            {
                Log.Error("No Clients connected to ProgressHub");
                return;
            }

            await _progressHub.Clients.All.SendAsync("DownloadProgress", downloadProgress);
        }
 public void Download(DownloadInfo di, IDownloadProgress dpClient)
 {
     stop = false;
     this.dpClient = dpClient;
     this.di = di;
      if (di.Link.ScrapState == LinkScrapState.FullyLoaded)
         OnScrapVideoDetailsCompleted(di.Link);
     else
         new ScraperService().ScrapVideosDetailsAsync(this, di.Link);
 }
Beispiel #28
0
 public FetchGitHubFolder(string url, string relativePath, string destination, IZipExtractor zipExtractor,
                          IFileSystemOperations fileSystemOperations, IDownloader downloader, IDownloadProgress progressObserver)
 {
     this.url                  = url;
     this.relativePath         = relativePath;
     this.destination          = destination;
     this.zipExtractor         = zipExtractor;
     this.fileSystemOperations = fileSystemOperations;
     this.downloader           = downloader;
     this.progressObserver     = progressObserver;
 }
Beispiel #29
0
        private static void DownloadFile(string fileId, string targetDirectory)
        {
            FilesResource.GetRequest request = driveService.Files.Get(fileId);
            request.Fields = "size,name,id,md5Checksum";
            GoogleFile        fileinfo = request.Execute();
            var               stream   = new MemoryStream();
            IDownloadProgress progress = request.DownloadWithStatus(stream);

            EditorCoroutineUtility.StartCoroutine(DownloadRoutine(progress, stream, fileId, fileinfo.Name, fileinfo.Size.Value, targetDirectory), Instance);
            WriteTasks.TryAdd(fileId, false);
        }
 public async Task ExtractRelativeFolder(Stream stream, string relativeZipPath, string destination,
                                         IDownloadProgress progressObserver = null)
 {
     await ExtractCore(stream, destination,
                       zipArchive =>
     {
         var baseEntry = relativeZipPath;
         var contents  = zipArchive.Entries.Where(x => x.Key.StartsWith(baseEntry) && !x.IsDirectory);
         return(contents);
     }, progressObserver : progressObserver);
 }
 public async Task ExtractFirstChildToFolder(Stream stream, string destination,
                                             IDownloadProgress progressObserver = null)
 {
     await ExtractCore(stream, destination,
                       zipArchive =>
     {
         var baseEntry = FirstChild(zipArchive.Entries);
         var contents  = zipArchive.Entries.Where(x => x.Key.StartsWith(baseEntry.Key) && !x.IsDirectory);
         return(contents);
     }, FirstChild, progressObserver);
 }
Beispiel #32
0
        public FetchGitHubLatestReleaseAsset(string repoUrl, string assetName, IZipExtractor extractor, IGitHubClient gitHubClient, IDownloader downloader, IDownloadProgress progressObserver)
        {
            this.repoUrl          = repoUrl ?? throw new ArgumentNullException(nameof(repoUrl));
            this.assetName        = assetName ?? throw new ArgumentNullException(nameof(assetName));
            this.extractor        = extractor ?? throw new ArgumentNullException(nameof(extractor));
            this.gitHubClient     = gitHubClient;
            this.downloader       = downloader;
            this.progressObserver = progressObserver;

            folderPath = Path.Combine(SubFolder, Path.GetFileNameWithoutExtension(assetName));
        }
            protected void DriveService_ProgressChanged(IDownloadProgress downloadProgress)
            {
                try
                {
                  var status = StatusType.NotStarted;
                  long bytesProcessed = downloadProgress.BytesDownloaded;
                  long totalBytes = FileSize;
                  Exception processException = downloadProgress.Exception;

                  if (downloadProgress.Status == DownloadStatus.Completed)
                  {
                status = StatusType.Completed;
                  }
                  else if (downloadProgress.Status == DownloadStatus.Downloading)
                  {
                status = StatusType.Processing;
                  }
                  else if (downloadProgress.Status == DownloadStatus.Failed)
                  {
                status = StatusType.Failed;
                  }
                  else
                  {
                status = StatusType.Starting;
                  }

                  UpdateProgress(status, bytesProcessed, totalBytes, processException);

                  if (_Status == StatusType.Failed && String.IsNullOrEmpty(_ExceptionMessage))
                  {
                Debugger.Break();
                  }

                  if (Processed)
                  {
                try
                {
                  if (Completed)
                  {
                if (!String.IsNullOrEmpty(_DownloadFilePath))
                {
                  if (!String.Equals(_DownloadFilePath, _FileInfo.FilePath, StringComparison.CurrentCultureIgnoreCase))
                  {
                    API.DriveService.MoveFile(_DownloadFilePath, _FileInfo.FilePath);

                    _DownloadFilePath = "";
                  }
                }
                  }
                  else
                  {
                if (!String.IsNullOrEmpty(_DownloadFilePath))
                {
                  API.DriveService.DeleteFile(_DownloadFilePath);

                  _DownloadFilePath = "";
                }

                _DriveService.CleanupFile(_FileInfo, true);
                  }
                }
                catch (Exception exception)
                {
                  Log.Error(exception, false);

                  if (!Failed)
                  {
                _ExceptionMessage = exception.Message;
                _Status = StatusType.Failed;
                  }
                }

                try
                {
                  if (Completed)
                  {
                _LastWriteTime = API.DriveService.GetFileLastWriteTime(FilePath);

                if (!API.DriveService.IsDateTimeEqual(_LastWriteTime, _FileInfo.ModifiedDate))
                {
                  _LastWriteTime = API.DriveService.SetFileLastWriteTime(FilePath, _FileInfo.ModifiedDate);
                }
                  }
                }
                catch (Exception exception)
                {
                  Log.Error(exception, false);

                  if (!Failed)
                  {
                _ExceptionMessage = exception.Message;
                _Status = StatusType.Failed;
                  }
                }
                  }

                  if (_Status == StatusType.Failed && String.IsNullOrEmpty(_ExceptionMessage))
                  {
                Debugger.Break();
                  }

                  InvokeOnProgressEvent();
                }
                catch (Exception exception)
                {
                  Log.Error(exception, false);
                }
            }
Beispiel #34
0
 static void Download_ProgressChanged(IDownloadProgress progress)
 {
    Console.WriteLine(progress.Status + " " + progress.BytesDownloaded + " bytes");
 }
 private void CheckForError(IDownloadProgress downloadProgress)
 {
     if (downloadProgress.Status == DownloadStatus.Failed || downloadProgress.Exception != null)
     {
         GoogleApiException googleApiException = downloadProgress.Exception as GoogleApiException;
         if (googleApiException != null && googleApiException.HttpStatusCode == HttpStatusCode.NotFound)
         {
             ErrorRecord errorRecord = new ErrorRecord(
                 new ItemNotFoundException($"Storage object '{ObjectName}' does not exist."),
                 "ObjectNotFound",
                 ErrorCategory.ObjectNotFound,
                 ObjectName);
             ThrowTerminatingError(errorRecord);
         }
         // Default to just throwing the exception we get.
         throw downloadProgress.Exception;
     }
 }
 /// <summary>
 /// Updates the current progress and call the <see cref="ProgressChanged"/> event to notify listeners.
 /// </summary>
 private void UpdateProgress(IDownloadProgress progress)
 {
     ProgressChanged?.Invoke(progress);
 }