Exemplo n.º 1
0
        private static IEnumerable <string> CopyDirectory(string sourceFolder, string destFolder)
        {
            try
            {
                var result = new List <string>();
                if (!Directory.Exists(destFolder))
                {
                    Directory.CreateDirectory(destFolder);
                }
                var files = Directory.GetFiles(sourceFolder);
                foreach (var file in files)
                {
                    var name = Path.GetFileName(file);
                    var dest = Path.Combine(destFolder, name);
                    File.Copy(file, dest);
                    result.Add(file);
                }

                var folders = Directory.GetDirectories(sourceFolder);
                foreach (var folder in folders)
                {
                    if (DirectoryHelpers.Equals(folder, destFolder))
                    {
                        continue;
                    }

                    var name = Path.GetFileName(folder);
                    var dest = Path.Combine(destFolder, name);
                    result.AddRange(CopyDirectory(folder, dest));
                }

                return(result);
            }
            catch (System.Exception e)
            {
                throw new CopyDirectoryFailed("Не удалось скопировать файл или папку. См InnerException.", sourceFolder, destFolder, e);
            }
        }
Exemplo n.º 2
0
        public override async Task BeforeSave(ChangeTrackerArgs args)
        {
            if (!DirectoryHelpers.ValidateSettingPath(this.Folder))
            {
                throw new DirectoryNotFoundException($"Не найдена папка {this.Folder}, папка должна существовать.");
            }

            if (!args.IsNewEntity)
            {
                using (var context = Repository.GetEntityContext())
                {
                    var folderState = args.GetPropertyState <string>(nameof(Folder));
                    if (folderState.IsChanged)
                    {
                        var mangaFolders = await context.Get <IManga>().Select(m => m.Folder).ToListAsync().ConfigureAwait(false);

                        mangaFolders = mangaFolders.Select(DirectoryHelpers.GetAbsoluteFolderPath).ToList();
                        var settingAbsoluteFolderPath = DirectoryHelpers.GetAbsoluteFolderPath(this.Folder);
                        if (mangaFolders.Any(f => DirectoryHelpers.Equals(f, settingAbsoluteFolderPath) || DirectoryHelpers.IsSubfolder(f, settingAbsoluteFolderPath)))
                        {
                            throw new MangaSettingSaveValidationException($"Папка {this.Folder} используется мангой.", this);
                        }
                    }

                    var uriState = args.GetPropertyState <Uri>(nameof(MainUri));
                    if (folderState.IsChanged || uriState.IsChanged)
                    {
                        if (!args.CanAddEntities)
                        {
                            var debugMessage = $"Manga settings {MangaName}({Manga:D}) changed:";
                            if (folderState.IsChanged)
                            {
                                debugMessage += $" folder changed from '{folderState.OldValue}' to '{folderState.Value}'";
                            }
                            if (uriState.IsChanged)
                            {
                                debugMessage += $" uri changed from '{uriState.OldValue}' to '{uriState.Value}'";
                            }
                            Log.Add(debugMessage);
                        }

                        // Changed manga will be added to session.
                        if (args.CanAddEntities)
                        {
                            if (uriState.IsChanged &&
                                Login != null &&
                                Equals(Login.MainUri, uriState.OldValue) &&
                                !Equals(Login.MainUri, uriState.Value))
                            {
                                Login.MainUri = MainUri;
                            }

                            var mangas = await context.Get <IManga>().Where(m => m.Setting == this).ToListAsync().ConfigureAwait(false);

                            foreach (var manga in mangas)
                            {
                                if (uriState.IsChanged)
                                {
                                    manga.Uri = new Uri(manga.Uri.OriginalString.Replace(uriState.OldValue.GetLeftPart(UriPartial.Authority), uriState.Value.GetLeftPart(UriPartial.Authority)));
                                }
                                if (folderState.IsChanged)
                                {
                                    manga.RefreshFolder();
                                }
                                await context.AddToTransaction(manga).ConfigureAwait(false);
                            }
                        }
                    }
                }
            }

            await base.BeforeSave(args).ConfigureAwait(false);
        }