Exemple #1
0
        public static string Main(String filePath)
        {
            /*
             * J Vincent
             * creates checksum for given file
             *
             *
             */

            string checkfile = filePath;

            byte[] hashValue = default;
            string retVal    = null;

            //check file valid
            if (File.Exists(checkfile))
            {
                using (SHA256 mySHA256 = SHA256.Create())
                {
                    try
                    {
                        FileStream fileStream = new FileStream(checkfile, FileMode.Open);
                        // Be sure it's positioned to the beginning of the stream.
                        fileStream.Position = 0;
                        // Compute the hash of the fileStream.
                        hashValue = mySHA256.ComputeHash(fileStream);
                        // Close the file.
                        fileStream.Close();

                        retVal = CheckSum.ChecksumToStr(hashValue);
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }
            return(retVal);
        }
        public string fileMetaKeyword;          // string with comma delimted keywords. ideally from file keywords



        public void GetFileInfo(string filePath)
        {
            try
            {
                //Is it a valid file
                if (File.Exists(filePath))
                {
                    FileAttributes attributes = File.GetAttributes(filePath);

                    //Is it a directory
                    this.fileDir = false;
                    if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
                    {
                        this.fileDir = true;
                    }
                    //is it a system file
                    this.fileSystem = false;
                    if ((attributes & FileAttributes.System) == FileAttributes.System)
                    {
                        this.fileSystem = true;
                    }
                    if (this.fileDir == false)
                    {
                        // Read and Write:
                        if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                        {
                            this.fileMetaReadOnly = "Yes";
                        }
                        else
                        {
                            this.fileMetaReadOnly = "No";
                        }
                        // hidden file
                        if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                        {
                            this.fileMetaHidden = "Yes";
                        }
                        else
                        {
                            this.fileMetaHidden = "No";
                        }

                        // this needs to be the path relative to the catalogue

                        this.fileLocation = Path.GetDirectoryName(filePath).Replace(Globals.curCatPath, "");

                        var file = ShellFile.FromFilePath(filePath);
                        //date created
                        this.fileMetaDateCreated = file.Properties.System.DateCreated.Value.ToString();
                        //date modified
                        this.fileMetaDateModified = file.Properties.System.DateModified.Value.ToString();
                        //type of file (extension)
                        this.fileMetaType = file.Properties.System.FileExtension.Value;
                        //filename
                        this.fileName = file.Properties.System.FileName.Value;
                        //filesize in bytes
                        this.fileMetaSize = file.Properties.System.Size.Value.ToString();
                        //date taken (might be null)
                        this.fileMetaDateTaken = file.Properties.System.ItemDate.Value.ToString();
                        //file hash sha-256
                        this.fileHash = CheckSum.Main(filePath);


                        // the following need to be resolved before this app can properly go live

                        /* this will only work for pics and movies
                         * var directories = ImageMetadataReader.ReadMetadata(filePath);
                         * var subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
                         * var dateTime = subIfdDirectory?.GetDescription(ExifDirectoryBase.TagDateTime);
                         */

                        //file authors
                        //file keywords

                        /* this doesn't work. Not sure why
                         * string[] fileMetaKeywords = file.Properties.System.Keywords.Value; //null
                         * this.fileMetaKeyword = String.Join(",", fileKeywords);
                         * string[] fileMetaAuthors = file.Properties.System.Author.Value;
                         * this.fileMetaAuthor = String.Join(",", fileMetaAuthors);
                         */
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #3
0
        private void CaptureFileInfo(TreeNode nodeTop)
        {
            /*
             * J vincent
             * goes through files in tree below catalogue root and captures data from files not already in catalogue
             * tests first to see if already in catalogue
             * and that not a system file or directory
             *
             */

            ReadFileInfo fileInfo = new ReadFileInfo();


            foreach (TreeNode nodeSub in nodeTop.Nodes)
            {
                fileInfo.GetFileInfo(nodeSub.FullPath);
                //need to pass full path including actual location so add globals.curcatpath
                int alreadyHere = CheckSum.CheckCheckSum(Globals.curCatPath + fileInfo.fileLocation, fileInfo.fileName);
                if ((fileInfo.fileSystem == false && fileInfo.fileDir == false && alreadyHere == 0))
                {
                    if (fileInfo.fileName != null)
                    {
                        CatalogueItemMetaUpdate intoCat = new CatalogueItemMetaUpdate();
                        //add file information to tbl Items
                        intoCat.AddItem(fileInfo.fileName, fileInfo.fileLocation, fileInfo.fileHash, fileInfo.fileOwner);
                        //get ItemID
                        int myItemID = intoCat.itemID;
                        //add meta data to tblMetaItems for each metaTitle if available
                        if (fileInfo.fileMetaType != null)
                        {
                            intoCat.AddMetaItem(myItemID, true, "TYPE OF FILE", "string", fileInfo.fileMetaType);
                        }
                        if (fileInfo.fileName != null)
                        {
                            intoCat.AddMetaItem(myItemID, true, "NAME", "string", fileInfo.fileName);
                        }
                        if (fileInfo.fileMetaSize != null)
                        {
                            intoCat.AddMetaItem(myItemID, true, "SIZE", "INT", fileInfo.fileMetaSize);
                        }
                        if (fileInfo.fileMetaDateCreated != null)
                        {
                            intoCat.AddMetaItem(myItemID, true, "CREATED", "DateTime", fileInfo.fileMetaDateCreated);
                        }
                        if (fileInfo.fileMetaDateModified != null)
                        {
                            intoCat.AddMetaItem(myItemID, true, "MODIFIED", "DateTime", fileInfo.fileMetaDateModified);
                        }
                        if (fileInfo.fileMetaReadOnly != null)
                        {
                            intoCat.AddMetaItem(myItemID, true, "READ ONLY", "Boolean", fileInfo.fileMetaReadOnly);
                        }
                        if (fileInfo.fileLocation != null)
                        {
                            intoCat.AddMetaItem(myItemID, true, "LOCATION", "string", fileInfo.fileLocation);
                        }
                        if (fileInfo.fileMetaHidden != null)
                        {
                            intoCat.AddMetaItem(myItemID, true, "HIDDEN", "Boolean", fileInfo.fileMetaHidden);
                        }
                        if (fileInfo.fileMetaDateTaken != null)
                        {
                            intoCat.AddMetaItem(myItemID, true, "DATE TAKEN", "DateTime", fileInfo.fileMetaDateTaken);
                        }
                        if (fileInfo.fileMetaKeyword != null)
                        {
                            intoCat.AddMetaItem(myItemID, true, "KEYWORDS", "string", fileInfo.fileMetaKeyword);
                        }
                        //update catalogue latest version
                        intoCat.SetCatalogueVer();
                        toolStripVersion.Text = "Cat ver: " + Globals.curCatVer;
                        //update change log
                        ChangeLog.Main("New File Added: " + fileInfo.fileName, myItemID);
                        filesUpdated++;
                    }
                }
                //recursively call this method again until entire tree covered
                CaptureFileInfo(nodeSub);
            }

            //or selected files in current structure
            //later
        }
Exemple #4
0
        private void treeViewCat_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            /*
             * J Vincent
             * name autogenerated by visual studio
             * This populates the tree view with the files in the folder
             *
             * it needs to be compared with the details of files stored in the catalogue
             * and highlight any discrepenacies
             * and hide the DACAT folder
             *
             */

            if (e.Node.Nodes.Count > 0)
            {
                if (e.Node.Nodes[0].Text == "..." && e.Node.Nodes[0].Tag == null)
                {
                    e.Node.Nodes.Clear();

                    //get the list of sub direcotires
                    string[] dirs = Directory.GetDirectories(e.Node.Tag.ToString());

                    foreach (string dir in dirs)
                    {
                        DirectoryInfo di   = new DirectoryInfo(dir);
                        TreeNode      node = new TreeNode(di.Name, 1, 2);
                        //if its the DACAT folder ignore
                        if (di.Name != "DACAT")
                        {
                            try
                            {
                                //keep the directory's full path in the tag for use later
                                node.Tag = dir;

                                //if the directory has sub directories add the place holder
                                if (di.GetDirectories().Count() > 0)
                                {
                                    node.Nodes.Add(null, "...", 0, 0);
                                }

                                foreach (var file in di.GetFiles())
                                {
                                    //check if file is already in db
                                    string   fileDets = file.DirectoryName;
                                    int      checkRes = CheckSum.CheckCheckSum(fileDets, file.Name);
                                    TreeNode n;
                                    switch (checkRes)
                                    {
                                    case 0:     //not in db
                                        n = new TreeNode(file.Name, 4, 4);
                                        break;

                                    case 1:     //in db, checksum wrong
                                        n = new TreeNode(file.Name, 5, 5);
                                        break;

                                    case 2:     //in db, all ok
                                        n = new TreeNode(file.Name, 6, 6);
                                        break;

                                    default:
                                        n = new TreeNode(file.Name, 4, 4);
                                        break;
                                    }
                                    n.Tag = CheckSum.itemID;
                                    node.Nodes.Add(n);
                                }
                            }
                            catch (UnauthorizedAccessException)
                            {
                                //display a locked folder icon
                                node.ImageIndex         = 3;
                                node.SelectedImageIndex = 3;
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "DirectoryLister",
                                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            finally
                            {
                                e.Node.Nodes.Add(node);
                            }
                        }
                    }
                    //add files of rootdirectory
                    DirectoryInfo rootDir = new DirectoryInfo(e.Node.Tag.ToString());
                    foreach (var file in rootDir.GetFiles())
                    {
                        //check if file is already in db
                        string   fileDets = file.DirectoryName;
                        int      checkRes = CheckSum.CheckCheckSum(fileDets, file.Name);
                        TreeNode n;
                        switch (checkRes)
                        {
                        case 0:     //not in db
                            n = new TreeNode(file.Name, 4, 4);
                            break;

                        case 1:     //in db, checksum wrong
                            n = new TreeNode(file.Name, 5, 5);
                            break;

                        case 2:     //in db, all ok
                            n = new TreeNode(file.Name, 6, 6);
                            break;

                        default:
                            n = new TreeNode(file.Name, 4, 4);
                            break;
                        }
                        n.Tag = CheckSum.itemID;
                        e.Node.Nodes.Add(n);
                    }

                    //add missing files to treeview - these are files in the database that aren't in the folder
                    List <int> missing = Catalogue.MissingItems();
                    foreach (int missingFile in missing)
                    {
                        string[] filenames = Catalogue.GetFileName(missingFile);
                        TreeNode n;
                        n     = new TreeNode(filenames[0], 7, 7);
                        n.Tag = missingFile;
                        e.Node.Nodes.Add(n);
                        //update change logs
                        ChangeLog.Main("Missing File: " + Globals.curCatPath + filenames[1] + "\\" + filenames[0], missingFile);
                    }
                    treeViewCat.ExpandAll();
                }
            }
        }