Ejemplo n.º 1
0
        private bool?IsSamePhoto(PhotoInfo photoInfo, string otherFilePath)
        {
            try
            {
                PhotoInfo otherPhotoInfo = this.GetOrCreatePhotoInfo(otherFilePath);
                if (photoInfo.FileHash.Length != otherPhotoInfo.FileHash.Length)
                {
                    return(false);
                }

                for (int i = 0; i < photoInfo.FileHash.Length; i++)
                {
                    if (photoInfo.FileHash[i] != otherPhotoInfo.FileHash[i])
                    {
                        return(false);
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        private void MovePhotoToDuplicatesDirectory(PhotoInfo photoInfo)
        {
            // I use the file hash to put duplicates into the same directory
            string fileHash = BitConverter.ToString(photoInfo.FileHash).Replace("-", string.Empty);

            string destinationDirectoryPath = Path.Combine(this._duplicatesDirectoryPath, fileHash);

            string destinationFilePath;

            int conflictRevision = 1;

            while (true)
            {
                destinationFilePath = Path.Combine(destinationDirectoryPath, GetPhotoDestinationFileName(photoInfo, conflictRevision));

                if (File.Exists(destinationFilePath))
                {
                    conflictRevision++;
                }
                else
                {
                    this.MovePhoto(photoInfo.FileName, destinationFilePath);
                    break;
                }
            }
        }
Ejemplo n.º 3
0
        private void CachePhotoInfo(PhotoInfo photoInfo)
        {
            if (this._photoInfoCache.Count > 10000)
            {
                this._photoInfoCache.Clear();
            }

            this._photoInfoCache[photoInfo.FileName] = photoInfo;
        }
Ejemplo n.º 4
0
        private void OrganizeDirectory(string directoryPath, CancellationToken cancellationToken)
        {
            if (string.Equals(directoryPath, this._duplicatesDirectoryPath, StringComparison.InvariantCultureIgnoreCase))
            {
                this.RaiseDirectorySkipped(directoryPath);
                return;
            }

            this.RaiseDirectoryStarted(directoryPath);

            string[] filePaths = Directory.GetFiles(directoryPath);
            foreach (string filePath in filePaths)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                RaiseFileStarted(filePath);
                if (PhotoInfo.IsPhotoFileExtension(Path.GetExtension(filePath)))
                {
                    try
                    {
                        PhotoInfo photoInfo = this.GetOrCreatePhotoInfo(filePath);
                        PlacePhoto(photoInfo);
                    }
                    catch (Exception ex)
                    {
                        RaiseFileError(filePath, ex.Message);
                    }
                }
                else
                {
                    string fileName = Path.GetFileName(filePath);
                    if (!fileName.Equals("Thumbs.db", StringComparison.InvariantCultureIgnoreCase) &&
                        !fileName.Equals("Desktop.ini", StringComparison.InvariantCultureIgnoreCase))
                    {
                        RaiseFileSkipped(filePath);
                    }
                }

                RaiseFileFinished(filePath);
            }

            string[] childDirectoryNames = Directory.GetDirectories(directoryPath);
            foreach (string childDirectoryName in childDirectoryNames)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                OrganizeDirectory(childDirectoryName, cancellationToken);
            }

            this.RaiseDirectoryFinished(directoryPath);
        }
Ejemplo n.º 5
0
        private static string GetPhotoDestinationDirectoryName(string directoryPath, PhotoInfo photoInfo)
        {
            DateTime photoDateTime = photoInfo.DateTime;

            string year  = photoDateTime.ToString("yyyy");
            string month = photoDateTime.ToString("MM");
            string destinationDirectoryPath = Path.Combine(directoryPath, year);

            destinationDirectoryPath = Path.Combine(destinationDirectoryPath, month);
            return(destinationDirectoryPath);
        }
Ejemplo n.º 6
0
        private PhotoInfo GetOrCreatePhotoInfo(string filePath)
        {
            PhotoInfo photoInfo;

            if (!this.TryGetCachedPhotoInfo(filePath, out photoInfo))
            {
                photoInfo = new PhotoInfo(filePath);
                this.CachePhotoInfo(photoInfo);
            }

            return(photoInfo);
        }
Ejemplo n.º 7
0
        private void PlacePhoto(PhotoInfo photoInfo)
        {
            string sourceFilePath      = string.Empty;
            string destinationFilePath = string.Empty;

            try
            {
                sourceFilePath = photoInfo.FileName;

                string destinationDirectoryPath = GetPhotoDestinationDirectoryName(this._organizedDirectoryPath, photoInfo);
                int    conflictRevision         = 1;

                while (true)
                {
                    destinationFilePath = Path.Combine(destinationDirectoryPath, GetPhotoDestinationFileName(photoInfo, conflictRevision));

                    // If the file is already in the right place, then I skip it
                    if (string.Equals(sourceFilePath, destinationFilePath, StringComparison.InvariantCultureIgnoreCase))
                    {
                        RaiseFileNotMoved(sourceFilePath);
                        break;
                    }

                    // I move duplicates into the duplicates directory
                    if (File.Exists(destinationFilePath))
                    {
                        if (IsSamePhoto(photoInfo, destinationFilePath) == true)
                        {
                            this.MovePhotoToDuplicatesDirectory(photoInfo);
                            this.RaiseDuplicateFileMoved(sourceFilePath, destinationFilePath);
                            break;
                        }
                        else
                        {
                            conflictRevision++;
                        }
                    }
                    else
                    {
                        this.MovePhoto(sourceFilePath, destinationFilePath);
                        this.RaiseFileMoved(sourceFilePath, destinationFilePath);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                RaiseFileError(sourceFilePath, ex.Message);
            }
        }
Ejemplo n.º 8
0
        private static string GetPhotoDestinationFileName(PhotoInfo photoInfo, int conflictRevision)
        {
            DateTime photoDateTime = photoInfo.DateTime;

            string year      = photoDateTime.ToString("yyyy");
            string month     = photoDateTime.ToString("MM");
            string day       = photoDateTime.ToString("dd");
            string hour      = photoDateTime.ToString("HH"); //24 hour clock
            string minute    = photoDateTime.ToString("mm");
            string revision  = conflictRevision.ToString("D3");
            string extension = Path.GetExtension(photoInfo.FileName);

            if (conflictRevision > 0)
            {
                return(string.Format("IMG {0}-{1}-{2} {3}.{4}.{5}{6}",
                                     year, month, day, hour, minute, revision, extension));
            }
            else
            {
                return(string.Format("IMG {0}-{1}-{2} {3}.{4}{5}",
                                     year, month, day, hour, minute, extension));
            }
        }
Ejemplo n.º 9
0
 private bool TryGetCachedPhotoInfo(string filePath, out PhotoInfo photoInfo)
 {
     return(this._photoInfoCache.TryGetValue(filePath, out photoInfo));
 }