Ejemplo n.º 1
0
        /// <inheritdoc/>
        public async Task DownloadFileAsync(string url, string path)
        {
            if (!_appDataFileProvider.IsWriteEnabled)
            {
                return;
            }

            using var response = await GetS3Object(url);

            using var targetStream = _appDataFileProvider.OpenFile(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);

            await response.ResponseStream.CopyToAsync(targetStream);
        }
        /// <inheritdoc />
        public bool TryDequeue(out T item)
        {
            lock (_fileLock)
            {
                if (CountInternal > 0)
                {
                    var filepath = GetFilePath(Head);
                    try
                    {
                        // Open the file using the "DeleteOnClose" flag so that it is deleted after being read.
                        // This eliminates the need to delete the file in a separate operation.
                        using var fs = _fileProvider.OpenFile(filepath, FileMode.Open, FileAccess.Read, FileShare.None, 4096, FileOptions.DeleteOnClose);
                        item         = _serializer.Deserialize(fs);

                        _logger?.LogTrace("[{0}] Successfully dequeued item from persistent queue. {1} items left in queue.", nameof(FilePersistentQueue <T> .TryDequeue), CountInternal);
                        return(true);
                    }
                    catch (Exception ex)
                    {
                        // If this fails, we don't want it to get stuck.
                        // We'll log the error, with the bad file name, and increment the head counter in the finally.
                        _logger?.LogError("Error dequeuing object in file {0}: {1}", filepath, ex.ToMinimized());
                    }
                    finally
                    {
                        Head++;
                        UpdateIndex();
                    }
                }

                _logger?.LogDebug("[{0}] No records left in persistent queue.", nameof(FilePersistentQueue <T> .TryDequeue));

                item = default;
                return(false);
            }
        }
        /// <inheritdoc/>
        public async Task DownloadFileAsync(string url, string toPath)
        {
            if (!_appDataFileProvider.IsWriteEnabled)
            {
                return;
            }

            string path = ConvertFileUrlToPath(url);

            using (var streamFrom = File.OpenRead(path))
                using (var streamTo = _appDataFileProvider.OpenFile(toPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
                {
                    await streamFrom.CopyToAsync(streamTo);
                }
        }
        /// <inheritdoc/>
        public async Task DownloadFileAsync(string url, string toPath)
        {
            if (!_appDataFileProvider.IsWriteEnabled)
            {
                return;
            }

            using (HttpClient httpClient = new HttpClient())
                using (var response = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead))
                {
                    response.EnsureSuccessStatusCode();
                    using (var streamFrom = await response.Content.ReadAsStreamAsync())
                        using (var streamTo = _appDataFileProvider.OpenFile(toPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
                        {
                            await streamFrom.CopyToAsync(streamTo);
                        }
                }
        }