Beispiel #1
0
        public async Task <bool> DeleteFromDBAsync(FreeDiscItemDownload freeDiscDownloader)
        {
            if (freeDiscDownloader == null)
            {
                #if DEBUG
                Debug.Write("DeleteFromDBAsync: freeDiscDownloader == null");
                #endif
                return(false);
            }
            Debug.Write("DeleteFromDB: ID" + freeDiscDownloader.DBID + " Title: " + freeDiscDownloader?.Title + " Status: " + freeDiscDownloader?.ItemStatus.ToString());
            if (freeDiscDownloader.DBID == 0)
            {
                Debug.Write("DeleteFromDB: freeDiscDownloader.DBID == 0 !");
                return(false);
            }
            try
            {
                var conn = new SQLite.SQLiteAsyncConnection(App.AppSetting.DBDownloadPath);
                await conn.CreateTableAsync <FreeDiscItemDownload>();

                await conn.DeleteAsync(freeDiscDownloader);
            }
            catch (Exception e)
            {
                Debug.Write("DeleteFromDB: Delete error !");
                return(false);
            }
            return(true);
        }
Beispiel #2
0
        public async Task <bool> SaveToDBAsync(FreeDiscItemDownload freeDiscDownloader)
        {
            if (freeDiscDownloader == null)
            {
                #if DEBUG
                Debug.Write("SaveToDBAsync: freeDiscDownloader == null");
                #endif
                return(false);
            }
            Debug.Write("SaveToDB: ID" + freeDiscDownloader.DBID + " Title: " + freeDiscDownloader?.Title + " Status: " + freeDiscDownloader?.ItemStatus.ToString());
            try
            {
                var conn = new SQLite.SQLiteAsyncConnection(App.AppSetting.DBDownloadPath);
                await conn.CreateTableAsync <FreeDiscItemDownload>();

                await conn.InsertAsync(freeDiscDownloader);
            }
            catch (Exception e)
            {
                Debug.Write("SaveToDB: Save error !");
                return(false);
            }

            Debug.Write("SaveToDB: Result ID" + freeDiscDownloader?.DBID ?? "NULL");
            return(true);
        }
Beispiel #3
0
 public bool SaveToDB(FreeDiscItemDownload freeDiscDownloader)
 {
     if (freeDiscDownloader == null)
     {
         return(false);
     }
     using (var conn = new SQLite.SQLiteConnection(App.AppSetting.DBDownloadPath))
     {
         conn.CreateTable <FreeDiscItemDownload>();
         var rowsCount = conn.Insert(freeDiscDownloader);
         if (rowsCount == 0)
         {
             return(false);
         }
     }
     return(true);
 }
        // add new item from search model
        public async Task AddNewItemToDownloadAsync(FreeDiscItem itemToAdd)
        {
            Debug.WriteLine("AddNewItemToDownload()");
            if (itemToAdd == null)
            {
                Debug.WriteLine("AddNewItemToDownload: itemToAdd = null");
                return;
            }
            if (IsFreeDiscItemDownloadOnQueue(itemToAdd))
            {
                Debug.WriteLine( "Element już istnieje na liście do pobrania" + itemToAdd.Title );
                return;
            }

            var downloaditem = new FreeDiscItemDownload(itemToAdd)
            {
                RowEven = (DownloadItemList?.Count ?? 0) > 0 ? !DownloadItemList[0]?.RowEven ?? true : true
            };

            DownloadItemList.Insert(0, downloaditem);
            await _freeDiscItemDownloadRepository.SaveToDBAsync(downloaditem);
            await DownloadQueueProcessAsync();
        }
Beispiel #5
0
        public async void DownloadItem(FreeDiscItemDownload freeDiscDownloader)
        {
            if (CurrentDownloadFile != null)
            {
                if (IsDownloadInProgress())
                {
                    Debug.WriteLine("DownloadItem: CurrentDownloadFile is active!");
                    return;
                }
            }

            if (freeDiscDownloader == null)
            {
                Debug.WriteLine("DownloadItem: freeDiscDownloader == null");
                return;
            }

            if (freeDiscDownloader.Url.Length < 6)
            {
                Debug.WriteLine("DownloadItem: freeDiscDownloader.Url.Length < 6");
                return;
            }

            await Task.Yield();

            await Task.Run(async() =>
            {
                Debug.WriteLine("DownloadItem: Downloading file: " + freeDiscDownloader.Url);
                freeDiscDownloader.ItemStatus             = DownloadStatus.DownloadInProgress;
                var downloadManager                       = CrossDownloadManager.Current;
                downloadManager.PathNameForDownloadedFile = new System.Func <IDownloadFile, string>(file => { return(freeDiscDownloader.FilePath); });

                CurrentDownloadFile = downloadManager.CreateDownloadFile(freeDiscDownloader.Url);

                CurrentDownloadFile.Headers.Add("Host", @"stream.freedisc.pl");
                CurrentDownloadFile.Headers.Add("Accept", @"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                CurrentDownloadFile.Headers.Add("Accept-Language", @"pl,en-US;q=0.7,en;q=0.3");
                CurrentDownloadFile.Headers.Add("Accept-Encoding", @"");
                CurrentDownloadFile.Headers.Add("Connection", @"keep-alive");
                CurrentDownloadFile.Headers.Add("Pragma", @"no-cache");
                CurrentDownloadFile.Headers.Add("DNT", @"1");
                CurrentDownloadFile.Headers.Add("Cache-Control", @"no-cache");
                CurrentDownloadFile.Headers.Add("Referer", @"http://reseton.pl/static/player/v612/jwplayer.flash.swf");
                CurrentDownloadFile.Headers.Add("User-Agent", @"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0");

                downloadManager.Start(CurrentDownloadFile, true);
                var isDownloading = true;
                while (isDownloading)
                {
                    isDownloading = IsDownloadInProgress();
                    if (isDownloading)
                    {
                        if (CurrentDownloadFile.TotalBytesExpected > 0)
                        {
                            freeDiscDownloader.DownloadProgres = CurrentDownloadFile.TotalBytesWritten / CurrentDownloadFile.TotalBytesExpected;
                        }
                    }
                    await Task.Delay(500);
                }
            });

            switch (CurrentDownloadFile.Status)
            {
            case DownloadFileStatus.COMPLETED:
                freeDiscDownloader.ItemStatus = DownloadStatus.DownloadFinish;
                break;

            case DownloadFileStatus.CANCELED:
                freeDiscDownloader.ItemStatus = DownloadStatus.WaitingForDownload;
                break;

            default:
                freeDiscDownloader.ItemStatus = DownloadStatus.DownloadInterrupted;
                break;
            }
        }
Beispiel #6
0
 public bool UpdateDB(FreeDiscItemDownload freeDiscDownloader)
 {
     return(false);
 }
Beispiel #7
0
 public bool DeleteFromDB(FreeDiscItemDownload freeDiscDownloader)
 {
     return(false);
 }
Beispiel #8
0
        public async Task <bool> DownloadItemAsync(FreeDiscItemDownload freeDiscDownloader)
        {
            CurrentfreeDiscDownloader = freeDiscDownloader;
            // checking permission
            bool havePermisson = false;

            try
            {
                var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);

                if (status != PermissionStatus.Granted)
                {
                    var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Storage);

                    if (results.ContainsKey(Permission.Storage))
                    {
                        status = results[Permission.Storage];
                    }
                }
                if (status == PermissionStatus.Granted)
                {
                    havePermisson = true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write("DownloadItemAsync: Exception: " + ex.ToString());
            }

            if (!havePermisson)
            {
                await Application.Current.MainPage.DisplayAlert(freeDiscDownloader?.Title,
                                                                "Nie można pobrać pliku. Aplikacja nie posiada uprawnień do zapisu plików na urządzeniu", "OK");

                freeDiscDownloader.ItemStatus      = DownloadStatus.DownloadInterrupted;
                freeDiscDownloader.DownloadProgres = 0;
                await UpdateDBAsync(freeDiscDownloader);

                return(false);
            }

            //checking input data
            if (IsDownloadInProgress())
            {
                Debug.Write("DownloadItemAsync: IsDownloadInProgress()");
                return(false);
            }

            if (freeDiscDownloader.Url.Length < 5)
            {
                Debug.Write("DownloadItemAsync: freeDiscDownloader.Url.Length < 5");
                return(false);
            }

            if (freeDiscDownloader.FilePath.Length == 0)
            {
                Debug.Write("DownloadItemAsync: freeDiscDownloader.FilePath.Length == 0");
                return(false);
            }

            if (!Directory.Exists(freeDiscDownloader.FileDirectory))
            {
                Debug.Write("DownloadItemAsync: !Directory.Exists(freeDiscDownloader.FileDirectory: " + freeDiscDownloader.FileDirectory);
                try
                {
                    Directory.CreateDirectory(freeDiscDownloader.FileDirectory);
                }
                catch (Exception e)
                {
                    Debug.Write("DownloadItemAsync: CreateDirectory error: " + e.ToString());
                    return(false);
                }
            }

            if (File.Exists(freeDiscDownloader.FilePath))
            {
                Debug.Write("DownloadItemAsync: File.Exists(freeDiscDownloader.FilePath)");
                try
                {
                    File.Delete(freeDiscDownloader.FilePath);
                }
                catch (Exception e)
                {
                    Debug.Write("DownloadItemAsync: File.Delete error: " + e.ToString());
                    return(false);
                }
            }

            // begin download
            Debug.Write("DownloadItemAsync: begin download");
            freeDiscDownloader.DownloadProgres = 0;
            freeDiscDownloader.ItemStatus      = DownloadStatus.DownloadInProgress;
            await UpdateDBAsync(freeDiscDownloader);

            CurrentFileDownload = new FileDownload(freeDiscDownloader.Url, freeDiscDownloader.FilePath, 64 * 1024);

            CurrentFileDownload.DownloadProgressChanged += (e, a) =>
            {
                var inst = e as FileDownload;
                if (inst.ContentLength > 0)
                {
                    freeDiscDownloader.DownloadProgres = (double)inst.BytesWritten / inst.ContentLength;
                }
            };

            CurrentFileDownload.DownloadCompleted += (e, a) =>
            {
                freeDiscDownloader.DownloadProgres = 1;
            };

            await CurrentFileDownload.Start();

            Debug.Write("DownloadItemAsync: ContentLength:" + CurrentFileDownload.ContentLength);
            if (CurrentFileDownload.Done)
            {
                freeDiscDownloader.ItemStatus      = DownloadStatus.DownloadFinish;
                freeDiscDownloader.DownloadProgres = 1;
                await UpdateDBAsync(freeDiscDownloader);

                Debug.Write("DownloadItemAsync: CurrentFileDownload.Done");
                return(true);
            }

            freeDiscDownloader.ItemStatus      = DownloadStatus.DownloadInterrupted;
            freeDiscDownloader.DownloadProgres = 0;
            await UpdateDBAsync(freeDiscDownloader);

            Debug.Write("DownloadItemAsync:  DownloadStatus.DownloadInterrupted;");
            return(false);
        }
        public async Task <bool> DownloadItemAsync(FreeDiscItemDownload freeDiscDownloader)
        {
            if (freeDiscDownloader == null)
            {
                #if DEBUG
                Debug.Write("DownloadItemAsync: freeDiscDownloader == null");
                #endif
                return(false);
            }
            CurrentfreeDiscDownloader = freeDiscDownloader;
            // checking permission
            bool havePermisson = false;
            try
            {
                var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);

                if (status != PermissionStatus.Granted)
                {
                    var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Storage);

                    if (results.ContainsKey(Permission.Storage))
                    {
                        status = results[Permission.Storage];
                    }
                }
                if (status == PermissionStatus.Granted)
                {
                    havePermisson = true;
                }
            }
            catch (Exception ex)
            {
                #if DEBUG
                Debug.Write("DownloadItemAsync: Exception: " + ex.ToString());
                #endif
            }

            if (!havePermisson)
            {
                await Application.Current.MainPage.DisplayAlert(freeDiscDownloader?.Title,
                                                                "Nie można pobrać pliku. Aplikacja nie posiada uprawnień do zapisu plików na urządzeniu", "OK");

                freeDiscDownloader.ItemStatus      = DownloadStatus.DownloadInterrupted;
                freeDiscDownloader.DownloadProgres = 0;

                await UpdateDBAsync(freeDiscDownloader);

                return(false);
            }

            //checking input data
            if (IsDownloadInProgress())
            {
                #if DEBUG
                Debug.Write("DownloadItemAsync: IsDownloadInProgress()");
                #endif
                return(false);
            }

            if (!Uri.IsWellFormedUriString(freeDiscDownloader.Url, UriKind.Absolute))
            {
                #if DEBUG
                Debug.Write("DownloadItemAsync: URI is incorrect");
                #endif
                return(false);
            }


            if (!ExtensionMethods.IsValidPath(freeDiscDownloader.FilePath, true))
            {
                #if DEBUG
                Debug.Write("DownloadItemAsync: freeDiscDownloader.FilePath is incorrect");
                #endif
                return(false);
            }

            if (!Directory.Exists(freeDiscDownloader.FileDirectory))
            {
                #if DEBUG
                Debug.Write("DownloadItemAsync: !Directory.Exists(freeDiscDownloader.FileDirectory: " + freeDiscDownloader.FileDirectory);
                #endif
                try
                {
                    Directory.CreateDirectory(freeDiscDownloader.FileDirectory);
                }
                catch (Exception e)
                {
                    #if DEBUG
                    Debug.Write("DownloadItemAsync: CreateDirectory error: " + e.ToString());
                    #endif
                    return(false);
                }
            }

            if (File.Exists(freeDiscDownloader.FilePath))
            {
                Debug.Write("DownloadItemAsync: File.Exists(freeDiscDownloader.FilePath)");
                try
                {
                    File.Delete(freeDiscDownloader.FilePath);
                }
                catch (Exception e)
                {
                    Debug.Write("DownloadItemAsync: File.Delete error: " + e.ToString());
                    return(false);
                }
            }

            // begin download
            #if DEBUG
            Debug.Write("DownloadItemAsync: begin download");
            #endif
            freeDiscDownloader.DownloadProgres = 0;
            freeDiscDownloader.ItemStatus      = DownloadStatus.DownloadInProgress;

            await UpdateDBAsync(freeDiscDownloader);

            CurrentCancellationToken = new CancellationTokenSource();

            bool result = false;
            using (var client = new HttpClientDownloadWithProgress(freeDiscDownloader.Url, freeDiscDownloader.FilePath, CurrentCancellationToken))
            {
                client.ProgressChanged += (totalFileSize, totalBytesDownloaded, progressPercentage) => {
                    freeDiscDownloader.DownloadProgres = (double)(progressPercentage / 100);
                };

                await client.StartDownload();

                result = client.Done;
            }

            if (result)
            {
                freeDiscDownloader.ItemStatus      = DownloadStatus.DownloadFinish;
                freeDiscDownloader.DownloadProgres = 1;

                Debug.Write("DownloadItemAsync: Co jest do kurwy: " + freeDiscDownloader.ItemStatus.ToString());
                await UpdateDBAsync(freeDiscDownloader);

                #if DEBUG
                Debug.Write("DownloadItemAsync: DownloadFinish");
                #endif
                return(true);
            }

            freeDiscDownloader.ItemStatus      = DownloadStatus.DownloadInterrupted;
            freeDiscDownloader.DownloadProgres = 0;

            await UpdateDBAsync(freeDiscDownloader);

            #if DEBUG
            Debug.Write("DownloadItemAsync:  DownloadInterrupted");
            #endif
            return(false);
        }