Example #1
0
        static void MovHandler(String file, SearchLocation config, Archive archive)
        {
            //DateTime createTime = File.GetCreationTime(file);

            //Archive(file, createTime, archive);
        }
Example #2
0
        static void JpegHandler(String file, SearchLocation config, Archive archive)
        {
            DateTime createTime = File.GetCreationTime(file);

            using (FileStream stream = File.OpenRead(file))
            using (Bitmap image = (Bitmap)Bitmap.FromStream(stream))
            {
                createTime = TagParser.Parse<ExifTags>(image.PropertyItems.ToList())
                    .FileChangeDateTime
                    .GetValueOrDefault(createTime);
            }

            Archive(file, createTime, archive);
        }
Example #3
0
        private static void Archive(String file, DateTime createTime, Archive archive)
        {
            String ext = Path.GetExtension(file);
            String folder = createTime.GetFolderName();
            String targetDir = Path.Combine(archive.DestinationFullPath, folder);
            String targetFile = Path.Combine(targetDir,
                $"{createTime.ToString("MM-dd-yyyy")}_{DateTime.UtcNow.Ticks.ToString()}{ext}");

            MD5 hasher = MD5.Create();

            // Copy the file if it doesnt already exist.
            try
            {
                // We might need to create the directory.
                CreateDirectory(targetDir);

                String hash = null;
                using (FileStream stream = File.OpenRead(file))
                {
                    hash = Convert.ToBase64String(hasher.ComputeHash(stream));
                }

                // Does this file already exist in the databse?
                if(!MediaFile.Exists(hash))
                {
                    using (OrganizerDatabaseContext context = new OrganizerDatabaseContext())
                    {
                        MediaFile mediaFile = new MediaFile();
                        mediaFile.ArchiveDateTime = mediaFile.CreatedDateTime = DateTime.UtcNow;
                        mediaFile.ContentHash = hash;
                        mediaFile.OriginalFileName = file;
                        mediaFile.TargetFileName = targetFile;

                        context.MediaFiles.Add(mediaFile);

                        context.SaveChanges();
                    }
                }
                else
                {
                    // Overwrite it if we can.
                    MediaFile existingFile = MediaFile.GetByHash(hash);
                    
                    if(existingFile != null) // Should never be the case.
                    {
                        if(!File.Exists(existingFile.TargetFileName))
                        {
                            targetFile = existingFile.TargetFileName;
                        }
                        else
                        {
                            Console.WriteLine($"Skipping: {file}.");
                        }
                    }
                }

                // Copy it over.
                try
                {
                    Console.WriteLine($"Archiving: {file}.");
                    File.Copy(file, targetFile);
                }
                catch
                {
                    // Delete the media file if we couldnt copy it over.
                    MediaFile.DeleteByHash(hash);
                }
            }
            catch (Exception e)
            {
            }
        }