public static ProcessStatus SyncArchive()
        {
            var status = new ProcessStatus();

            var newRecords = new List<Photo>();

            foreach (var photo in Repository.Photos())
            {
                if (!File.Exists(photo.ArchiveFullName))
                {
                    photo.DateDeleted = DateTime.Now;
                    photo.Status = PhotoStatus.Deleted;
                    status.PhotoRemoved();
                }
                else
                {
                    status.PhotoExists();
                }
            }

            var archivePath = Configuration.ArchivePath.TrimEnd('\\');
            var archive = new DirectoryInfo(archivePath);

            var extensions = Configuration.Extensions;

            var fileList = new List<FileInfo>();

            foreach (var extension in extensions)
            {
                var mask = string.Format("*.{0}", extension);

                var files = archive.GetFiles(mask, SearchOption.AllDirectories)
                    .Where(x => x.DirectoryName != null)
                    .ToList();

                fileList.AddRange(files);
            }

            Parallel.ForEach(fileList, file =>
                                    {
                                        var shelf = file.Directory.FullName.Replace(archivePath, string.Empty).TrimStart('\\');
                                        var filename = file.Name;

                                        if (!Repository.Photos().Any(x => x.Shelf == shelf && x.Filename == filename))
                                        {
                                            newRecords.Add(Photo.New(file, shelf));
                                            status.PhotoAdded();
                                        }
                                    });

            newRecords.Save();

            var dups = DeDupe();

            status.PhotoRemoved(dups);

            return status;
        }
Example #2
0
        private void btnLoad_Click(object sender, EventArgs e)
        {
            Files = new List<Movie>();

            //Files = Files.Open(fileNamesFile);

            if (Files.Count < 1)
            {
                if (Files.GetMovies(this.Dir, this.Replaces))
                {
                    if (Files.Save(fileNamesFile))
                        lblDir.Text = "Saved";
                    else
                        lblDir.Text = "Not Saved";
                }
                else
                    lblDir.Text = "No Files Exist.\nChoose another\ndirectory.";
            }
            lblDir.Text = folderBrowserDialog1.SelectedPath + "\n" + Files.Count.ToString() + " avi files found";
            dataGridView1.DataSource = Files;
        }
        public void SaveIEnumerableExtension()
        {
            List<Task2> Tasks = new List<Task2>();
            for (int x = 0; x < 100; ++x)
            {
                Task2 TempTask = new Task2();
                TempTask.Description = "This is a test";
                TempTask.DueDate = new DateTime(1900, 1, 1);
                TempTask.Name = "Test task";
                Tasks.Add(TempTask);
            }
            Tasks.Save<Task2, long>();

            using (Utilities.SQL.SQLHelper Helper = new Utilities.SQL.SQLHelper("SELECT * FROM Task2_", "Data Source=localhost;Initial Catalog=ORMTestDatabase3;Integrated Security=SSPI;Pooling=false", CommandType.Text))
            {
                Helper.ExecuteReader();
                int Counter = 0;
                while (Helper.Read())
                {
                    Assert.Equal("This is a test", Helper.GetParameter<string>("Description_", ""));
                    Assert.Equal("Test task", Helper.GetParameter<string>("Name_", ""));
                    ++Counter;
                }
                Assert.Equal(100, Counter);
            }
        }
        protected override void DoAction()
        {
            var entities = GetEntitiesToProcess();
            foreach (var entity in entities) {
                var queriesForDomain = new HashSet<string>();
                entity.Status |= DomainStatus.PhrasesCollected;
                try {
                    for (var page = 1;; page++) {
                        var hasUnique = false;
                        var content = SpywordsQueryWrapper.GetQueriesForDomain(entity.Domain, page);
                        var listLinksToInsert = new List<Domainphrase>();
                        foreach (Match wordMatch in _siteSpywordsExpractor.Matches(content)) {
                            var word = wordMatch.Groups["word"].Value.ToLower();
                            if (queriesForDomain.Contains(word)) {
                                continue;
                            }
                            queriesForDomain.Add(word);
                            hasUnique = true;
                            var phrase = Phrase.DataSource
                                .WhereEquals(Phrase.Fields.Text, word)
                                .First(Phrase.Fields.ID);
                            if(phrase == null) {
                                phrase = new Phrase {
                                    Datecreated = DateTime.UtcNow,
                                    Status = PhraseStatus.NotCollected,
                                    CollectionIdentity = CollectionIdentity,
                                    Text = word
                                };
                                phrase.Save();
                            }
                            var firstDomainPhrase = Domainphrase.DataSource
                                .WhereEquals(Domainphrase.Fields.DomainID, entity.ID)
                                .WhereEquals(Domainphrase.Fields.PhraseID, phrase.ID)
                                .First();
                            if (firstDomainPhrase == null) {
                                var domainphrase = new Domainphrase {
                                    DomainID = entity.ID,
                                    PhraseID = phrase.ID,
                                    SourceType = SourceType.Context,
                                    CollectionIdentity = CollectionIdentity
                                };
                                listLinksToInsert.Add(domainphrase);
                            } else {
                                firstDomainPhrase.SourceType |= SourceType.Context;
                                firstDomainPhrase.Save();
                            }
                        }
                        TaskRunner.Instance.AddAction(() => {
                            listLinksToInsert.Save<Domainphrase, int>();
                        });
                        if (!hasUnique) {
                            break;
                        }
                    }
                }
                catch (Exception ex) {
                    Logger.Error(ex);
                    entity.Status |= DomainStatus.PhrasesCollectedError;
                }

                entity.Save();
            }
        }
        private static int DeDupe()
        {
            var dupCount = 0;

            var dups = Repository.Photos().GroupBy(x => x.HashValue)
                .Select(x => new
                                 {
                                     Hash = x.Key,
                                     PhotoCount = x.Count()
                                 })
                .Where(x => x.PhotoCount > 1)
                .ToList();

            var dupSaves = new List<Photo>();

            var activeList = new List<PhotoStatus>
                                 {
                                     PhotoStatus.Excluded,
                                     PhotoStatus.Included,
                                     PhotoStatus.Pending
                                 };

            foreach (var dup in dups)
            {
                var ids = Repository.Photos().Where(x => x.HashValue == dup.Hash).Select(x => x.ID).ToList();

                var first = Repository.Photos()
                    .Where(x => ids.Contains(x.ID))
                    .Where(x => activeList.Contains(x.Status))
                    .OrderBy(x => x.DateAdded)
                    .ThenByDescending(x => x.Status)
                    .FirstOrDefault();

                if (first != null)
                {
                    ids.Remove(first.ID);
                    dupSaves.Add(first);
                }

                foreach (var photo in Repository.Photos().Where(x => ids.Contains(x.ID)))
                {
                    if (!activeList.Contains(photo.Status))
                    {
                        continue;
                    }

                    photo.Status = PhotoStatus.Duplicated;
                    photo.DateDeleted = DateTime.Now;

                    dupSaves.Add(photo);

                    dupCount++;
                }
            }

            dupSaves.Save();

            return dupCount;
        }
        public static ProcessStatus ExtractFromCamera(bool cleanCamera = false)
        {
            var status = new ProcessStatus();

            var cameraPath = Configuration.CameraPath.TrimEnd('\\');
            var archive = new DirectoryInfo(cameraPath);

            var extensions = Configuration.Extensions;

            var fileList = new List<FileInfo>();

            foreach (var extension in extensions)
            {
                var mask = string.Format("*.{0}", extension);

                var files = archive.GetFiles(mask, SearchOption.AllDirectories)
                    .Where(x => x.DirectoryName != null)
                    .ToList();

                fileList.AddRange(files);
            }

            if (fileList.Count == 0)
            {
                return status;
            }

            var shelfDate = DateTime.Now;

            var shelf = string.Format("{0:yyyyMMdd_hhmmss}", shelfDate);

            var shelfPath = Path.Combine(Configuration.ArchivePath, shelf);

            var newPhotos = new List<Photo>();

            foreach (var file in fileList)
            {
                var photo = Photo.New(file, shelf);

                if (Repository.Photos().Any(x => x.HashValue == photo.HashValue))
                {
                    status.PhotoExists();
                    continue;
                }

                if (!Directory.Exists(shelfPath))
                {
                    Directory.CreateDirectory(shelfPath);
                }

                bool success;

                try
                {
                    File.Copy(file.FullName, photo.ArchiveFullName);
                    success = true;

                }
                catch (Exception)
                {
                    success = false;
                }

                if (success)
                {
                    newPhotos.Add(photo);
                    status.PhotoAdded();

                    if (cleanCamera)
                    {
                        try
                        {
                            File.Delete(file.FullName);
                            status.PhotoRemoved();
                        }
                        catch (Exception)
                        {
                            // swallow
                        }
                    }
                }
            }

            newPhotos.Save();

            return status;
        }
        public static ProcessStatus UpdateFrame()
        {
            var status = new ProcessStatus();

            var size = new Size(Configuration.FrameWidth, Configuration.FrameHeight);

            var frame = new DirectoryInfo(Configuration.FramePath);

            var files = frame.GetFiles("*.*", SearchOption.AllDirectories)
                    .ToList();

            var filesToDelete = new List<FileInfo>();

            var photos = Repository.Photos()
                .Where(x => x.Status == PhotoStatus.Included)
                .ToList();

            foreach (var file in files)
            {
                if (photos.All(x => x.FrameFullName != file.FullName))
                {
                    filesToDelete.Add(file);
                    status.PhotoRemoved();
                }
            }

            filesToDelete.ForEach(x => File.Delete(x.FullName));

            var added = new List<Photo>();

            foreach (var photo in photos)
            {
                if (File.Exists(photo.FrameFullName))
                {
                    status.PhotoExists();
                    continue;
                }

                status.PhotoAdded();

                Image frameImage;

                using (var image = Image.FromFile(photo.ArchiveFullName))
                {
                    frameImage = ResizeImage(image, size);
                }

                frameImage.Save(photo.FrameFullName);
            }

            added.Save(false);

            var states = photos.Select(x => new PhotoFrameState
                                                {
                                                    PhotoID = x.ID,
                                                    DateAdded = DateTime.Now
                                                });

            states.Save();

            return status;
        }