Beispiel #1
0
        /// <summary>
        /// Create an item in AltspaceVR.
        /// </summary>
        /// <param name="item_singular">'space_template' or 'kit'</param>
        /// <param name="name">Name of item to create</param>
        /// <param name="description">The description</param>
        /// <param name="imageFileName">(Optional) Local file name of the image</param>
        /// <param name="tag_list">(templates only) comma separated tag list</param>
        ///
        /// <returns>The ID of the generated item</returns>
        public static string CreateAltVRItem(string item_singular, string name, string description, string imageFileName, string tag_list = null)
        {
            string item_fn = null;

            if (item_singular == "space_template")
            {
                item_fn = "template";
            }
            else if (item_singular == "kit")
            {
                item_fn = "kit";
            }
            else
            {
                throw new InvalidDataException(item_singular + ": Not a recognized Altspace item");
            }

            string item_plural         = item_singular + "s";
            string commit_btn_playload = "Create " + item_fn.Substring(0, 1).ToUpper() + item_fn.Substring(1);

            string id_result = null;
            string authtoken;

            string auth_token_pattern  = "type=\"hidden\" name=\"authenticity_token\" value=\"";
            string template_id_pattern = "data-method=\"delete\" href=\"/" + item_plural + "/";

            EditorUtility.DisplayProgressBar("Creating " + item_fn, "Retrieving landing page...", 0.0f);

            try
            {
                HttpResponseMessage result = LoginManager.GetHttpClient().GetAsync(item_plural + "/new").Result;
                string content             = result.Content.ReadAsStringAsync().Result;
                result.EnsureSuccessStatusCode();

                authtoken = Common.GetWebParameter(content, auth_token_pattern);
            }
            catch (Exception e)
            {
                Debug.LogWarning(e.Message);
                EditorUtility.ClearProgressBar();
                return(null);
            }

            EditorUtility.DisplayProgressBar("Creating " + item_fn + "", "Posting new " + item_fn + "...", 0.5f);

            try
            {
                MultipartFormDataContent form             = new MultipartFormDataContent();
                ByteArrayContent         imageFileContent = null;

                // Web server is not completely standards compliant and requires the form-data headers in the form
                // name="itemname", rather than also accepting name=itemname.
                // .NET HttpClient only uses quotes when necessary. Bummer.
                form.Add(new StringContent("✓"), "\"utf8\"");
                form.Add(new StringContent(authtoken), "\"authenticity_token\"");
                form.Add(new StringContent(name), item_singular + "[name]");
                form.Add(new StringContent(description), item_singular + "[description]");

                if (!String.IsNullOrEmpty(imageFileName))
                {
                    imageFileContent = new ByteArrayContent(File.ReadAllBytes(imageFileName));
                    form.Add(imageFileContent, item_singular + "[image]", Path.GetFileName(imageFileName));
                }
                else
                {
                    imageFileContent = new ByteArrayContent(new byte[0]);
                    form.Add(imageFileContent, item_singular + "[image]");
                }

                if (tag_list != null)
                {
                    form.Add(new StringContent(tag_list), item_singular + "[tag_list]");
                }

                form.Add(new StringContent(commit_btn_playload), "\"commit\"");

                HttpResponseMessage result = LoginManager.GetHttpClient().PostAsync(item_plural, form).Result;
                string content             = result.Content.ReadAsStringAsync().Result;
                result.EnsureSuccessStatusCode();

                id_result = Common.GetWebParameter(content, template_id_pattern);
            }
            catch (HttpRequestException)
            {
                Debug.LogError("HTTP Request error");
            }
            catch (FileNotFoundException)
            {
                Debug.LogError("Image file not found");
            }
            finally
            {
                EditorUtility.ClearProgressBar();
            }

            return(id_result);
        }