public void DownloadFile(string ip, string filename, string filetype,
                          ref BlackVueDownloaderCopyStats blackVueDownloaderCopyStats)
 {
     if (_fileSystemHelper.Exists($"Record/{filename}"))
     {
         blackVueDownloaderCopyStats.Ignored++;
     }
     else
     {
         try
         {
             string url  = $"http://{ip}/Record/{filename}";
             var    path = url.DownloadFileAsync("Record");
             Console.WriteLine($"Downloading {filetype} file: {url}");
             path.Wait();
             blackVueDownloaderCopyStats.Copied++;
         }
         catch (FlurlHttpTimeoutException e)
         {
             Console.WriteLine($"FlurlHttpTimeoutException: {e.Message}");
             blackVueDownloaderCopyStats.Errored++;
         }
         catch (FlurlHttpException e)
         {
             if (e.Call.Response != null)
             {
                 Console.WriteLine($"Failed with response code: {e.Call.Response.StatusCode}");
             }
             Console.Write($"Failed before getting a response: {e.Message}");
             blackVueDownloaderCopyStats.Errored++;
         }
         catch (Exception e)
         {
             Console.WriteLine($"Exception: {e.Message}");
             blackVueDownloaderCopyStats.Errored++;
         }
     }
 }
示例#2
0
 public IReadWriteControlFile GetApplicationConfiguration()
 {
     lock (SyncLock)
     {
         if (ControlFile != null)
         {
             return(ControlFile);
         }
         try
         {
             var filename = GetApplicationControlFilePath();
             Logger.Debug(() => $"ApplicationControlFileProvider:GetApplicationConfiguration loading {filename}");
             if (FileSystemHelper.Exists(filename))
             {
                 Logger.Debug(() => $"ApplicationControlFileProvider:GetApplicationConfiguration exists {filename}");
                 ControlFile = ControlFileFactory.OpenControlFile(filename);
             }
         } catch (Exception ex)
         {
             Logger.LogException(() => $"ApplicationControlFileProvider:GetApplicationConfiguration", ex);
         }
     }
     return(ControlFile);
 }
        /// <summary>
        /// For given camera ip, filename, and filetype, download the file and return a status
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="filename"></param>
        /// <param name="filetype"></param>
        /// <param name="tempdir"></param>
        /// <param name="targetdir"></param>
        /// <param name="timeout"></param>
        public void DownloadFile(string ip, string filename, string filetype, string tempdir, string targetdir, int timeout)
        {
            string filepath;
            string tempFilepath;

            try
            {
                filepath = Path.Combine(targetdir, filename);
            }
            catch (Exception e)
            {
                logger.Error($"Path Combine exception for filepath, filename {filename}, Exception Message: {e.Message}");
                BlackVueDownloaderCopyStats.Errored++;
                return;
            }

            try
            {
                tempFilepath = Path.Combine(tempdir, filename);
            }
            catch (Exception e)
            {
                logger.Error($"Path Combine exception for temp_filepath, filename {filename}, Exception Message: {e.Message}");
                BlackVueDownloaderCopyStats.Errored++;
                return;
            }

            if (_fileSystemHelper.Exists(filepath))
            {
                logger.Info($"File exists {filepath}, ignoring");
                BlackVueDownloaderCopyStats.Ignored++;
            }
            else
            {
                try
                {
                    var url = $"http://{ip}/Record/{filename}";

                    var tempfile   = Path.Combine(tempdir, filename);
                    var targetfile = Path.Combine(targetdir, filename);

                    // If it already exists in the _tmp directory, delete it.
                    if (_fileSystemHelper.Exists(tempFilepath))
                    {
                        logger.Info($"File exists in tmp {tempFilepath}, deleting");
                        BlackVueDownloaderCopyStats.TmpDeleted++;
                        _fileSystemHelper.Delete(tempFilepath);
                    }

                    // Download to the temp directory, that way, if the file is partially downloaded,
                    // it won't leave a partial file in the target directory
                    logger.Info($"Downloading {filetype} file: {url}");
                    Stopwatch st = Stopwatch.StartNew();
                    if (timeout > 0)
                    {
                        url.WithTimeout(timeout).DownloadFileAsync(tempdir).Wait();
                    }
                    else
                    {
                        url.DownloadFileAsync(tempdir).Wait();
                    }
                    st.Stop();
                    BlackVueDownloaderCopyStats.DownloadingTime = BlackVueDownloaderCopyStats.DownloadingTime.Add(st.Elapsed);

                    FileInfo fi = new FileInfo(tempfile);

                    BlackVueDownloaderCopyStats.TotalDownloaded += fi.Length;

                    // File downloaded. Move from temp to target.
                    _fileSystemHelper.Move(tempfile, targetfile);

                    logger.Info($"Downloaded {filetype} file: {url}");
                    BlackVueDownloaderCopyStats.Copied++;
                }
                catch (FlurlHttpTimeoutException e)
                {
                    logger.Error($"FlurlHttpTimeoutException: {e.Message}");
                    BlackVueDownloaderCopyStats.Errored++;
                }
                catch (FlurlHttpException e)
                {
                    if (e.Call.Response != null)
                    {
                        logger.Error($"Failed with response code: {e.Call.Response.StatusCode}");
                    }
                    Console.Write($"Failed before getting a response: {e.Message}");
                    BlackVueDownloaderCopyStats.Errored++;
                }
                catch (Exception e)
                {
                    logger.Error($"Exception: {e.Message}");
                    BlackVueDownloaderCopyStats.Errored++;
                }
            }
        }
示例#4
0
        /// <summary>
        /// For given camera ip, filename, and filetype, download the file and return a status
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="filename"></param>
        /// <param name="filetype"></param>
        public void DownloadFile(string ip, string filename, string filetype, string tempdir, string targetdir)
        {
            string filepath      = "";
            string temp_filepath = "";

            try
            {
                filepath = Path.Combine("Record", filename);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Path Combine exception for filepath, filename {filename}, Exception Message: {e.Message}");
                BlackVueDownloaderCopyStats.Errored++;
                return;
            }

            try
            {
                temp_filepath = Path.Combine("_tmp", filename);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Path Combine exception for temp_filepath, filename {filename}, Exception Message: {e.Message}");
                BlackVueDownloaderCopyStats.Errored++;
                return;
            }

            if (_fileSystemHelper.Exists(filepath))
            {
                Console.WriteLine($"File exists {filepath}, ignoring");
                BlackVueDownloaderCopyStats.Ignored++;
            }
            else
            {
                try
                {
                    var url = $"http://{ip}/Record/{filename}";
                    Console.WriteLine($"Downloading {filetype} file: {url}");

                    var tempfile   = Path.Combine(tempdir, filename);
                    var targetfile = Path.Combine(targetdir, filename);

                    // If it already exists in the _tmp directory, delete it.
                    if (_fileSystemHelper.Exists(temp_filepath))
                    {
                        Console.WriteLine($"File exists in tmp {temp_filepath}, deleting");
                        BlackVueDownloaderCopyStats.TmpDeleted++;
                        _fileSystemHelper.Delete(temp_filepath);
                    }

                    // Copy to the temp directory, that way, if the file is partially downloaded,
                    // it won't leave a partial file in the target directory
                    url.DownloadFileAsync(tempdir).Wait();

                    // File downloaded. Move from temp to target.
                    _fileSystemHelper.Move(tempfile, targetfile);

                    Console.WriteLine($"Downloaded {filetype} file: {url}");
                    BlackVueDownloaderCopyStats.Copied++;
                }
                catch (FlurlHttpTimeoutException e)
                {
                    Console.WriteLine($"FlurlHttpTimeoutException: {e.Message}");
                    BlackVueDownloaderCopyStats.Errored++;
                }
                catch (FlurlHttpException e)
                {
                    if (e.Call.Response != null)
                    {
                        Console.WriteLine($"Failed with response code: {e.Call.Response.StatusCode}");
                    }
                    Console.Write($"Failed before getting a response: {e.Message}");
                    BlackVueDownloaderCopyStats.Errored++;
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Exception: {e.Message}");
                    BlackVueDownloaderCopyStats.Errored++;
                }
            }
        }