Пример #1
0
        public async Task <DownloadResponse> RemoteDownloadLatest(Action <DownloadInfo> callbackProgress = null)
        {
            var response = new DownloadResponse();

            if (!RemoteAuthenticate())
            {
                return(response);
            }

            bool success = await RemoteUpdateFiles();

            if (!success)
            {
                return(response);
            }

            // get last modifier build id
            var file = GetLatestBuild();

            if (file == null)
            {
                return(response);
            }

            var request = _driveService.Files.Get(file.Id);

            request.MediaDownloader.ChunkSize = _settingsProvider.Current.DownloadChunkSize;
            await using var streamMemory      = new MemoryStream();

            // progress callback
            if (callbackProgress != null)
            {
                request.MediaDownloader.ProgressChanged += progress =>
                {
                    var info = new DownloadInfo()
                    {
                        FileSize = (long)file.FileSize,
                        Progress = progress,
                    };
                    callbackProgress.Invoke(info);
                };
            }

            await request.MediaDownloader.DownloadAsync(file.DownloadUrl, streamMemory);

            // copy used part of the buffer
            var buffer = streamMemory.GetBuffer();
            var bytes  = new byte[streamMemory.Length];

            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] = buffer[i];
            }

            response.Success = true;
            response.Bytes   = bytes;
            response.File    = file;
            return(response);
        }
Пример #2
0
        public async Task <DownloadResponse> Download(Action <DownloadInfo> callbackProgress = null)
        {
            DownloadResponse response = new DownloadResponse();

            response = await RemoteDownloadLatest(callbackProgress);

            if (response.Success && response.Bytes.Length > 0)
            {
                var dir  = $"{_settingsProvider.Current.DirectoryDownload}/";
                var path = $"{dir}/{response.File.Title}";
                if (_fileService.TryCreateDirectory(dir))
                {
                    _fileService.TrySave(path, response.Bytes);
                    response.PathDownload = path;
                }
            }

            return(response);
        }