Esempio n. 1
0
        private void CheckForLocalFile(GFile gFile, SyncResultsBuilder results)
        {
            var fullPath = GetFileFolderPath(gFile);

            _logger.LogInformation($"New File \"{fullPath}\"");

            var localPath    = Path.Combine(Config.DestinationPath, fullPath);
            var downloadFile = new DownloadableFile(gFile, localPath);

            try
            {
                if (LFile.Exists(localPath))
                {
                    _logger.LogInformation("   File exists");
                    if (Config.ForceDownloads)
                    {
                        _logger.LogInformation($"   Force downloads enabled, will download to: {localPath}");
                        _filesToDownload.Add(downloadFile);
                        return;
                    }

                    var lFileInfo = new FileInfo(localPath);
                    if (lFileInfo.LastWriteTimeUtc <= (gFile.ModifiedTime ?? gFile.CreatedTime))
                    {
                        _logger.LogInformation($"   Google file is newer, will download to: {localPath}");
                        _filesToDownload.Add(downloadFile);
                    }
                }
                else
                {
                    // File doesn't exist, download it
                    _logger.LogInformation($"   File does not exist, downloading to: {localPath}");
                    _filesToDownload.Add(downloadFile);
                }
            }
            catch (Exception exception)
            {
                var error = downloadFile.ToErroredFile(exception);
                results.ErroredFiles.Add(error);
            }
        }
Esempio n. 2
0
        private void DownloadFile(DownloadableFile download, SyncResultsBuilder results)
        {
            _logger.LogInformation($"[dl: {download.GFile.Name}] download starting at {download.Destination}");
            var sanitizedDestination = SanatizePath(download.Destination);

            if (sanitizedDestination != download.Destination)
            {
                _logger.LogError($"[dl: {download.GFile.Name}] Warning: Invalid Destination path.  Will use \"{sanitizedDestination}\" instead");
            }
            // Create the directorys (if they dont exist) to avoid exception when making file
            var folderPath = Path.GetDirectoryName(sanitizedDestination);

            Directory.CreateDirectory(folderPath);
            // Create the file in a temp location to avoid overwriting the file and corrupting if the download is broken
            var tempFile = sanitizedDestination + ".temp";

            try
            {
                using (var stream = new FileStream(tempFile, FileMode.Create))
                {
                    var request = _service.Files.Get(download.GFile.Id);
                    // Add a handler which will be notified on progress changes.
                    // It will notify on each chunk download and when the
                    // download is completed or failed.
                    request.MediaDownloader.ProgressChanged +=
                        (IDownloadProgress progress) =>
                    {
                        switch (progress.Status)
                        {
                        case DownloadStatus.Downloading:
                        {
                            string percent;
                            if (download.GFile.Size.HasValue)
                            {
                                percent = $"{((100 * progress.BytesDownloaded) / download.GFile.Size.Value)}%";
                            }
                            else
                            {
                                percent = "...";
                            }

                            _logger.LogInformation($"[dl: {download.GFile.Name}] {percent}");
                            break;
                        }

                        case DownloadStatus.Completed:
                        {
                            _logger.LogInformation($"[dl: {download.GFile.Name}] Download complete.");
                            results.SuccessfulDownloads.Add(download);
                            break;
                        }

                        case DownloadStatus.Failed:
                        {
                            _logger.LogError($"[dl: {download.GFile.Name}] Download failed.  Exception: {progress.Exception.Message}");
                            results.ErroredFiles.Add(download.ToErroredFile(progress.Exception));
                            break;
                        }
                        }
                    };
                    request.DownloadWithStatus(stream);
                    stream.Dispose();
                    // Finished downloading, delete the original and rename the temp to the original
                    LFile.Delete(sanitizedDestination);
                    LFile.Move(tempFile, sanitizedDestination);
                }
            }
            catch (Exception exception)
            {
                _logger.LogError($"[dl: {download?.GFile?.Name}] Download failed.  Exception: {exception.Message}");
                _logger.LogError(exception.Message);
                if (download?.GFile != null)
                {
                    var errorFile = download.ToErroredFile(exception);
                    results.ErroredFiles.Add(errorFile);
                }
            }
        }
Esempio n. 3
0
 public static ErroredFile FromDownloadableFile(DownloadableFile download, Exception exception)
 {
     return(new ErroredFile(download, exception));
 }
Esempio n. 4
0
 public ErroredFile(DownloadableFile file, Exception exception)
 {
     File      = file;
     Exception = exception;
 }