Пример #1
0
        private async Task ProcessFileAsync(HttpClient client, Uri uri)
        {
            _logger.LogInfo($"Found file: {uri}");

            if (!IsValidUri(uri, ConstraintType.File))
            {
                return;
            }

            HttpResponseMessage response;

            try
            {
                response = await client.GetAsync(uri);
            }
            catch (Exception e)
            {
                _logger.LogError($"Not loaded file: {uri}. Error message: {e.Message}");
                return;
            }

            if (!response.IsSuccessStatusCode)
            {
                _logger.LogError($"Not loaded file: {uri}. Status code: {response.StatusCode}");
                return;
            }

            _logger.LogInfo($"Loaded file: {uri}");

            var fileStream = await response.Content.ReadAsStreamAsync();

            await _saver.SaveFileAsync(uri, fileStream);
        }
Пример #2
0
        /// <summary>
        /// Process file at the current uri
        /// </summary>
        /// <param name="httpClient">Http Client</param>
        /// <param name="fileUri">File uri</param>
        /// <returns>Task</returns>
        private async Task ProcessFileAsync(HttpClient httpClient, Uri fileUri)
        {
            if (!_transactionConstraints.IsAcceptableUrl(fileUri, _primaryUri) || !_fileExtensionConstraints.IsAcceptableFileType(fileUri))
            {
                return;
            }

            try
            {
                HttpResponseMessage response = await httpClient.GetAsync(fileUri, _token);

                await _contentSaver.SaveFileAsync(fileUri, response.Content.ReadAsStreamAsync().Result);
            }
            catch (TaskCanceledException ex)
            {
                throw new TaskCanceledException(ex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }