Ejemplo n.º 1
0
        public async void DownloadItem(ResourceItem item)
        {
            var client = new HttpClient();
            var cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken       = cancellationTokenSource.Token;

            CancellationTokens[item.id] = cancellationTokenSource;

            try
            {
                Progress <double> progress = new Progress <double>();

                progress.ProgressChanged += (sender, value) =>
                {
                    ReportProgress(item, value);
                };

                var tempFolder = Windows.Storage.ApplicationData.Current.TemporaryFolder;
                var tempFile   = await tempFolder.CreateFileAsync(GuidHelper.CreateNewGuid().ToString() + ".zip");

                var response = await client.GetAsync(item.item, HttpCompletionOption.ResponseHeadersRead, cancellationToken);

                response.EnsureSuccessStatusCode();

                var total             = response.Content.Headers.ContentLength.HasValue ? response.Content.Headers.ContentLength.Value : -1L;
                var canReportProgress = total != -1 && progress != null;

                using (var stream = await response.Content.ReadAsStreamAsync())
                    using (var outputStream = await tempFile.OpenStreamForWriteAsync())
                    {
                        var totalRead    = 0L;
                        var buffer       = new byte[4096];
                        var isMoreToRead = true;

                        do
                        {
                            cancellationToken.ThrowIfCancellationRequested();
                            var read = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);

                            if (read == 0)
                            {
                                isMoreToRead = false;
                            }
                            else
                            {
                                var data = new byte[read];
                                buffer.ToList().CopyTo(0, data, 0, read);
                                totalRead += read;

                                await outputStream.WriteAsync(data, 0, read);

                                if (canReportProgress)
                                {
                                    ((IProgress <double>)progress).Report((totalRead * 1d) / (total * 1d));
                                }
                            }
                        } while (isMoreToRead);
                    }
                var destinationPath = ItemPath(item);
                ZipFile.ExtractToDirectory(tempFile.Path, destinationPath);
                File.WriteAllText(destinationPath + "\\description.json", JsonConvert.SerializeObject(item));

                CancellationTokens.Remove(item.id);
                DownloadSuccess(item);
            }
            catch
            {
                CancellationTokens.Remove(item.id);
                DownloadFailure(item);
            }
        }
Ejemplo n.º 2
0
 public string ItemPath(ResourceItem item)
 {
     return(AddonFolderPath + "\\" + item.id);
 }
 private void ShowItem(ResourceItem item)
 {
     Nav.IsBackEnabled = true;
     Container.Navigate(typeof(ResourceItemPage), new ItemParameter(item));
 }
Ejemplo n.º 4
0
 private void ReportProgress(ResourceItem item, double progress)
 {
     ProgressUpdate(item, progress);
 }
 public ItemParameter(ResourceItem item)
 {
     this.Item = item;
 }