Exemplo n.º 1
0
 public void UpdateStatus(Enums.DocStatus status, Document doc)
 {
     var fic = KernelFactory.Instance.Get<IFileIndexContext>();
     var document = fic.Documents.First(i => i.DocumentId == doc.DocumentId);
     document.Status = status;
     fic.SaveChanges();
 }
Exemplo n.º 2
0
        public void CheckFileTimes(string path, Document doc)
        {
            var fi = new FileInfo(path);
            fi.Refresh();

            // READ Events
            if (doc.DocEvents.Any(i => i.Type == Enums.EventType.Read))
            {
                var recentRead = doc.DocEvents.OrderByDescending(i => i.Time).First(j => j.Type == Enums.EventType.Read);
                if (DateTime.Compare(recentRead.Time, fi.LastAccessTime.AddMilliseconds(-fi.LastAccessTime.Millisecond)) < 0)
                {
                    // add the event to the list with the Type 'Read'
                    _events.Add(new FileSystemEvent() {
                        Type = Enums.EventType.Read,
                        FileInf = fi,
                        DocumentId = doc.DocumentId,
                        LastWrite = fi.LastWriteTime,
                        LastAccess = fi.LastAccessTime,
                        CreationTime = fi.CreationTime
                    });
                }
            }

            // WRITE Events
            if (doc.DocEvents.Any(i => i.Type == Enums.EventType.Write))
            {
                var recentWrite = doc.DocEvents.OrderByDescending(i => i.Time).First(j => j.Type == Enums.EventType.Write);
                if (DateTime.Compare(recentWrite.Time, fi.LastWriteTime.AddMilliseconds(-fi.LastWriteTime.Millisecond)) < 0)
                {
                    // add the event to the list with the Type 'Write'
                    _events.Add(new FileSystemEvent() {
                        Type = Enums.EventType.Write,
                        FileInf = fi,
                        DocumentId = doc.DocumentId,
                        LastWrite = fi.LastWriteTime,
                        LastAccess = fi.LastAccessTime,
                        CreationTime = fi.CreationTime
                    });
                }
            }
        }
Exemplo n.º 3
0
 public void Retrieve(string key, string dest, Document doc)
 {
     Download.DownloadFile(key, Settings.Default.S3BucketName, dest);
     var vm = KernelFactory.Instance.Get<IVersionManager>();
     vm.UpdateStatus(Enums.DocStatus.Current, doc);
 }
Exemplo n.º 4
0
 private DocEvent GetLastWriteEvent(Document doc)
 {
     return doc.DocEvents.OrderByDescending(i => i.Time).First(j => j.Type == Enums.EventType.Write);
 }
Exemplo n.º 5
0
        public void WriteChangesToDB()
        {
            foreach (var e in _events)
            {
                // CREATE
                if (e.Type == Enums.EventType.Create)
                {
                    var hash = KernelFactory.Instance.Get<IHash>().HashFile(e.FileInf.FullName);
                    // path doesn't exist - check against hash
                    if (_fic.Documents.Any(i => i.DocumentHash == hash))
                    {
                        // if hash exists just add the path to the existing document
                        var doc = _fic.Documents.First(i => i.DocumentHash == hash);

                        if (doc.Status == Enums.DocStatus.Archived)
                        {
                            doc.Status = Enums.DocStatus.Current;
                        }

                        doc.DocPaths.Add(new DocPath() { Path = e.FileInf.FullName, Directory = e.FileInf.DirectoryName, Name = e.FileInf.Name });
                        doc.DocEvents.Add(new DocEvent() { Type = e.Type, Time = e.CreationTime });
                        UpdateFileTimes(doc, e);
                    }
                    else
                    {
                        // no path or hash found in DB
                        var doc = new Document() { DocumentHash = hash, Size = e.FileInf.Length, Status = Enums.DocStatus.Indexed };
                        doc.DocPaths.Add(new DocPath() { Path = e.FileInf.FullName, Directory = e.FileInf.DirectoryName, Name = e.FileInf.Name });
                        doc.DocEvents.Add(new DocEvent() { Type = e.Type, Time = e.CreationTime });
                        UpdateFileTimes(doc, e);
                        _fic.Documents.Add(doc);
                    }
                }
                // READ
                else if (e.Type == Enums.EventType.Read)
                {
                    // just add the read event
                    var doc = _fic.DocPaths.First(j => j.Path == e.FileInf.FullName).Document;
                    doc.DocEvents.Add(new DocEvent() { Time = e.LastAccess, Type = e.Type });
                }
                // WRITE
                else if (e.Type == Enums.EventType.Write)
                {
                    var hash = KernelFactory.Instance.Get<IHash>().HashFile(e.FileInf.FullName);
                    // add the write event plus update the file hash, possibly branch it out to a new document too + check it hasn't updated to = another document
                    if (_fic.Documents.Any(i => i.DocumentHash == hash))
                    {
                        // file hash matches an existing document (move the path to the matching document)
                        var matchingDoc = _fic.Documents.First(i => i.DocumentHash == hash);

                        if (matchingDoc.Status == Enums.DocStatus.Archived)
                        {
                            matchingDoc.Status = Enums.DocStatus.Current;
                        }

                        matchingDoc.DocPaths.Add(_fic.DocPaths.First(i => i.Path == e.FileInf.FullName));
                        matchingDoc.DocEvents.Add(new DocEvent() { Time = e.LastWrite, Type = e.Type });
                        //Logger.write("Changed (new hash matches existing document) " + e.FileInf.Name);
                    }
                    else
                    {
                        if (_fic.DocPaths.Any(i => i.Path == e.FileInf.FullName))
                        {
                            var currentPath = _fic.DocPaths.First(i => i.Path == e.FileInf.FullName);
                            var relatedDoc = currentPath.Document;

                            if (relatedDoc.DocPaths.Count() == 1)
                            {
                                // update the doc
                                relatedDoc.DocumentHash = hash;
                                relatedDoc.Size = e.FileInf.Length;
                                relatedDoc.Status = Enums.DocStatus.Indexed;
                                relatedDoc.DocEvents.Add(new DocEvent() { Time = e.LastWrite, Type = e.Type });
                                //Logger.write("Changed (same document, updated hash, status indexed) " + e.FileInf.Name);
                            }
                            else if (relatedDoc.DocPaths.Count() > 1)
                            {
                                // create new doc and add the path to it
                                var newDoc = new Document() { DocumentHash = hash, Size = e.FileInf.Length, Status = Enums.DocStatus.Indexed };
                                newDoc.DocPaths.Add(currentPath);
                                newDoc.DocEvents.Add(new DocEvent() { Time = e.LastWrite, Type = e.Type });
                                _fic.Documents.Add(newDoc);
                                //Logger.write("Changed (new hash, new document, status indexed");
                            }
                            else
                            {
                                // can't happen
                                Logger.Write("Error, impossible logic");
                            }
                        }
                    }
                }

                RestoreFileTimes(e);
                _fic.SaveChanges();
            }

            _events.Clear();
        }
Exemplo n.º 6
0
        public void UpdateFileTimes(Document doc, FileSystemEvent eve)
        {
            // READ Events
            if (doc.DocEvents.Any(i => i.Type == Enums.EventType.Read))
            {
                var recentRead = doc.DocEvents.OrderByDescending(i => i.Time).First(j => j.Type == Enums.EventType.Read);
                if (DateTime.Compare(recentRead.Time, eve.FileInf.LastAccessTime.AddMilliseconds(-eve.FileInf.LastAccessTime.Millisecond)) < 0)
                {
                    // add event to doc
                    doc.DocEvents.Add(new DocEvent() { Type = Enums.EventType.Read, Time = eve.FileInf.LastAccessTime });
                }
            }
            else
            {
                doc.DocEvents.Add(new DocEvent() { Type = Enums.EventType.Read, Time = eve.FileInf.LastAccessTime });
            }

            // WRITE Events
            if (doc.DocEvents.Any(i => i.Type == Enums.EventType.Write))
            {
                var recentWrite = doc.DocEvents.OrderByDescending(i => i.Time).First(j => j.Type == Enums.EventType.Write);
                if (DateTime.Compare(recentWrite.Time, eve.FileInf.LastWriteTime.AddMilliseconds(-eve.FileInf.LastWriteTime.Millisecond)) < 0)
                {
                    // add event to doc
                    doc.DocEvents.Add(new DocEvent() { Type = Enums.EventType.Write, Time = eve.FileInf.LastWriteTime });
                }
            }
            else
            {
                doc.DocEvents.Add(new DocEvent() { Type = Enums.EventType.Write, Time = eve.FileInf.LastWriteTime });
            }
        }