Ejemplo n.º 1
0
 private void AddCommonModelFields(MultipartFormDataContent form, UploadModelRequest request)
 {
     form.Add(new StringContent(ShortenString(request.Name, 48)), "name");
     form.Add(new StringContent(ShortenString(request.Description, 1024)), "description");
     form.Add(new StringContent(request.IsPrivate.To_0_1()), "private");
     form.Add(new StringContent(request.IsInspectable.To_0_1()), "isInspectable");
     form.Add(new StringContent(request.IsPublished.To_0_1()), "isPublished");
     form.Add(new StringContent(request.License), "license");
     var optionsJson = JsonConvert.SerializeObject(request.Options);
     form.Add(new StringContent(JsonConvert.SerializeObject(request.Options)), "options");
     form.AddRange(request.Tags, "tags");
     form.AddRange(request.Categories, "categories");
 }
        public async Task <SketchfabUploadResponse> UploadModelAsync(UploadModelRequest request, string sketchFabToken)
        {
            SketchfabUploadResponse sfResponse = new SketchfabUploadResponse();

            try
            {
                _logger.LogInformation($"Uploading model [{request.FilePath}].");
                if (string.IsNullOrWhiteSpace(request.FilePath))
                {
                    throw new ArgumentNullException(nameof(request.FilePath));
                }

                if (!File.Exists(request.FilePath))
                {
                    throw new FileNotFoundException($"File [{request.FilePath}] not found.");
                }
                HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, $"{SketchfabApiUrl}/models");
                httpRequestMessage.AddAuthorizationHeader(sketchFabToken, request.TokenType);
                using var form                  = new MultipartFormDataContent();
                using var fileContent           = new ByteArrayContent(await File.ReadAllBytesAsync(request.FilePath));
                fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
                form.Add(fileContent, "modelFile", Path.GetFileName(request.FilePath));
                if (!string.IsNullOrWhiteSpace(request.Source))
                {
                    form.Add(new StringContent(request.Source), "source");
                }
                else
                {
                    _logger.LogWarning("Sketchfab upload has no source configured. It's better to set one to uniquely identify all the models generated by the exporter, see https://Sketchfab.com/developers/guidelines#source");
                }

                AddCommonModelFields(form, request);

                httpRequestMessage.Content = form;


                var httpClient = _httpClientFactory.CreateClient();
                var response   = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead);

                _logger.LogInformation($"{nameof(UploadModelAsync)} responded {response.StatusCode}");

                if (response.IsSuccessStatusCode)
                {
                    var uuid = response.Headers.GetValues("Location").FirstOrDefault();
                    sfResponse.ModelId    = uuid;
                    sfResponse.StatusCode = response.StatusCode;
                    sfResponse.Message    = response.ReasonPhrase;
                    request.ModelId       = uuid;
                    _logger.LogInformation("Uploading is complete. Model uuid is " + uuid);
                }
                else
                {
                    _logger.LogError($"Error in Sketchfab upload: {response.StatusCode} {response.ReasonPhrase}");
                    sfResponse.StatusCode = response.StatusCode;
                    sfResponse.Message    = response.ReasonPhrase;
                }

                return(sfResponse);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Sketchfab upload error: {ex.Message}");
                throw;
            }
        }
 private string GetAuthPrefix(UploadModelRequest request)
 {
     return(request.TokenType.ToString());
 }