コード例 #1
0
        /// <inheritdoc />
        public async Task <Skylink> UploadFile(UploadItem item, UploadOptions options = default)
        {
            if (item is null)
            {
                throw new ArgumentNullException(nameof(item));
            }
            options ??= UploadOptions._default;

            using var multiPartContent = new MultipartFormDataContent { CreateFileContent("file", item) };

            var response = await _httpClient.PostAsync($"/skynet/skyfile{options.ToQueryString()}", multiPartContent).ConfigureAwait(false);

            response.EnsureSuccessStatusCode();

            var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

            var uploadResponse = await JsonSerializer.DeserializeAsync <UploadResponse>(contentStream, _jsonSerializerOptions).ConfigureAwait(false);

            return(uploadResponse.ParseAndValidate());
        }
コード例 #2
0
 /// <inheritdoc />
 public Task <Skylink> UploadFile(IFileInfo file, UploadOptions options = default) => UploadFile(new UploadItem(file), options);
コード例 #3
0
        /// <inheritdoc />
        public Task <Skylink> UploadFile(IFileProvider fileProvider, string filePath, UploadOptions options = default)
        {
            if (fileProvider is null)
            {
                throw new ArgumentNullException(nameof(fileProvider));
            }
            if (filePath is null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }
            if (fileProvider is NullFileProvider)
            {
                throw new ArgumentException("Cannot access files from NullFileProvider", nameof(fileProvider));
            }
            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentException("Path cannot be empty", nameof(filePath));
            }

            var fileInfo = fileProvider.GetFileInfo(filePath);

            if (!fileInfo.Exists)
            {
                throw new FileNotFoundException("Cannot find file at specified path", filePath);
            }

            return(UploadFile(new UploadItem(fileInfo), options));
        }