Esempio n. 1
0
        static int Main(string[] args)
        {
            MRArguments argument = UsageChecker.checkAndBuildArgument(args);

            if (MRStatusCode.CODE_SUCCESS != argument.statusCode)
            {
                return(argument.statusCode);
            }

            int processedCounter = 0;
            int totalCount       = argument.targetFiles.Count;

            Parallel.ForEach(argument.targetFiles, (file) => {
                int currentProcessCount = Interlocked.Increment(ref processedCounter);
                MediaProcessingContext processingContext = new MediaProcessingContext(file);

                if (!processingContext.isSupportedMimeType)
                {
                    Console.WriteLine("{2}/{3}  - {0} is not supported file type [{1}]", processingContext.targetFile.Name, processingContext.mimeType, currentProcessCount, totalCount);
                    return;
                }

                MediaFileService.checkOldestDateTime(processingContext);
                MediaFileService.createDestinationDir(argument, processingContext);
                MediaFileService.moveToDestination(processingContext);
                if (!processingContext.targetFile.FullName.Equals(processingContext.movedFile.FullName))
                {
                    MediaFileService.editFileDates(processingContext);
                    Console.WriteLine("{3}/{4}  - {0} -> {1} and set its all meta dates to [{2}]", processingContext.targetFile.Name, processingContext.movedFile.Name, processingContext.oldestFileDate, currentProcessCount, totalCount);
                    return;
                }
                Console.WriteLine("{1}/{2}  - {0} rearrangement failed.", processingContext.targetFile.Name, processingContext.movedFile.Name, processingContext.oldestFileDate, currentProcessCount, totalCount);
            });
            return(MRStatusCode.CODE_SUCCESS);
        }
Esempio n. 2
0
        public static void editFileDates(MediaProcessingContext context)
        {
            ShellFile extendedFile = ShellFile.FromFilePath(context.movedFile.FullName);

            File.SetCreationTime(context.movedFile.FullName, context.oldestFileDate);
            File.SetLastWriteTime(context.movedFile.FullName, context.oldestFileDate);
            File.SetLastAccessTime(context.movedFile.FullName, context.oldestFileDate);

            ShellPropertyWriter writer = null;

            try {
                writer = extendedFile.Properties.GetPropertyWriter();
                if (context.mimeType.StartsWith("image"))
                {
                    writer.WriteProperty(SystemProperties.System.Photo.DateTaken, context.oldestFileDate);
                }
                if (context.mimeType.StartsWith("video"))
                {
                    writer.WriteProperty(SystemProperties.System.Media.DateEncoded, context.oldestFileDate);
                }
            } catch (Exception ignored) {
                Console.WriteLine("ERROR : {0}`s metadata writing failed", context.movedFile.Name);
            } finally {
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }
Esempio n. 3
0
        public static void createDestinationDir(MRArguments argument, MediaProcessingContext context)
        {
            String destinationParent = argument.dirForMove.FullName + Path.DirectorySeparatorChar + context.oldestFileDate.ToString("yyyyMMdd");

            if (!File.Exists(destinationParent))
            {
                Directory.CreateDirectory(destinationParent);
            }
            context.destinationParent = destinationParent;
        }
Esempio n. 4
0
 private static DateTime?getDateTimeFromMetadata(MediaProcessingContext context)
 {
     if (context.mimeType.StartsWith("image"))
     {
         return(context.extendedTargetFile.Properties.System.Photo.DateTaken.Value);
     }
     if (context.mimeType.StartsWith("video"))
     {
         return(context.extendedTargetFile.Properties.System.Media.DateEncoded.Value);
     }
     return(null);
 }
Esempio n. 5
0
        public static void moveToDestination(MediaProcessingContext context)
        {
            bool   created;
            int    tries       = 0;
            String destination = context.destinationParent + Path.DirectorySeparatorChar + context.targetFile.Name;

            do
            {
                created = moveTo(context.targetFile.FullName, destination, tries++);
                if (!created)
                {
                    destination = context.destinationParent + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(context.targetFile.Name) + "_" + tries + Path.GetExtension(context.targetFile.Name);
                    Console.WriteLine("WARN : {0} move failed. retrying and renamed to {1}", context.targetFile.Name, Path.GetFileName(destination));
                }
                else
                {
                    context.movedFile = new FileInfo(destination);
                }
            } while(!created);
        }
Esempio n. 6
0
        public static void checkOldestDateTime(MediaProcessingContext context)
        {
            DateTime dateCreated      = File.GetCreationTime(context.targetFile.FullName);
            DateTime?dateModified     = File.GetLastWriteTime(context.targetFile.FullName);
            DateTime?dateFromFileName = getDateTimeFromFileName(context.targetFile.Name);
            DateTime?dateFromMetadata = getDateTimeFromMetadata(context);


            DateTime baseDateTime = new DateTime(1995, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            DateTime minDateTime  = dateCreated;

            // 95 년 보다 큰 데이터 중에서 가장 작은
            List <DateTime> dates = new List <DateTime>()
            {
                dateCreated
            };

            if (dateModified != null && dateModified.HasValue && DateTime.Compare(baseDateTime, dateModified.Value) < 1)
            {
                dates.Add(dateModified.Value);
            }
            if (dateFromFileName != null && dateFromFileName.HasValue && DateTime.Compare(baseDateTime, dateFromFileName.Value) < 1)
            {
                dates.Add(dateFromFileName.Value);
            }
            if (dateFromMetadata != null && dateFromMetadata.HasValue && DateTime.Compare(baseDateTime, dateFromMetadata.Value) < 1)
            {
                dates.Add(dateFromMetadata.Value);
            }

            foreach (DateTime date in dates)
            {
                if (DateTime.Compare(minDateTime, date) > 0)
                {
                    minDateTime = date;
                }
            }

            context.oldestFileDate = minDateTime;
        }