Пример #1
0
        public List <WCFTemplate> ImportTemplates(Stream file)
        {
            List <WCFTemplate> returnTemplateList = new List <WCFTemplate>();

            try
            {
                HttpMultipartParser parser = new HttpMultipartParser(file, "file");

                if (parser.Success)
                {
                    string authenticationCookie = parser.Parameters["auth"];

                    string userName = AuthHandler.Authorize(authenticationCookie, PermissionsTable.Instance.CanImportTemplates);

                    if (parser.FileContents.Length > 0)
                    {
                        string jsonString = ParseTools.ToString(parser.FileContents);
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List <WCFTemplate>));

                        using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))
                        {
                            List <WCFTemplate> templates = serializer.ReadObject(ms) as List <WCFTemplate>;

                            if (templates != null)
                            {
                                foreach (WCFTemplate template in templates)
                                {
                                    UpdateTemplateIds(template);
                                    _addin.UpdateTemplate(userName, template, string.Empty);
                                }
                            }
                            return(templates);
                        }
                    }
                }
            }
            catch (ServiceAuthorizationException)
            {
                returnTemplateList = GetImportTemplatesError("Access denied, please login with different user to have this feature available.");
                // throw new ServiceAuthorizationException("Access denied, please login with different user to have this feature available.");
            }
            catch (Exception e)
            {
                returnTemplateList = GetImportTemplatesError(e.Message);
            }

            return(returnTemplateList);
        }