public void FileConnectorGetFileBytes1Test()
        {
            FileSystemConnector fileSystemConnector = new FileSystemConnector(@".\Resources", "");

            using (var bytes = fileSystemConnector.GetFileStream("office365.png"))
            {
                Assert.IsTrue(bytes.Length > 0);
            }

            using (var bytes2 = fileSystemConnector.GetFileStream("Idonotexist.xml"))
            {
                Assert.IsNull(bytes2);
            }
        }
        internal static ProvisioningTemplate LoadProvisioningTemplateFromFile(string templatePath, ITemplateProviderExtension[] templateProviderExtensions)
        {
            // Prepare the File Connector
            string templateFileName = System.IO.Path.GetFileName(templatePath);

            // Prepare the template path
            var fileInfo = new FileInfo(templatePath);
            FileConnectorBase fileConnector = new FileSystemConnector(fileInfo.DirectoryName, "");

            // Load the provisioning template file
            Stream stream           = fileConnector.GetFileStream(templateFileName);
            var    isOpenOfficeFile = ApplyProvisioningTemplate.IsOpenOfficeFile(stream);

            XMLTemplateProvider provider;

            if (isOpenOfficeFile)
            {
                provider         = new XMLOpenXMLTemplateProvider(new OpenXMLConnector(templateFileName, fileConnector));
                templateFileName = templateFileName.Substring(0, templateFileName.LastIndexOf(".", StringComparison.Ordinal)) + ".xml";
            }
            else
            {
                provider = new XMLFileSystemTemplateProvider(fileConnector.Parameters[FileConnectorBase.CONNECTIONSTRING] + "", "");
            }

            ProvisioningTemplate provisioningTemplate = provider.GetTemplate(templateFileName, templateProviderExtensions);

            provisioningTemplate.Connector = provider.Connector;

            // Return the result
            return(provisioningTemplate);
        }
        public void FileConnectorSaveStream3Test()
        {
            FileSystemConnector fileSystemConnector = new FileSystemConnector(@".", "wrong");

            using (var fileStream = System.IO.File.OpenRead(@".\resources\custombg.jpg"))
            {
                fileSystemConnector.SaveFileStream("blabla.png", @"Resources\Templates", fileStream);
            }

            long byteCount = 0;

            using (var fileStream = System.IO.File.OpenRead(@".\resources\office365.png"))
            {
                byteCount = fileStream.Length;
                fileSystemConnector.SaveFileStream("blabla.png", @"Resources\Templates", fileStream);
            }

            //read the file
            using (var bytes = fileSystemConnector.GetFileStream("blabla.png", @"Resources\Templates"))
            {
                Assert.IsTrue(byteCount == bytes.Length);
            }

            // file will be deleted at end of test
        }
Beispiel #4
0
        internal static ProvisioningTemplate LoadProvisioningTemplateFromFile(string templatePath, ITemplateProviderExtension[] templateProviderExtensions, Action <Exception> exceptionHandler)
        {
            // Prepare the File Connector
            string templateFileName = System.IO.Path.GetFileName(templatePath);

            // Prepare the template path
            var fileInfo = new FileInfo(templatePath);
            FileConnectorBase fileConnector = new FileSystemConnector(fileInfo.DirectoryName, "");

            // Load the provisioning template file
            Stream stream = fileConnector.GetFileStream(templateFileName);

            if (stream == null)
            {
                throw new FileNotFoundException($"File {templatePath} does not exist.", templatePath);
            }

            var isOpenOfficeFile = FileUtilities.IsOpenOfficeFile(stream);

            XMLTemplateProvider provider;

            if (isOpenOfficeFile)
            {
                var openXmlConnector = new OpenXMLConnector(templateFileName, fileConnector);
                provider = new XMLOpenXMLTemplateProvider(openXmlConnector);
                if (!String.IsNullOrEmpty(openXmlConnector.Info?.Properties?.TemplateFileName))
                {
                    templateFileName = openXmlConnector.Info.Properties.TemplateFileName;
                }
                else
                {
                    templateFileName = templateFileName.Substring(0, templateFileName.LastIndexOf(".", StringComparison.Ordinal)) + ".xml";
                }
            }
            else
            {
                provider = new XMLFileSystemTemplateProvider(fileConnector.Parameters[FileConnectorBase.CONNECTIONSTRING] + "", "");
            }
            try
            {
                ProvisioningTemplate provisioningTemplate = provider.GetTemplate(templateFileName, templateProviderExtensions);
                provisioningTemplate.Connector = provider.Connector;
                return(provisioningTemplate);
            }
            catch (ApplicationException ex)
            {
                if (ex.InnerException is AggregateException)
                {
                    if (exceptionHandler != null)
                    {
                        foreach (var exception in ((AggregateException)ex.InnerException).InnerExceptions)
                        {
                            exceptionHandler(exception);
                        }
                    }
                }
            }
            return(null);
        }
Beispiel #5
0
        internal static ProvisioningHierarchy LoadProvisioningHierarchyFromFile(string templatePath, ITemplateProviderExtension[] templateProviderExtensions)
        {
            // Prepare the File Connector
            string templateFileName = System.IO.Path.GetFileName(templatePath);

            // Prepare the template path
            var fileInfo = new FileInfo(templatePath);
            FileConnectorBase fileConnector = new FileSystemConnector(fileInfo.DirectoryName, "");

            // Load the provisioning template file
            var isOpenOfficeFile = false;

            using (var stream = fileConnector.GetFileStream(templateFileName))
            {
                isOpenOfficeFile = FileUtilities.IsOpenOfficeFile(stream);
            }

            XMLTemplateProvider provider;

            if (isOpenOfficeFile)
            {
                var openXmlConnector = new OpenXMLConnector(templateFileName, fileConnector);
                provider = new XMLOpenXMLTemplateProvider(openXmlConnector);
                if (!String.IsNullOrEmpty(openXmlConnector.Info?.Properties?.TemplateFileName))
                {
                    templateFileName = openXmlConnector.Info.Properties.TemplateFileName;
                }
                else
                {
                    templateFileName = templateFileName.Substring(0, templateFileName.LastIndexOf(".", StringComparison.Ordinal)) + ".xml";
                }

                var hierarchy = (provider as XMLOpenXMLTemplateProvider).GetHierarchy();
                if (hierarchy != null)
                {
                    hierarchy.Connector = provider.Connector;
                    return(hierarchy);
                }
            }
            else
            {
                provider = new XMLFileSystemTemplateProvider(fileConnector.Parameters[FileConnectorBase.CONNECTIONSTRING] + "", "");
            }

            ProvisioningHierarchy provisioningHierarchy = provider.GetHierarchy(templateFileName);

            provisioningHierarchy.Connector = provider.Connector;
            // Return the result
            return(provisioningHierarchy);
        }
        public void FileConnectorDelete2Test()
        {
            // upload the file
            FileSystemConnector fileSystemConnector = new FileSystemConnector(@".", "wrong");

            using (var fileStream = System.IO.File.OpenRead(@".\resources\office365.png"))
            {
                fileSystemConnector.SaveFileStream("blabla.png", @"Resources\Templates", fileStream);
            }

            // delete the file
            fileSystemConnector.DeleteFile("blabla.png", @"Resources\Templates");

            //read the file
            using (var bytes = fileSystemConnector.GetFileStream("blabla.png", @"Resources\Templates"))
            {
                Assert.IsNull(bytes);
            }

            // file will be deleted at end of test
        }
        public void FileConnectorForwardslashSupportTest()
        {
            // Path with forwardslash separator
            var filename = "separator.png";
            var containerWithForwardSlash = @"Resources/Templates/newfolder";

            // Constructor replaces folder delimiter
            FileSystemConnector fileSystemConnector = new FileSystemConnector(".", containerWithForwardSlash);

            Assert.AreEqual(@"Resources\Templates\newfolder", fileSystemConnector.GetContainer());

            // Save a file
            long byteCount = 0;

            using (var fileStream = System.IO.File.OpenRead(@".\resources\office365.png"))
            {
                byteCount = fileStream.Length;
                fileSystemConnector.SaveFileStream(filename, containerWithForwardSlash, fileStream);
            }

            // List files
            var files = fileSystemConnector.GetFiles(containerWithForwardSlash);

            Assert.IsTrue(files.Contains(filename));

            // Read the file
            using (var bytes = fileSystemConnector.GetFileStream(filename, containerWithForwardSlash))
            {
                Assert.IsTrue(byteCount == bytes.Length);
            }

            // Delete the file
            fileSystemConnector.DeleteFile(filename, containerWithForwardSlash);

            // Folder will be deleted in cleanup
        }
Beispiel #8
0
        private static ProvisioningHierarchy LoadProvisioningHierarchyFromFile(string templatePath, Action <Exception> exceptionHandler)
        {
            // Prepare the File Connector
            string templateFileName = System.IO.Path.GetFileName(templatePath);

            // Prepare the template path
            var fileInfo = new FileInfo(templatePath);
            FileConnectorBase fileConnector = new FileSystemConnector(fileInfo.DirectoryName, "");

            // Load the provisioning template file
            var isOpenOfficeFile = false;

            using (var stream = fileConnector.GetFileStream(templateFileName))
            {
                isOpenOfficeFile = FileUtilities.IsOpenOfficeFile(stream);
            }

            XMLTemplateProvider provider;

            if (isOpenOfficeFile)
            {
                var openXmlConnector = new OpenXMLConnector(templateFileName, fileConnector);
                provider = new XMLOpenXMLTemplateProvider(openXmlConnector);
                if (!String.IsNullOrEmpty(openXmlConnector.Info?.Properties?.TemplateFileName))
                {
                    templateFileName = openXmlConnector.Info.Properties.TemplateFileName;
                }
                else
                {
                    templateFileName = templateFileName.Substring(0, templateFileName.LastIndexOf(".", StringComparison.Ordinal)) + ".xml";
                }

                var hierarchy = (provider as XMLOpenXMLTemplateProvider).GetHierarchy();
                if (hierarchy != null)
                {
                    hierarchy.Connector = provider.Connector;
                    return(hierarchy);
                }
            }
            else
            {
                provider = new XMLFileSystemTemplateProvider(fileConnector.Parameters[FileConnectorBase.CONNECTIONSTRING] + "", "");
            }

            try
            {
                ProvisioningHierarchy provisioningHierarchy = provider.GetHierarchy(templateFileName);
                provisioningHierarchy.Connector = provider.Connector;
                return(provisioningHierarchy);
            }
            catch (ApplicationException ex)
            {
                if (ex.InnerException is AggregateException)
                {
                    if (exceptionHandler != null)
                    {
                        foreach (var exception in ((AggregateException)ex.InnerException).InnerExceptions)
                        {
                            exceptionHandler(exception);
                        }
                    }
                }
            }
            return(null);
        }