Example #1
0
File: Util.cs Project: wcchh/fSort
        public static EXIF GetExif(string filePathname)
        //public static DateTime? DateTaken(string filePathname)
        {
            //return DateTaken(System.Drawing.Image.FromFile(filePathname));
            EXIF exif = new EXIF();

            System.Drawing.Image img = null;

            try
            {
                img = System.Drawing.Image.FromFile(filePathname);
                exif.ExifDTOriginal = DateTaken(img, filePathname);
                if (exif.ExifDTOriginal == null)
                {
                    Log.Debug(filePathname + " has NO EXIF data");
                }
                else
                {
                    int Model = 0x0110; //Equipment model
                                        //0x010F;//Equipment manufacturer : Company
                    if (img.PropertyIdList.Contains(Model))
                    {
                        string modelTag = System.Text.Encoding.ASCII.GetString(img.GetPropertyItem(Model).Value);
                        modelTag            = modelTag.TrimEnd('\0');
                        modelTag            = modelTag.TrimEnd();
                        exif.EquipmentModel = modelTag;

                        //Console.Write("Model: " + modelTag);
                        //if (modelTag.IndexOf("9GF00") >= 0)
                        //{
                        //    Console.WriteLine(".<= " + filePathname);
                        //}
                        //else Console.WriteLine(".");
                    }
                }
            }
            catch (OutOfMemoryException e)
            {
                Log.Error("[OutOfMemoryException] " + filePathname + " : " + e.Message);
                //exif.ExifDTOriginal = null;
                exif = null;
            }
            finally
            {
                if (img != null)
                {
                    img.Dispose();
                }
            }

            return(exif);
        }
Example #2
0
File: Util.cs Project: wcchh/fSort
        public static bool ifHasTheSameFile(string src, string tar, bool isBinaryChk = false)
        {
            bool isTheSame = false;

            if (src == tar)
            {
                isTheSame = true;
            }
            else
            {
                if (File.Exists(tar))
                {
                    if (isBinaryChk)
                    {
                        //isTheSame = Util.isTheSameFileByCHKBinary(src, tar);
                        isTheSame = Util.isTheSameFileByCHKHash(src, tar);
                    }
                    else
                    {
                        FileInfo srcFi = new System.IO.FileInfo(src);
                        FileInfo tarFi = new System.IO.FileInfo(tar);
                        //compare file size
                        if (srcFi.Length == tarFi.Length && srcFi.LastWriteTime == tarFi.LastWriteTime)
                        {
                            isTheSame = true;

                            if (EXIF.FileExtensions.Contains(srcFi.Extension.ToLowerInvariant()))
                            {
                                //compare date-time exif
                                EXIF srcExif = Util.GetExif(src);
                                EXIF tarExif = Util.GetExif(tar);
                                if (srcExif != null && tarExif != null)
                                {
                                    if (srcExif.ExifDTOriginal != null && tarExif.ExifDTOriginal != null)
                                    {
                                        if (!srcExif.ExifDTOriginal.Equals(tarExif.ExifDTOriginal))
                                        {
                                            isTheSame = false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(isTheSame);
        }
Example #3
0
        // 1. Search file with filename extension list
        // 2. Rebuild target file by format and exif data
        // 3. Search target location, create folder if not exist (Reference source folder name)
        // 4. Check file duplication, then ...?
        //        duplication rule: same name with size and time, may binary.
        //        move to duplication folder, add dup number with dup the dup files.
        // 5. Move /Copy file to target location
        // 6. [Move] also check and remove the empty folder. (Remove Thumb.db picasa.ini)
        public void Run(RunOption sc)
        {
            if (project == null)
            {
                return;
            }
            //project = new UserProject();
            int          nFileCheckCount = 0, nFileActCount = 0;
            MyFolderInfo folderInfo = new MyFolderInfo();

            StreamWriter swDamaged = new StreamWriter(sc.Report_DamagedFilePathame);

            swDamaged.WriteLine("[Start at " + DateTime.Now + "]");
            StreamWriter swIgnored = new StreamWriter(sc.Report_IgnoredFilePathame);

            swIgnored.WriteLine("[Start at " + DateTime.Now + "]");
            swDamaged.AutoFlush = true;
            swIgnored.AutoFlush = true;

            StreamWriter swCopyFailed = null;

            if (project.isContentConfirmByComparedBinary && (project.fAction == UserProject.FileAction.Copy || project.fAction == UserProject.FileAction.CopyThenDelete))
            {
                swCopyFailed = new StreamWriter(sc.Report_CopyFailedFilePathame);
                swCopyFailed.WriteLine("[Start at " + DateTime.Now + "]");
                swCopyFailed.AutoFlush = true;
            }

            ForeachFolderFile((fi) => {
                //ProgressUpdate.Invoke(this, fi.FullName);
                nFileCheckCount++;
                string sourceFile      = fi.FullName;
                string destinationFile = string.Empty, destinationDupFile = string.Empty, FileState = string.Empty;
                string CameraModel     = string.Empty;
                DateTime fileDateTime  = new DateTime();

                //support file Extensions
                //if (Array.IndexOf(project.imageFileExts, fi.Extension.ToLower()) >= 0)
                if (project.imageFileExts.Contains(fi.Extension.ToLowerInvariant()))
                {
                    fileDateTime = fi.LastWriteTime;

                    if (EXIF.FileExtensions.Contains(fi.Extension.ToLowerInvariant()))
                    {
                        EXIF exif = Util.GetExif(fi.FullName);
                        if (exif == null)
                        {
                            fileDateTime = new DateTime(); //Skip act with this file.
                            FileState    = "檔案損毀";
                            swDamaged.WriteLine(fi.FullName);
                            sc.Report_DamagedFileCount++;
                        }
                        else
                        {
                            CameraModel = exif.EquipmentModel;
                            DateTime?dt = exif.ExifDTOriginal;
                            if (dt != null)
                            {
                                fileDateTime = (DateTime)dt;
                            }

                            FileState = fileDateTime.ToString();
                        }
                    }
                }
                else if (project.videoFileExts.Contains(fi.Extension.ToLowerInvariant()))
                //if (Array.IndexOf(project.videoFileExts, fi.Extension.ToLower()) >= 0)
                {
                    fileDateTime = fi.LastWriteTime;
                    //Console.WriteLine(">" + fi.FullName + fi.LastWriteTime.ToLocalTime().ToString());
                    //} else if (fi.Name == "Info")
                    //{
                    //    //Merge to target .. But we don't know the target path!
                }
                else
                {
                    FileState = "忽略";
                    swIgnored.WriteLine(fi.FullName);
                    sc.Report_IgnoredFileCount++;
                }

                if (!fileDateTime.Equals(new DateTime()))
                {
                    //ProgressUpdate.Invoke(this, fi.FullName);
                    //Destination file folder and file name.
                    destinationFile        = BuildDestinationFullFilePathname(fi, fileDateTime, project);
                    string destinationPath = System.IO.Path.GetDirectoryName(destinationFile);

                    //File Action
                    switch (project.fAction)
                    {
                    case UserProject.FileAction.CopyThenDelete:
                    case UserProject.FileAction.Copy:
                        bool isCopySuccess = false;
                        if (!Util.ifHasTheSameFile(sourceFile, destinationFile, project.isTheSameContentByComparedBinary))
                        {
                            if (!Directory.Exists(destinationPath))
                            {
                                Directory.CreateDirectory(destinationPath);    //create if folder not exist
                            }

                            System.IO.File.Copy(sourceFile, destinationFile);
                            Util.GetHash_WithGenToHashInfoFile(destinationFile);
                            isCopySuccess = true;
                        }

                        AddFolderInfo(folderInfo, sourceFile, destinationFile);

                        if (isCopySuccess)
                        {
                            if (project.isContentConfirmByComparedBinary)
                            {
                                if (!Util.ifHasTheSameFile(sourceFile, destinationFile, true))
                                {
                                    //Result is not the same!
                                    isCopySuccess = false;
                                    if (swCopyFailed != null)
                                    {
                                        swCopyFailed.WriteLine(sourceFile);
                                        sc.Report_CopyFailedFileCount++;
                                    }
                                }
                            }
                            if (project.fAction == UserProject.FileAction.CopyThenDelete && isCopySuccess)
                            {
                                System.IO.File.Delete(sourceFile);
                            }
                        }
                        break;

                    case UserProject.FileAction.Move:
                        if (Util.ifHasTheSameFile(sourceFile, destinationFile, project.isTheSameContentByComparedBinary))
                        {
                            //get Destination Duplicated foder name path
                            string phase2   = destinationFile.Substring(project.targetRootPathname.Length);
                            destinationFile = project.targetRootPathname + @"\Dup" + phase2;
                            destinationPath = System.IO.Path.GetDirectoryName(destinationFile);    //Update new path
                        }
                        if (!Directory.Exists(destinationPath))
                        {
                            Directory.CreateDirectory(destinationPath);    //create if folder not exist
                        }

                        MoveWithoutReplace(sourceFile, destinationFile, (CameraModel == string.Empty) ? "INCREASENUMBER" : CameraModel);
                        AddFolderInfo(folderInfo, sourceFile, destinationFile);
                        break;

                    case UserProject.FileAction.ListOnly:
                    default:
                        break;
                    }
                    nFileActCount++;
                    FileState = destinationFile;
                }
                ProgressUpdate.Invoke(this, sourceFile + PREDEF._ACTIONSIGNSTRING_ + FileState);

                //Leave progress controlable.
                bool ret = true;
                switch (sc.Step_ForceProgressTo)
                {
                case "pause":
                    do
                    {
                        Thread.Sleep(1000);
                    } while (sc.Step_ForceProgressTo == "pause");
                    break;

                case "resume":
                    break;

                case "stop":
                    ret = false;
                    break;
                }
                return(ret);
            });

            string FileEndStr = "[End at " + DateTime.Now + "]";

            swDamaged.WriteLine(FileEndStr);
            swDamaged.Close();
            swDamaged.Dispose();
            swIgnored.WriteLine(FileEndStr);
            swIgnored.Close();
            swIgnored.Dispose();
            if (swCopyFailed != null)
            {
                swCopyFailed.WriteLine(FileEndStr);
                swCopyFailed.Close();
                swCopyFailed.Dispose();
            }

            ProgressUpdate.Invoke(this, string.Format(@"Total Files: {0}, Total Act Filecount: {1}",
                                                      nFileCheckCount,
                                                      nFileActCount));
        }