Beispiel #1
0
        public static StreamUploadClient GetUploadClient()
        {
            var binding = new BasicHttpBinding()
            {
                MessageEncoding = WSMessageEncoding.Mtom,
                TransferMode    = TransferMode.StreamedRequest,
            };

            if (_environment.CMSUrl.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
            {
                binding.Security = new BasicHttpSecurity
                {
                    Mode = BasicHttpSecurityMode.Transport
                };
            }
            StreamUploadClient uploadClient = new StreamUploadClient(binding,
                                                                     new EndpointAddress($"{_environment.CMSUrl}/webservices/CoreService{_version}.svc/streamUpload_basicHttp"));

            if (!string.IsNullOrEmpty(_environment.Username) && !string.IsNullOrEmpty(_environment.Password))
            {
                ((ClientBase <IStreamUpload>)uploadClient).ClientCredentials.Windows.ClientCredential.UserName = _environment.Username;
                ((ClientBase <IStreamUpload>)uploadClient).ClientCredentials.Windows.ClientCredential.Password = _environment.Password;
            }
            if (!string.IsNullOrEmpty(_environment.UserDomain))
            {
                ((ClientBase <IStreamUpload>)uploadClient).ClientCredentials.Windows.ClientCredential.Domain = string.IsNullOrEmpty(_environment.UserDomain) ? "." : _environment.UserDomain;
            }

            return(uploadClient);
        }
Beispiel #2
0
        /// <summary>
        /// Upload a file to Tridion from a binary array
        /// </summary>
        /// <param name="fileName">Tridion Filename</param>
        /// <param name="binaryContent">Binary Content as byte array</param>
        /// <returns>Full tridion file path</returns>
        public String UploadFile(String fileName, byte[] binaryContent)
        {
            StreamUploadClient client = null;

            try
            {
                client = UploadClient;
                return(client.UploadBinaryByteArray(fileName, binaryContent));
            }
            finally
            {
                if (client != null)
                {
                    if (client.State == CommunicationState.Faulted)
                    {
                        client.Abort();
                    }
                    else
                    {
                        client.Close();
                    }
                }
            }
        }
        public static StreamUploadClient GetStreamUploadClient(string host, string username, string password)
        {
            if (String.IsNullOrEmpty(host))
                host = "localhost";

            host = host.GetDomainNameAndPort();

            var binding = GetHttpBinding();

            var endpoint = new EndpointAddress(String.Format("http://{0}/webservices/CoreService{1}.svc/streamUpload_basicHttp", host, ClientVersion));

            StreamUploadClient client = new StreamUploadClient(binding, endpoint);

            return client;
        }
 private static void EnsureValidStreamUploadClient(MappingInfo mapping)
 {
     if (StreamUploadClient == null || StreamUploadClient.InnerChannel.State == CommunicationState.Faulted)
     {
         StreamUploadClient = GetStreamUploadClient(mapping);
     }
 }
 public static void ResetUploadClient()
 {
     StreamUploadClient = null;
 }
Beispiel #6
0
        private string ImportSingleItem(IEclUri eclUri)
        {
            string id = "tcm:0-0-0";
            IContentLibraryMultimediaItem eclItem = (IContentLibraryMultimediaItem)_eclContentLibraryContext.GetItem(eclUri);
            string       extension = eclItem.Filename.Substring(eclItem.Filename.LastIndexOf('.') + 1);
            MemoryStream ms        = null;
            string       tempPath;

            try
            {
                // create some template attributes
                IList <ITemplateAttribute> attributes = CreateTemplateAttributes(eclItem);

                // determine if item has content or is available online
                string publishedPath = eclItem.GetDirectLinkToPublished(attributes);
                if (string.IsNullOrEmpty(publishedPath))
                {
                    // we can directly get the content
                    IContentResult content = eclItem.GetContent(attributes);
                    ms = new MemoryStream();
                    content.Stream.CopyTo(ms);
                    ms.Position = 0;
                }
                else
                {
                    // read the content from the publish path
                    using (WebClient webClient = new WebClient())
                    {
                        byte[] thumbnailData = webClient.DownloadData(publishedPath);
                        ms = new MemoryStream(thumbnailData, false);
                    }
                }

                // upload binary (using netTcp binding as configured in SDL Tridion, because this Model extension is running inside the UI)
                using (StreamUploadClient suClient = new StreamUploadClient("streamUpload_netTcp_2012"))
                {
                    tempPath = suClient.UploadBinaryContent(eclItem.Filename, ms);
                }
            }
            finally
            {
                if (ms != null)
                {
                    ms.Dispose();
                }
            }

            // create tcm item
            var mmComponent = new ComponentData
            {
                Id     = id,
                Title  = eclItem.Title,
                Schema = new LinkToSchemaData {
                    IdRef = _schemaUri
                },
                LocationInfo = new LocationInfo {
                    OrganizationalItem = new LinkToOrganizationalItemData {
                        IdRef = _folderUri
                    }
                }
            };

            // put binary data in tcm item (using netTcp binding as configured in SDL Tridion, because this Model extension is running inside the UI)
            using (SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2012"))
            {
                // impersonate with current user
                client.Impersonate(_username);

                // set metadata
                var schemaFields = client.ReadSchemaFields(_schemaUri, true, new ReadOptions());
                if (schemaFields.MetadataFields.Any())
                {
                    var fields = Fields.ForMetadataOf(schemaFields, mmComponent);
                    if (!string.IsNullOrEmpty(eclItem.MetadataXml))
                    {
                        XNamespace ns       = GetNamespace(eclItem.MetadataXml);
                        XDocument  metadata = XDocument.Parse(eclItem.MetadataXml);
                        var        children = metadata.Element(ns + "Metadata").Descendants();
                        for (int i = 0; i < children.Count(); i++)
                        {
                            fields.AddFieldElement(new ItemFieldDefinitionData {
                                Name = "data"
                            });
                            var embeddedFields = fields["data"].GetSubFields(i);
                            embeddedFields.AddFieldElement(new ItemFieldDefinitionData {
                                Name = "key"
                            });
                            embeddedFields.AddFieldElement(new ItemFieldDefinitionData {
                                Name = "value"
                            });
                            embeddedFields["key"].Value   = children.ElementAt(i).Name.LocalName;
                            embeddedFields["value"].Value = children.ElementAt(i).Value;
                        }
                    }
                    mmComponent.Metadata = fields.ToString();
                }

                // find multimedia type
                var list           = client.GetSystemWideList(new MultimediaTypesFilterData());
                var multimediaType = list.OfType <MultimediaTypeData>().Single(mt => mt.FileExtensions.Contains(extension));

                // set BinaryContent of a component
                mmComponent.BinaryContent = new BinaryContentData
                {
                    UploadFromFile = tempPath,
                    Filename       = eclItem.Filename,
                    MultimediaType = new LinkToMultimediaTypeData {
                        IdRef = multimediaType.Id
                    }
                };

                // create (and save) component
                ComponentData data = (ComponentData)client.Create(mmComponent, new ReadOptions());
                id = data.Id;
            }

            //string result = string.Format("created {0}, from {1}, in {2}, using {3}, for {4}", id, eclUri, _folderUri, _schemaUri, _username);
            return(id);
        }
        private string ImportSingleItem(IEclUri eclUri)
        {
            string id = "tcm:0-0-0";
            IContentLibraryMultimediaItem eclItem = (IContentLibraryMultimediaItem)_eclContentLibraryContext.GetItem(eclUri);
            string extension = eclItem.Filename.Substring(eclItem.Filename.LastIndexOf('.') + 1);
            MemoryStream ms = null;
            string tempPath;
            try
            {
                // create some template attributes
                IList<ITemplateAttribute> attributes = CreateTemplateAttributes(eclItem);

                // determine if item has content or is available online
                string publishedPath = eclItem.GetDirectLinkToPublished(attributes);
                if (string.IsNullOrEmpty(publishedPath))
                {
                    // we can directly get the content 
                    IContentResult content = eclItem.GetContent(attributes);
                    ms = new MemoryStream();
                    content.Stream.CopyTo(ms);
                    ms.Position = 0;
                }
                else
                {
                    // read the content from the publish path
                    using (WebClient webClient = new WebClient())
                    {
                        byte[] thumbnailData = webClient.DownloadData(publishedPath);
                        ms = new MemoryStream(thumbnailData, false);
                    }
                }

                // upload binary (using netTcp binding as configured in SDL Tridion, because this Model extension is running inside the UI) 
                using (StreamUploadClient suClient = new StreamUploadClient("streamUpload_netTcp_2012"))
                {
                    tempPath = suClient.UploadBinaryContent(eclItem.Filename, ms);
                }
            }
            finally
            {
                if (ms != null)
                {
                    ms.Dispose();
                }
            }

            // create tcm item
            var mmComponent = new ComponentData
            {
                Id = id,
                Title = eclItem.Title,
                Schema = new LinkToSchemaData { IdRef = _schemaUri },
                LocationInfo = new LocationInfo { OrganizationalItem = new LinkToOrganizationalItemData { IdRef = _folderUri } }
            };

            // put binary data in tcm item (using netTcp binding as configured in SDL Tridion, because this Model extension is running inside the UI) 
            using (SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2012"))
            {
                // impersonate with current user
                client.Impersonate(_username);

                // set metadata
                var schemaFields = client.ReadSchemaFields(_schemaUri, true, new ReadOptions());
                if (schemaFields.MetadataFields.Any())
                {
                    var fields = Fields.ForMetadataOf(schemaFields, mmComponent);
                    if (!string.IsNullOrEmpty(eclItem.MetadataXml))
                    {
                        XNamespace ns = GetNamespace(eclItem.MetadataXml);
                        XDocument metadata = XDocument.Parse(eclItem.MetadataXml);
                        var children = metadata.Element(ns + "Metadata").Descendants();
                        for (int i = 0; i < children.Count(); i++)
                        {
                            fields.AddFieldElement(new ItemFieldDefinitionData { Name = "data" });
                            var embeddedFields = fields["data"].GetSubFields(i);
                            embeddedFields.AddFieldElement(new ItemFieldDefinitionData { Name = "key" });
                            embeddedFields.AddFieldElement(new ItemFieldDefinitionData { Name = "value" });
                            embeddedFields["key"].Value = children.ElementAt(i).Name.LocalName;
                            embeddedFields["value"].Value = children.ElementAt(i).Value;
                        }
                    }
                    mmComponent.Metadata = fields.ToString();
                }

                // find multimedia type
                var list = client.GetSystemWideList(new MultimediaTypesFilterData());
                var multimediaType = list.OfType<MultimediaTypeData>().Single(mt => mt.FileExtensions.Contains(extension));

                // set BinaryContent of a component
                mmComponent.BinaryContent = new BinaryContentData
                {
                    UploadFromFile = tempPath,
                    Filename = eclItem.Filename,
                    MultimediaType = new LinkToMultimediaTypeData { IdRef = multimediaType.Id }
                };

                // create (and save) component
                ComponentData data = (ComponentData)client.Create(mmComponent, new ReadOptions());
                id = data.Id;
            }

            //string result = string.Format("created {0}, from {1}, in {2}, using {3}, for {4}", id, eclUri, _folderUri, _schemaUri, _username);
            return id;
        }