Ejemplo n.º 1
0
        public static Task CreateDirectory(this IService service, Action <DirectoryCleanupConfig> init)
        {
            var r = new DirectoryCleanupConfig();

            init(r);
            return(service.CreateDirectory(r));
        }
Ejemplo n.º 2
0
        public static bool?IsExpired(DirectoryCleanupConfig record, SubDirectoryState subDirectory)
        {
            if (!Directory.Exists(subDirectory.FullPath))
            {
                return(null);
            }
            var lastModified = Directory.GetLastWriteTime(subDirectory.FullPath);

            return(ExpirationDate(lastModified, record.MaxAge) < DateTime.Now);
        }
Ejemplo n.º 3
0
        public async Task UpdateDirectory(DirectoryCleanupConfig directory)
        {
            await EnsureFreshness();

            var index = records.FindIndex(r => r.Id == directory.Id);

            if (index != -1)
            {
                records[index]          = (DirectoryCleanupConfig)directory.Clone();
                records[index].FullPath = PathUtils.NormalizePath(directory.FullPath);
                await PutAllDataAndHandleExceptions();

                Changed?.Invoke(this, EventArgs.Empty);
            }
            else
            {
                throw new DirectoryConfigNotFoundException(directory.Id);
            }
        }
Ejemplo n.º 4
0
        public async Task CreateDirectory(DirectoryCleanupConfig directory)
        {
            if (directory == null)
            {
                throw new ArgumentNullException(nameof(directory));
            }

            await EnsureFreshness();

            var normalizedPath = PathUtils.NormalizePath(directory.FullPath);

            if (records.Any(r => r.Id == directory.Id))
            {
                throw new DirectoryConfigNotFoundException(directory.Id);
            }

            records.Insert(0, directory);
            await PutAllDataAndHandleExceptions();

            Changed?.Invoke(this, EventArgs.Empty);
        }
Ejemplo n.º 5
0
 public Task CreateDirectory(DirectoryCleanupConfig record)
 {
     return(ds.CreateDirectory(record));
 }
Ejemplo n.º 6
0
        private DirectoryState GetState(DirectoryCleanupConfig config)
        {
            var state = new DirectoryState
            {
                Config = config
            };
            DirectoryInfo directoryInfo;

            try
            {
                directoryInfo = new DirectoryInfo(config.FullPath);
            }
            catch (PathTooLongException)
            {
                state.PathTooLong = true;
                return(state);
            }
            catch (ArgumentException)
            {
                state.InvalidPath = true;
                return(state);
            }

            state.Exists = directoryInfo.Exists;

            if (directoryInfo.Exists)
            {
                DateTime?lastWriteTime = null;

                try
                {
                    lastWriteTime = Directory.GetLastWriteTime(config.FullPath);
                }
                catch (UnauthorizedAccessException e)
                {
                    state.Unauthorized = true;
                }

                var lastCleanupTime       = config.LastCleanupTime ?? lastWriteTime ?? DateTime.MinValue;
                var correctedMaxAgeInDays = Math.Max(config.MaxAge, minAge.TotalDays);
                var expiration            = lastCleanupTime.AddDays(correctedMaxAgeInDays);
                state.ExpirationTime = expiration;

                var hasFilter = !string.IsNullOrWhiteSpace(config.Filter);

                var includeFiles = config.CleanupTarget == CleanupTarget.All ||
                                   config.CleanupTarget == CleanupTarget.Files;
                var includeSubDirectories = config.CleanupTarget == CleanupTarget.All ||
                                            config.CleanupTarget == CleanupTarget.Directories;

                if (includeFiles)
                {
                    var files = hasFilter ? directoryInfo.GetFiles(config.Filter) : directoryInfo.GetFiles();
                    state.NumberOfFiles = files.Length;
                }

                if (includeSubDirectories)
                {
                    var subDirs = hasFilter
                        ? directoryInfo.GetDirectories(config.Filter)
                        : directoryInfo.GetDirectories();
                    state.NumberOfSubDirectories = subDirs.Length;
                }

                state.NumberOfChildren = state.NumberOfSubDirectories + state.NumberOfFiles;
            }

            return(state);
        }
Ejemplo n.º 7
0
 public Task UpdateDirectory(DirectoryCleanupConfig record)
 {
     return(TryNTimesAsync(service => service.UpdateDirectory(record)));
 }