Example #1
0
        private bool CopyFilesWithFilter(SyncRule rule, string filter)
        {
            try
            {
                _directory.CreateDirectory(rule.Dest);
            }
            catch (Exception ex)
            {
                _messenger.Send(new LogMessage(string.Format("Failed to create directory {0}: {1}", rule.Dest, ex.Message)));
                return(false);
            }

            foreach (var sourceFile in _directory.GetFiles(rule.Source, filter, SearchOption.AllDirectories))
            {
                if (!_enabled)
                {
                    break;
                }

                var destPath = GetFileDest(sourceFile, rule.Source, rule.Dest, rule.Flatten);
                if (ShouldSync(sourceFile, destPath))
                {
                    _messenger.Send(new LogMessage(string.Format("Copying {0} to {1}", sourceFile, destPath)));
                    CopyFile(sourceFile, destPath);
                }
            }

            return(true);
        }
Example #2
0
        public static void ValidateDataDirectory(string bundledDataDirectory, string dataDirectory)
        {
            if (!Directory.Exists(dataDirectory))
            {
                Directory.CreateDirectory(dataDirectory);
            }

            foreach (var bundledDataPath in Directory.GetFiles(bundledDataDirectory))
            {
                var data     = Path.GetFileName(bundledDataPath);
                var dataPath = Path.Combine(dataDirectory, data.NonNull());
                if (!File.Exists(dataPath))
                {
                    File.Copy(bundledDataPath, dataPath);
                }
                else
                {
                    var time1 = FileInfo.FromFileName(bundledDataPath).LastWriteTimeUtc;
                    var time2 = FileInfo.FromFileName(dataPath).LastWriteTimeUtc;
                    if (time1 != time2)
                    {
                        File.Copy(bundledDataPath, dataPath, true);
                    }
                }
            }
        }
Example #3
0
        public void WriteFile(string fileName, string data, Encoding encoding)
        {
            if (!_directory.Exists(Path.GetDirectoryName(fileName)))
            {
                _directory.CreateDirectory(Path.GetDirectoryName(fileName));
            }

            _file.WriteAllText(fileName, data, encoding);
        }
Example #4
0
        string GetTempBasePath()
        {
            var path = CrossPlatform.GetApplicationTempDir();

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            return(path);
        }
Example #5
0
        private void SetTempFolders(Job job)
        {
            job.JobTempFolder = Path.Combine(_tempFolderProvider.TempFolder,
                                             "Job_" + Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
            job.JobTempOutputFolder = Path.Combine(job.JobTempFolder, "tempoutput");
            _directory.CreateDirectory(job.JobTempFolder);
            _directory.CreateDirectory(job.JobTempOutputFolder);

            // Shorten the temp folder for GS compatibility
            job.JobTempFolder = PathHelper.GetShortPathName(job.JobTempFolder);
            //TODO remove this after upgrade to GS 9.19
        }
 public override bool ExecuteOperation()
 {
     if (_impersonatedUser != null)
     {
         return(ExecuteOperationWithAuth());
     }
     if (DirectoryExist(_path, _dirWrapper))
     {
         _handleOverwrite.ExecuteOperation();
     }
     _dirWrapper.CreateDirectory(_path.Path);
     return(true);
 }
        string GetTempBasePath()
        {
            var path1 = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

            path1 = Path.Combine(path1, Assembly.GetEntryAssembly()?.GetName().Name ?? "Octopus.Calamari");
            path1 = Path.Combine(path1, "Temp");
            var path = path1;

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            return(path);
        }
            public DirectoryMockWithSpy(IDirectory directorySystem, IList <string> directories = null)
            {
                if (directorySystem != null)
                {
                    _directorySystem = directorySystem;
                }

                if (directories != null)
                {
                    foreach (var directory in directories)
                    {
                        _directorySystem.CreateDirectory(directory);
                    }
                }
            }
Example #9
0
        public void Save(IEnumerable <SiteFile> files)
        {
            string outputPath = _settings.TargetConnection.GetConnectionStringValue(_connectionStringFilepathKey);

            foreach (var sitePage in files)
            {
                string fullPath   = System.IO.Path.Combine(outputPath, sitePage.RelativeFilePath);
                string folderPath = System.IO.Path.GetDirectoryName(fullPath);

                if (!_directory.Exists(folderPath))
                {
                    _directory.CreateDirectory(folderPath);
                }

                if (sitePage.IsBase64Encoded)
                {
                    try
                    {
                        _file.WriteAllBytes(fullPath, Convert.FromBase64String(sitePage.Content));
                    }
                    catch (UnauthorizedAccessException)
                    {
                        //TODO: Log the fact that this file was skipped
                    }
                }
                else
                {
                    _file.WriteAllText(fullPath, sitePage.Content);
                }
            }
        }
 public static void CreateDirectoryIfNotExists(this IDirectory directory, string path)
 {
     if (!directory.Exists(path))
     {
         directory.CreateDirectory(path);
     }
 }
Example #11
0
        public List <CbrExtractOutput> Extract(CbrPaths paths)
        {
            var outputList = new List <CbrExtractOutput>();

            var files = _directory.GetFiles(paths.SourcePath, "*.*", paths.SourceSearchOption).Where(f => f.EndsWith(".cbr") || f.EndsWith(".cbz"));

            foreach (var item in files)
            {
                string finalDestination = paths.DestinationPath;

                if (_extractionRules.ForceSubFolder)
                {
                    finalDestination = _path.Combine(finalDestination, _path.GetFileName(item).Replace(_path.GetExtension(item), ""));
                    _directory.CreateDirectory(finalDestination);
                }


                var output = new CbrExtractOutput();
                output.HasFolder           = _extractionRules.ForceSubFolder;
                output.OriginalCbrFileName = _path.GetFileName(item);
                //TODO: consider subfolder approach
                output.FullSourcePath      = paths.SourcePath;
                output.FullDestinationPath = paths.DestinationPath;
                output.Files = FactoryDecoupling.WriteToFiles(finalDestination, item);
                outputList.Add(output);
            }

            return(outputList);
        }
Example #12
0
        public void Save(IEnumerable <SiteFile> files)
        {
            foreach (var sitePage in files)
            {
                String fullPath   = System.IO.Path.Combine(_outputPath, sitePage.RelativeFilePath);
                String folderPath = System.IO.Path.GetDirectoryName(fullPath);

                if (!_directory.Exists(folderPath))
                {
                    _directory.CreateDirectory(folderPath);
                }

                if (sitePage.IsBase64Encoded)
                {
                    try
                    {
                        _file.WriteAllBytes(fullPath, Convert.FromBase64String(sitePage.Content));
                    }
                    catch (UnauthorizedAccessException)
                    {
                        //TODO: Log the fact that this file was skipped
                    }
                }
                else
                {
                    _file.WriteAllText(fullPath, sitePage.Content);
                }
            }

            Console.WriteLine($"Site written to {System.IO.Path.GetFullPath(_outputPath)}");
        }
Example #13
0
        private void RecursiveCopy(IElement srcelem, IDirectory dstdirpar)
        {
            if(srcelem.IsDirectory) {

                IDirectory srcdir  = (IDirectory) srcelem;
                IElement   dstelem = dstdirpar.GetChild(srcdir.Name); // TODO: try catch

                if(dstelem != null) {
                    throw new Exception(dstelem.FullName + " already exists.");
                }

                IDirectory dstdir = dstdirpar.CreateDirectory(srcdir.Name); // TODO: try catch

                foreach(IDirectory srcsub in srcdir.GetDirectories()) { // TODO: try catch
                    if(!_cancel) RecursiveCopy(srcsub, dstdir);
                }

                foreach(IFile srcsub in srcdir.GetFiles()) { // TODO: try catch
                    if(!_cancel) RecursiveCopy(srcsub, dstdir);
                }

            } else {

                SingleCopy((IFile) srcelem, dstdirpar);
            }
        }
Example #14
0
        public ImageRepository(IFile file, IDirectory directory, IConfigurationManager configurationManager)
        {
            _configurationManager = configurationManager;
            File      = file;
            Directory = directory;
            var client = new MongoClient("mongodb://localhost:27017");

            Database   = client.GetDatabase(_configurationManager.GetValue("database"));
            Collection = Database.GetCollection <Image>("Images");
            GridFs     = new GridFSBucket(Database);

            var     fs     = new FileStream("config.json", FileMode.Open, FileAccess.Read);
            JObject config = null;

            using (StreamReader streamReader = new StreamReader(fs))
                using (JsonTextReader reader = new JsonTextReader(streamReader))
                {
                    config = (JObject)JToken.ReadFrom(reader);
                }
            ImageDirectory = config?.GetValue("ImageDirectory")?.ToString();
            if (!Directory.Exists(ImageDirectory))
            {
                Directory.CreateDirectory(ImageDirectory);
            }
        }
Example #15
0
        internal static void EnsureDirectory(this IDirectory directory, string filePath, out IDirectory dir, out string fileName)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            filePath = filePath.Trim('/');

            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentException("filePath");
            }

            var index = filePath.LastIndexOf('/');

            if (index < 0)
            {
                dir      = directory;
                fileName = filePath;
            }
            else
            {
                dir      = directory.CreateDirectory(filePath.Substring(0, index));
                fileName = filePath.Substring(index + 1);
            }
        }
Example #16
0
 private static void ValidateUserDirectory()
 {
     if (!Directory.Exists(Constant.PluginsDirectory))
     {
         Directory.CreateDirectory(Constant.PluginsDirectory);
     }
 }
Example #17
0
        private void ConfigureClientToRedirectMailsToDisk(SmtpClient client)
        {
            // http://stackoverflow.com/questions/567765/how-can-i-save-an-email-instead-of-sending-when-using-smtpclient
            client.DeliveryMethod          = SmtpDeliveryMethod.SpecifiedPickupDirectory;
            client.PickupDirectoryLocation = GetMailDiskFolderPath();

            // if we are working with a relative path we need to modify it
            if (client.PickupDirectoryLocation.StartsWith("~"))
            {
                var root       = AppDomain.CurrentDomain.BaseDirectory;
                var pickupRoot = client.PickupDirectoryLocation
                                 .Replace("~", root);

                pickupRoot = pickupRoot.Replace("/", @"\");
                client.PickupDirectoryLocation = pickupRoot;
            }

            // ensure that the pickup location exists
            if (_directory.Exists(client.PickupDirectoryLocation))
            {
                return;
            }

            _logger.Info($"RnMailClient - mail pickup directory not found ({client.PickupDirectoryLocation})," +
                         " attempting to create it now");

            _directory.CreateDirectory(client.PickupDirectoryLocation);

            _logger.Debug("RnMailClient - successfully created mail pickup directory");
        }
        public bool CompressAsset(string sourceFilePath, string targetFolderPath, bool overwrite = true, string encryptionKey = null)
        {
            if (string.IsNullOrWhiteSpace(sourceFilePath) ||
                !_file.Exists(sourceFilePath) ||
                (!string.IsNullOrWhiteSpace(targetFolderPath) &&
                 _directory.Exists(targetFolderPath) &&
                 (_file.GetAttributes(targetFolderPath) & FileAttributes.Directory) == 0))
            {
                return(false);
            }

            if (!string.IsNullOrWhiteSpace(targetFolderPath) &&
                !_directory.Exists(targetFolderPath))
            {
                _directory.CreateDirectory(targetFolderPath);
            }

            var targetCompressedFilePath = Path.Combine(targetFolderPath ?? string.Empty, GetCompressedAssetFileName(sourceFilePath, false));
            var targetEncryptedFilePath  = Path.Combine(targetFolderPath ?? string.Empty, GetCompressedAssetFileName(sourceFilePath, true));
            var encrypted = !string.IsNullOrWhiteSpace(encryptionKey);

            if (!overwrite &&
                ((_file.Exists(targetCompressedFilePath) && !encrypted) ||
                 (_file.Exists(targetEncryptedFilePath) && encrypted)))
            {
                return(false);
            }

            _file.DeleteFileIfExists(targetCompressedFilePath);

            SaveCompressedFile(sourceFilePath, targetCompressedFilePath);
            EncryptCompressedFileIfRequired(encrypted, targetEncryptedFilePath, targetCompressedFilePath, encryptionKey);

            return(true);
        }
Example #19
0
        public string RunWithCache(string cacheKey, Func <string> getValueToCache)
        {
            var cacheFilepath = GetCacheFilePath(cacheKey);

            try
            {
                if (!_file.Exists(cacheFilepath))
                {
                    if (!_directory.Exists(_dotnetUserProfileFolderPath))
                    {
                        _directory.CreateDirectory(_dotnetUserProfileFolderPath);
                    }

                    var runResult = getValueToCache();

                    _file.WriteAllText(cacheFilepath, runResult);
                    return(runResult);
                }
                else
                {
                    return(_file.ReadAllText(cacheFilepath));
                }
            }
            catch (Exception ex)
            {
                if (ex is UnauthorizedAccessException ||
                    ex is PathTooLongException ||
                    ex is IOException)
                {
                    return(getValueToCache());
                }

                throw;
            }
        }
Example #20
0
        /// <summary>
        ///     Creates the directory and stores in "CreatedDirectories" which parent directories hab to be created
        /// </summary>
        /// <returns>false if creating directory throws exception</returns>
        public bool CreateDirectory(string directory)
        {
            try
            {
                var directoryTree = GetDirectoryTree(directory);
                directoryTree = directoryTree.OrderBy(x => x).ToList(); //start with smallest

                foreach (var path in directoryTree)
                {
                    if (!_directoryWrap.Exists(path))
                    {
                        try
                        {
                            _directoryWrap.CreateDirectory(path);
                            _logger.Debug("Created directory " + path);
                        }
                        catch (Exception ex)
                        {
                            _logger.Warn("Exception while creating \"" + path + "\"\r\n" + ex.Message);
                            return(false);
                        }
                        _createdDirectories.Add(path);
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Example #21
0
        public void SendTestMail_EmailClientGetsCalledWithCorrectEmailContent_AttachmentFile()
        {
            var currentEmail = new Email();

            _pathWrap.GetTempPath().Returns("temp");


            _emailClient.When(x => x.ShowEmailClient(Arg.Any <Email>())).Do(
                x =>
            {
                currentEmail = x[0] as Email;
            });

            _clientTestEmail.SendTestEmail(_emailClientSettings);

            Received.InOrder(() =>
            {
                _pathWrap.GetTempPath();
                _directoryWrap.CreateDirectory(@"temp\PDFCreator-Test\SendSmtpTestmail");
                _fileWrap.WriteAllText(@"temp\PDFCreator-Test\SendSmtpTestmail\PDFCreator Mail Client Test.pdf", "");
            });

            Assert.AreEqual(1, currentEmail.Attachments.Count, "Test mail has more than one attachment");
            Assert.AreEqual(@"temp\PDFCreator-Test\SendSmtpTestmail\PDFCreator Mail Client Test.pdf",
                            currentEmail.Attachments.First().Filename);
        }
Example #22
0
        static Log()
        {
            CurrentLogDirectory = Path.Combine(Constant.DataDirectory, DirectoryName, Constant.Version);
            if (!Directory.Exists(CurrentLogDirectory))
            {
                Directory.CreateDirectory(CurrentLogDirectory);
            }

            var configuration = new LoggingConfiguration();
            var target        = new FileTarget();

            target.Layout = NLog.Layouts.Layout.FromString("[${longdate}] [${level:uppercase=true}]${message}\n");
            configuration.AddTarget("file", target);

            // Adding CurrentCulture since this is user facing
            target.FileName = CurrentLogDirectory.Replace(@"\", "/", StringComparison.CurrentCulture) + "/${shortdate}.txt";
#if DEBUG
            var rule = new LoggingRule("*", LogLevel.Debug, target);
#else
            var rule = new LoggingRule("*", LogLevel.Info, target);
#endif
            configuration.LoggingRules.Add(rule);
            target.Dispose();
            LogManager.Configuration = configuration;
        }
        private void SetInProgressSentinel()
        {
            try
            {
                if (!_directory.Exists(_nugetCachePath))
                {
                    _directory.CreateDirectory(_nugetCachePath);
                }

                // open an exclusive handle to the in-progress sentinel and mark it for delete on close.
                // we open with exclusive FileShare.None access to indicate that the operation is in progress.
                // buffer size is minimum since we won't be reading or writing from the file.
                // delete on close is to indicate that the operation is no longer in progress when we dispose
                // this.
                InProgressSentinel = _file.OpenFile(
                    InProgressSentinelPath,
                    FileMode.OpenOrCreate,
                    FileAccess.ReadWrite,
                    FileShare.None,
                    1,
                    FileOptions.DeleteOnClose);
            }
            catch (UnauthorizedAccessException)
            {
                UnauthorizedAccess = true;
            }
            catch { }
        }
Example #24
0
 public static void CreateDirectoryIfNotExists(this IDirectory directory, string path)
 {
     Guard.Against.NullOrWhiteSpace(path, nameof(path));
     if (!directory.Exists(path))
     {
         directory.CreateDirectory(path);
     }
 }
        public void Move(string filePath, string destinationDirectory)
        {
            _directory.CreateDirectory(destinationDirectory);

            var newFilePath = $@"{destinationDirectory}\{_path.GetFileName(filePath)}";

            _file.Move(filePath, newFilePath);
        }
Example #26
0
        private void CreateOutputDirectory(string outputFilenameTemplate)
        {
            var directory = PathSafe.GetDirectoryName(outputFilenameTemplate);

            if (directory != null && !DirectoryWrap.Exists(directory))
            {
                DirectoryWrap.CreateDirectory(directory);
            }
        }
Example #27
0
        public void ValidateResourceFolder()
        {
            var folder = EnvironmentVariables.ResourcePath;

            if (!_directory.Exists(folder))
            {
                _directory.CreateDirectory(folder);
            }
        }
        public void TestCleanup()
        {
            CloseEditor();

            if (!Directory.Exists(_settingsFolderPath))
            {
                Directory.CreateDirectory(_settingsFolderPath);
            }
        }
Example #29
0
        // Public Methods (3) 

        public IDirectory GetDirectory(IEnumerable <char> path, bool createIfNotExist = false)
        {
            var dirPath = path.AsString();

            if (dirPath != null)
            {
                // remove beginning '/' chars
                while (dirPath.StartsWith(_PATH_SEPERATOR_EXPR))
                {
                    dirPath = dirPath.Substring(1)
                              .Trim();
                }

                // remove encoding '/' chars
                while (dirPath.EndsWith(_PATH_SEPERATOR_EXPR))
                {
                    dirPath = dirPath.Substring(0, dirPath.Length - 1)
                              .Trim();
                }
            }

            if (string.IsNullOrWhiteSpace(dirPath))
            {
                return(this.GetRootDirectory());
            }

            var parts = dirPath.Split(new string[] { _PATH_SEPERATOR_EXPR },
                                      StringSplitOptions.None);

            IDirectory currentDir = this.GetRootDirectory();

            for (var i = 0; i < parts.Length; i++)
            {
                var p = parts[i].ToLower().Trim();

                var subDir = CollectionHelper.SingleOrDefault(currentDir.GetDirectories(),
                                                              d => p == (d.Name ?? string.Empty).ToLower().Trim());

                if (subDir == null)
                {
                    if (createIfNotExist)
                    {
                        subDir = currentDir.CreateDirectory(p);
                    }
                }

                currentDir = subDir;
                if (currentDir == null)
                {
                    // part does not exist
                    break;
                }
            }

            return(currentDir);
        }
Example #30
0
        public string GetDownloadPath(string downloadUrl)
        {
            var downloadLocation = _tempFolderProvider.TempFolder;

            _directory.CreateDirectory(downloadLocation);
            var uri      = new Uri(downloadUrl);
            var filename = PathSafe.GetFileName(uri.LocalPath);

            return(PathSafe.Combine(downloadLocation, filename));
        }
Example #31
0
        public void BuildReleaseExamples(string releasePath)
        {
            var programDataBuilders = new List <ResourceBuilderTO>();
            var resourcePath        = EnvironmentVariables.ResourcePath;

            if (!_directoryWrapper.Exists(resourcePath))
            {
                _directoryWrapper.CreateDirectory(resourcePath);
            }
            var resourcesFolders    = _directoryWrapper.EnumerateDirectories(resourcePath, "*", SearchOption.AllDirectories);
            var allResourcesFolders = resourcesFolders.ToList();

            allResourcesFolders.Add(resourcePath);

            BuildStream(resourcePath, allResourcesFolders.ToArray(), programDataBuilders);

            // get all installed resource ids in ProgramData
            var programDataIds = programDataBuilders.Select(currentItem =>
            {
                XElement xml = null;
                try
                {
                    xml = XElement.Load(currentItem._fileStream);
                }
                catch (Exception e)
                {
                    Dev2Logger.Error("Resource [ " + currentItem._filePath + " ] caused " + e.Message, GlobalConstants.WarewolfError);
                }

                return(xml?.Attribute("ID")?.Value ?? null);
            })
                                 .Where(o => o != null)      // ignore files with no id
                                 .OrderBy(o => o).ToArray(); // cache installed ids in array

            var releaseFolders    = _directoryWrapper.EnumerateDirectories(releasePath, "*", SearchOption.AllDirectories);
            var allReleaseFolders = releaseFolders.ToList();

            allReleaseFolders.Add(releasePath);

            var programFilesBuilders = new List <ResourceBuilderTO>();

            BuildStream(releasePath, allReleaseFolders.ToArray(), programFilesBuilders);

            var foundMissingResources = CopyMissingResources(programDataIds, programFilesBuilders, _directoryWrapper, _fileWrapper);

            foreach (var builderTO in programDataBuilders.Concat(programFilesBuilders))
            {
                builderTO._fileStream.Close();
            }

            if (foundMissingResources)
            {
                ResourceCatalog.Instance.Reload();
            }
        }
Example #32
0
        public void Directory(IDirectory sdir, IDirectory ddir)
        {
            if (ddir == null || sdir == null) return;

            sdir.Source = ddir.Source = sdir;
            sdir.Destination = ddir.Destination = ddir;

            int con;

            if (sdir is LocalDirectory && ddir is LocalDirectory) con = 1;
            else {
                con = Math.Max(FtpConnections.Count(true, sdir.Url), FtpConnections.Count(false, ddir.Url));
                if (ddir is FtpDirectory) {
                    ((FtpDirectory)ddir).TransferProgress = true;
                } else if (sdir is FtpDirectory) {
                    ((FtpDirectory)sdir).TransferProgress = true;
                }
            }
            if (con == 0) con = 1;
            var list = sdir.List().Where(file => !Silversite.Services.Paths.Match(ExcludePatterns, file.RelativePath)).ToList();
            var dlist = ddir.List();
            //ddir.CreateDirectory(null);

            Parallel.ForEach<FileOrDirectory>(list, new ParallelOptions { MaxDegreeOfParallelism = con },
                (src) => {
                    FileOrDirectory dest = null;
                    lock(dlist) { if (dlist.Contains(src.Name)) dest = dlist[src.Name]; }
                    if (dest != null && dest.Class != src.Class && (src.Changed > dest.Changed || Mode == Silversite.Services.Sync.Mode.Clone)) {
                        ddir.Delete(dest);
                        dest = null;
                    }
                    if (src.Class == ObjectClass.File) { // src is a file
                        if (dest == null
                            || ((Mode == Silversite.Services.Sync.Mode.Update || Mode == Silversite.Services.Sync.Mode.Add) && src.Changed > dest.Changed)
                            || (Mode == Silversite.Services.Sync.Mode.Clone && (src.Changed > dest.Changed + dt))) {
                            var s = sdir.ReadFile(src);
                            if (s != null) {
                                using (s) {
                                    ddir.WriteFile(s, src);
                                }
                            }
                        }
                    } else { // src is a directory
                        if (dest == null) Directory((IDirectory)src, ddir.CreateDirectory(src));
                        else Directory((IDirectory)src, (IDirectory)dest);
                    }
                    lock (dlist) { dlist.Remove(src.Name); }
                });
            if (Mode != Silversite.Services.Sync.Mode.Add) {
                foreach (var dest in dlist) ddir.Delete(dest);
            }
        }
 public void StartTest()
 {
     _directoryWrap = new DirectoryWrap();
     _directoryInfoWrap = _directoryWrap.CreateDirectory(path);
     Assert.IsTrue(_directoryInfoWrap.Exists, "Directory TempTest must be created.");
 }