rawfilewizard

git clone https://git.clttr.info/rawfilewizard.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

RawFile.cs (8806B)


      1 using SilkypixFileMover.Helpers;
      2 using SilkypixFileMover.Properties;
      3 using System;
      4 using System.Collections.Generic;
      5 using System.IO;
      6 using System.Linq;
      7 
      8 namespace SilkypixFileMover.Objects
      9 {
     10     internal class RawFile : IEquatable<RawFile>
     11     {
     12         #region Properties
     13 
     14         internal string RawFileName
     15         {
     16             get;
     17             private set;
     18         }
     19 
     20         internal string RawFileFullPath
     21         {
     22             get
     23             {
     24                 return Path.Combine( RawFileSourcePath, RawFileName );
     25             }
     26         }
     27 
     28         internal string RawFileSourcePath
     29         {
     30             get;
     31             private set;
     32         }
     33 
     34         internal string Size
     35         {
     36             get;
     37             private set;
     38         }
     39 
     40         internal DateTime TimeStamp
     41         {
     42             get;
     43             private set;
     44         }
     45 
     46         private IEnumerable<string> jpegSourceFileNamesWithSubfolder
     47         {
     48             get
     49             {
     50                 List<string> jpegFileList = new List<string>();
     51 
     52                 // JPEGs im definierten Unterordner
     53                 foreach ( string fileExt in Settings.Default.JpegFileExtensions.Split( '|' ) )
     54                 {
     55                     jpegFileList.Add( Path.Combine( Settings.Default.JpegSubFolderName,
     56                         string.Format( "{0}.{1}", Path.GetFileNameWithoutExtension( RawFileName ), fileExt ) ) );
     57                 }
     58 
     59                 // nach "Dateien die so ähnlich heißen" suchen
     60                 if ( Settings.Default.IncludeSimilarFiles )
     61                 {
     62                     string[] similarFiles = Directory.GetFiles(
     63                         Path.Combine( RawFileSourcePath, Settings.Default.JpegSubFolderName ),
     64                         string.Format( "{0}{1}*", Path.GetFileNameWithoutExtension( RawFileName ), Settings.Default.NameAdditionChar ) );
     65 
     66                     if ( similarFiles.Length > 0 )
     67                     {
     68                         foreach ( string file in similarFiles )
     69                         {
     70                             jpegFileList.Add( Path.Combine( Settings.Default.JpegSubFolderName,
     71                                     Path.GetFileName( file ) ) );
     72                         }
     73                     }
     74                 }
     75 
     76                 // auch JPEGs die direkt neben dem RAW liegen
     77                 // TODO: führt zu Problemen wenn schon ein normales JPEG aus dem Unterordner da ist - rename?
     78                 //foreach ( string fileExt in Settings.Default.JpegFileExtensions.Split( '|' ) )
     79                 //{
     80                 //    jpegFileList.Add( Path.Combine( string.Format( "{0}.{1}", Path.GetFileNameWithoutExtension( RawFileName ), fileExt ) ) );
     81                 //}
     82 
     83                 return jpegFileList;
     84             }
     85         }
     86 
     87         private IEnumerable<string> sidecarFileNamesWithSubfolder
     88         {
     89             get
     90             {
     91                 List<string> sidecarPixFiles = new List<string>();
     92                 foreach ( string extension in Settings.Default.SidecarFileExtensions.Split( '|' ) )
     93                 {
     94                     string[] sidecarFiles = Directory.GetFiles(
     95                         Path.Combine(RawFileSourcePath, Settings.Default.SidecarFileSubFolderName),
     96                         string.Format("{0}.*.{1}", Path.GetFileNameWithoutExtension(RawFileName), extension));
     97 
     98                     if (sidecarFiles.Length > 0)
     99                     {
    100                         foreach (string file in sidecarFiles)
    101                         {
    102                             sidecarPixFiles.Add(Path.Combine(Settings.Default.SidecarFileSubFolderName,
    103                                     Path.GetFileName(file)));
    104                         }
    105                     }
    106                 }
    107 
    108                 return sidecarPixFiles;
    109             }
    110         }
    111 
    112         #endregion
    113 
    114         internal RawFile( string filename )
    115         {
    116             string fileExt = Path.GetExtension( filename ).ToLower().TrimStart( '.' ).ToLowerInvariant();
    117 
    118             if ( fileExt.Equals( Settings.Default.RawFileExtension, StringComparison.OrdinalIgnoreCase ) )
    119             {
    120                 RawFileName = Path.GetFileName( filename );
    121                 RawFileSourcePath = Path.GetDirectoryName( filename );
    122             }
    123 
    124             if ( Settings.Default.JpegFileExtensions.Split( '|' ).Any( p => p.ToLowerInvariant() == fileExt ) )
    125             {
    126                 string fileWithoutExt = Path.GetFileNameWithoutExtension( filename );
    127                 int underscorePos;
    128                 if ( ( underscorePos = fileWithoutExt.IndexOf( Settings.Default.NameAdditionChar ) ) > 0 )
    129                 {
    130                     fileWithoutExt = fileWithoutExt.Substring( 0, underscorePos );
    131                 }
    132                 RawFileName = string.Format( "{0}.{1}", fileWithoutExt, Settings.Default.RawFileExtension );
    133                 if ( !string.IsNullOrWhiteSpace( Settings.Default.JpegSubFolderName ) )
    134                 {
    135                     RawFileSourcePath = Path.GetDirectoryName( filename ).Replace( Settings.Default.JpegSubFolderName, string.Empty );
    136                 }
    137                 else
    138                 {
    139                     RawFileSourcePath = Path.GetDirectoryName( filename );
    140                 }
    141             }
    142 
    143             FileInfo fi = new FileInfo( RawFileFullPath );
    144             if ( fi.Exists )
    145             {
    146                 TimeStamp = fi.CreationTime;
    147                 Size = string.Format( "{0} kB", fi.Length / 1024 );
    148             }
    149             else
    150             {
    151                 Size = "n/a";
    152             }
    153         }
    154 
    155         internal void Delete()
    156         {
    157             FileHelpers.SaveDelete( RawFileFullPath );
    158 
    159             FileHelpers.DeleteAllFilesInList( sidecarFileNamesWithSubfolder, RawFileSourcePath );
    160             FileHelpers.DeleteAllFilesInList( jpegSourceFileNamesWithSubfolder, RawFileSourcePath );
    161         }
    162 
    163         internal void Move( string destinationFolder )
    164         {
    165             if ( !Directory.Exists( destinationFolder ) )
    166             {
    167                 // TODO: DirectoryInfo auswerten, falls Anlage schiefgeht
    168                 DirectoryInfo dirInfo = Directory.CreateDirectory( destinationFolder );
    169             }
    170 
    171             string sidecarFilesDestFolder = Path.Combine( destinationFolder, Settings.Default.SidecarFileSubFolderName );
    172             if ( !Directory.Exists(sidecarFilesDestFolder) )
    173             {
    174                 // TODO: DirectoryInfo auswerten
    175                 DirectoryInfo dirInfo = Directory.CreateDirectory(sidecarFilesDestFolder);
    176             }
    177 
    178             string jpegDestFolder = Path.Combine( destinationFolder, Settings.Default.JpegSubFolderName );
    179             if ( !Directory.Exists( jpegDestFolder ) )
    180             {
    181                 // TODO: DirectoryInfo auswerten
    182                 DirectoryInfo dirInfo = Directory.CreateDirectory( jpegDestFolder );
    183             }
    184 
    185             string rawFileFullDestPath = Path.Combine( destinationFolder, RawFileName );
    186 
    187             // wenn eine Datei nicht verschoben werden kann, alles wieder zurück
    188             try
    189             {
    190                 File.Move( RawFileFullPath, rawFileFullDestPath );
    191 
    192                 FileHelpers.MoveAllFilesInList( jpegSourceFileNamesWithSubfolder, RawFileSourcePath, jpegDestFolder, false );
    193                 FileHelpers.MoveAllFilesInList( sidecarFileNamesWithSubfolder, RawFileSourcePath, sidecarFilesDestFolder, false);
    194             }
    195             catch ( Exception )
    196             {
    197                 FileHelpers.SaveMove( rawFileFullDestPath, RawFileFullPath );
    198 
    199                 FileHelpers.MoveAllFilesInList( jpegSourceFileNamesWithSubfolder, Directory.GetParent(jpegDestFolder).FullName, RawFileSourcePath, true );
    200                 FileHelpers.MoveAllFilesInList( sidecarFileNamesWithSubfolder, sidecarFilesDestFolder, RawFileSourcePath, true );
    201 
    202                 throw;
    203             }
    204         }
    205         
    206         #region IEquatable-Implementierung
    207 
    208         public override bool Equals( object obj )
    209         {
    210             if ( obj == null )
    211             {
    212                 return false;
    213             }
    214 
    215             RawFile objAsPart = obj as RawFile;
    216             if ( objAsPart == null )
    217             {
    218                 return false;
    219             }
    220             else
    221             {
    222                 return Equals( objAsPart );
    223             }
    224         }
    225 
    226         /// <summary>
    227         /// Der Hashcode entspricht dem Hashcode des kompletten Dateipfades
    228         /// </summary>
    229         /// <returns>Hashcode des Objektes</returns>
    230         public override int GetHashCode()
    231         {
    232             return RawFileFullPath.GetHashCode();
    233         }
    234 
    235         public bool Equals( RawFile other )
    236         {
    237             if ( other == null )
    238             {
    239                 return false;
    240             }
    241             return ( GetHashCode() == other.GetHashCode() );
    242         }
    243 
    244         #endregion
    245     }
    246 }