public void RetrieveMediaFiles(SourceConfiguration sourceConfig, IMediaQueue mediaQueue)
        {
            // if a destination directory not specified, error
            if (string.IsNullOrEmpty(sourceConfig.DestinationDirectory))
            {
                throw new InvalidOperationException("OneDrive Source requires destination directory -d");
            }

            mediaQueue.ReportProgress($"Connecting to OneDrive: {m_sourceName}");
            m_accessToken = NamedSource.GetOnedriveAccessToken(m_refreshToken);
            var queue = SelectFiles(sourceConfig, mediaQueue);

            DownloadMediaFiles(queue, sourceConfig, mediaQueue);
        }
Example #2
0
        static void ParseCommandLine(string[] args)
        {
            s_sourceConfiguration   = new SourceConfiguration();
            Program.s_photoFinisher = new PhotoFinisher();

            Program.s_photoFinisher.ProgressReported += ReportProgress;
            Program.s_photoFinisher.StatusReported   += ReportStatus;


            try
            {
                if (args.Length == 0)
                {
                    s_operation = Operation.ShowHelp;
                    return;
                }

                for (int i = 0; i < args.Length; ++i)
                {
                    switch (args[i].ToLowerInvariant())
                    {
                    case "-h":
                    case "-?":
                        s_operation = Operation.ShowHelp;
                        return;

                    case "-s":
                        if (s_mediaSource != null)
                        {
                            throw new Exception("CommandLine -s: Can only specify one source.");
                        }
                        s_mediaSource = new FileSource(NextArgument(args, ref i), false);
                        s_operation   = Operation.ProcessMediaFiles;
                        break;

                    case "-st":
                        if (s_mediaSource != null)
                        {
                            throw new Exception("CommandLine -st: Can only specify one source.");
                        }
                        s_mediaSource = new FileSource(NextArgument(args, ref i), true);
                        s_operation   = Operation.ProcessMediaFiles;
                        break;

                    case "-sdcim":
                        if (s_mediaSource != null)
                        {
                            throw new Exception("CommandLine -sdcim: Can only specify one source.");
                        }
                        s_mediaSource = new DcimSource();
                        s_operation   = Operation.ProcessMediaFiles;
                        break;

                    case "-sname":
                        if (s_mediaSource != null)
                        {
                            throw new Exception("CommandLine -sname: Can only specify one source.");
                        }
                        s_mediaSource = NamedSource.GetNamedSource(NextArgument(args, ref i));
                        s_operation   = Operation.ProcessMediaFiles;
                        break;

#if DEBUG
                    case "-copydcim":
                    {
                        Console.WriteLine("Copying test files.");
                        int n = DcimSource.CopyDcimTestFiles();
                        Console.WriteLine($"Copied {n} files from DCIM_Test to DCIM.");
                    }
                    break;
#endif

                    case "-selectafter":
                        s_sourceConfiguration.SelectAfter = NextArgumentAsDate(args, ref i)
                                                            .ResolveTimeZone(TimeZoneInfo.Local).Date;
                        break;

                    case "-selectincremental":
                        s_sourceConfiguration.SelectIncremental = true;
                        break;

                    case "-d":
                    {
                        string dst = NextArgument(args, ref i);
                        if (!Directory.Exists(dst))
                        {
                            throw new ArgumentException($"Destination folder '{dst}' does not exist.");
                        }
                        s_sourceConfiguration.DestinationDirectory = Path.GetFullPath(dst);
                        s_photoFinisher.DestinationDirectory       = s_sourceConfiguration.DestinationDirectory;
                    }
                    break;

                    case "-w":
                        s_waitBeforeExit = true;
                        break;

                    case "-sortby":
                        switch (NextArgument(args, ref i).ToLowerInvariant())
                        {
                        case "y":
                            s_photoFinisher.SortBy = DatePathType.Y;
                            break;

                        case "ym":
                            s_photoFinisher.SortBy = DatePathType.YM;
                            break;

                        case "ymd":
                            s_photoFinisher.SortBy = DatePathType.YMD;
                            break;

                        case "ymds":
                            s_photoFinisher.SortBy = DatePathType.YMDS;
                            break;

                        default:
                            throw new ArgumentException($"Unexpected value for -sortby: {args[i]}.");
                        }
                        break;

                    case "-sort":
                        s_photoFinisher.SortBy = DatePathType.YMD;
                        break;

                    case "-move":
                        s_sourceConfiguration.MoveFiles = true;
                        break;

                    case "-autorot":
                        s_photoFinisher.AutoRotate = true;
                        break;

                    case "-orderednames":
                        s_photoFinisher.SetOrderedNames  = true;
                        s_photoFinisher.SetMetadataNames = false;
                        break;

                    case "-filenamefrommetadata":
                        s_photoFinisher.SetMetadataNames = true;
                        s_photoFinisher.SetOrderedNames  = false;
                        break;

                    case "-metadatafromfilename":
                        s_photoFinisher.MetadataFromFilename = SetMode.SetIfEmpty;
                        break;

                    case "-metadatafromfilenameoverwrite":
                        s_photoFinisher.MetadataFromFilename = SetMode.SetAlways;
                        break;

                    case "-saveoriginalfn":
                    case "-saveoriginalfilename":
                        s_photoFinisher.SaveOriginalFilaname = true;
                        break;

                    case "-setuuid":
                        s_photoFinisher.SetUuid = true;
                        break;

                    case "-transcode":
                        s_photoFinisher.Transcode = true;
                        break;

                    case "-tag":
                        s_photoFinisher.AddKeywords.Add(NextArgument(args, ref i));
                        break;

                    case "-determinedate":
                        s_photoFinisher.AlwaysSetDate = true;
                        break;

                    case "-alltheway":
                        s_photoFinisher.AutoRotate = true;
                        if (!s_photoFinisher.SetMetadataNames)
                        {
                            s_photoFinisher.SetOrderedNames = true;
                        }
                        s_photoFinisher.SaveOriginalFilaname = true;
                        s_photoFinisher.SetUuid       = true;
                        s_photoFinisher.Transcode     = true;
                        s_photoFinisher.AlwaysSetDate = true;
                        break;

                    case "-deduplicate":
                        s_photoFinisher.DeDuplicate = true;
                        break;

                    case "-setdate":
                        s_photoFinisher.SetDateTo = NextArgumentAsDate(args, ref i)
                                                    .ResolveTimeZone(TimeZoneInfo.Local);
                        break;

                    case "-shiftdate":
                    {
                        string timeShift = NextArgument(args, ref i);

                        // If the next argument starts with a sign, then the shift amount is a simple timespan.
                        if (timeShift[0] == '+' || timeShift[0] == '-')
                        {
                            string   s = (timeShift[0] == '+') ? timeShift.Substring(1) : timeShift;
                            TimeSpan ts;
                            if (!TimeSpan.TryParse(s, System.Globalization.CultureInfo.InvariantCulture, out ts))
                            {
                                throw new ArgumentException($"Invalid value for -shiftDate '{timeShift}'.");
                            }

                            s_photoFinisher.ShiftDateBy = ts;
                        }

                        // Else, shift amount is the difference of two times
                        else
                        {
                            if (i + 1 >= args.Length)
                            {
                                throw new ArgumentException("-shiftDate requires two dates or one offset.");
                            }
                            string secondDate = NextArgument(args, ref i);

                            FileMeta.DateTag dtTarget;
                            if (!FileMeta.DateTag.TryParse(timeShift, out dtTarget))
                            {
                                throw new ArgumentException($"Invalid value for -shiftDate '{args[i]}'.");
                            }

                            FileMeta.DateTag dtSource;
                            if (!FileMeta.DateTag.TryParse(secondDate, out dtSource))
                            {
                                throw new ArgumentException($"Invalid value for -shiftDate '{args[i]}'.");
                            }

                            // Resolve timezone if it was ambiguous.
                            dtTarget.ResolveTimeZone(TimeZoneInfo.Local);
                            dtSource.ResolveTimeZone(TimeZoneInfo.Local);

                            // For whatever reason, they might have used different timezones. Take the difference between the UTC versions.
                            s_photoFinisher.ShiftDateBy = dtTarget.DateUtc.Subtract(dtSource.DateUtc);
                        }
                    }
                    break;

                    case "-settimezone":
                    {
                        string tz  = NextArgument(args, ref i);
                        var    tzi = TimeZoneParser.ParseTimeZoneId(tz);
                        if (tzi == null)
                        {
                            throw new ArgumentException($"Invalid value for -setTimezone '{tz}'. Use '-listTimezones' option to find valid values.");
                        }
                        s_photoFinisher.SetTimezoneTo = tzi;
                    }
                    break;

                    case "-changetimezone":
                    {
                        string tz  = NextArgument(args, ref i);
                        var    tzi = TimeZoneParser.ParseTimeZoneId(tz);
                        if (tzi == null)
                        {
                            throw new ArgumentException($"Invalid value for -changeTimezone '{tz}'. Use '-listTimezones' option to find valid values.");
                        }
                        s_photoFinisher.ChangeTimezoneTo = tzi;
                    }
                    break;

                    case "-updatefscreate":
                        s_photoFinisher.UpdateFileSystemDateCreated = true;
                        break;

                    case "-updatefsmod":
                        s_photoFinisher.UpdateFileSystemDateModified = true;
                        break;

                    case "-setwidth":
                        s_photoFinisher.SetWidth = NextArgumentAsInt(args, ref i);
                        break;

                    case "-setheight":
                        s_photoFinisher.SetHeight = NextArgumentAsInt(args, ref i);
                        break;

                    case "-log":
                        s_log = true;
                        break;

                    case "-listtimezones":
                    case "-listtimezone":
                        s_operation = Operation.ListTimeZones;
                        break;

                    case "-listnamedsources":
                        s_operation = Operation.ListNamedSources;
                        break;

                    case "-authonedrive":
                        s_sourceName = NextArgument(args, ref i);
                        s_operation  = Operation.AuthOneDrive;
                        break;

#if DEBUG
                    case "-testmetadatafromfilename":
                        s_testAction   = MediaFile.TestMetadataFromFilename;
                        s_testArgument = NextArgument(args, ref i);
                        s_operation    = Operation.TestAction;
                        break;
#endif

                    default:
                        throw new ArgumentException($"Unexpected command-line argument: {args[i]}");
                    }
                }

                if (s_photoFinisher.SortBy != DatePathType.None && string.IsNullOrEmpty(s_photoFinisher.DestinationDirectory))
                {
                    throw new ArgumentException("'-sort' option requires '-d' destination option.");
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
                Console.WriteLine("Use '-h' for syntax help");
                s_operation = Operation.CommandLineError;
            }
        } // ParseCommandLine
Example #3
0
        static void Main(string[] args)
        {
            try
            {
                ParseCommandLine(args);

                switch (s_operation)
                {
                case Operation.ShowHelp:
                    Console.WriteLine(c_Syntax);
                    break;

                case Operation.ProcessMediaFiles:
                    ProcessMediaFiles();
                    break;

                case Operation.ListTimeZones:
                    TimeZoneParser.ListTimezoneIds();
                    break;

                case Operation.ListNamedSources:
                    foreach (var name in NamedSource.GetSourceNames())
                    {
                        Console.WriteLine(name);
                    }
                    break;

                case Operation.DeleteNamedSource:
                    break;

                case Operation.AuthOneDrive:
                    NamedSource.OneDriveLoginAndAuthorize(s_sourceName);
                    break;

#if DEBUG
                case Operation.TestAction:
                    s_testAction(s_testArgument);
                    break;
#endif
                }
            }
            catch (Exception err)
            {
#if DEBUG
                Console.WriteLine(err.ToString());
#else
                Console.WriteLine(err.Message);
#endif
                if (s_logWriter != null)
                {
                    s_logWriter.WriteLine(err.ToString());
                }
            }
            finally
            {
                if (s_logWriter != null)
                {
                    s_logWriter.Dispose();
                    s_logWriter = null;
                }
            }

            if (s_waitBeforeExit)
            {
                Console.WriteLine();
                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();
            }
        }