예제 #1
0
        static int ParseDownloadOptions(DownloadOptions opts)
        {
            checkMutualExclusionDownload(opts);

            //If else tree for parsing mutually exclusive options
            //file(s) in DownloadFiles list
            if (opts.DownloadFiles.Count() != 0)
            {
                //for each file in the files list
                foreach (var file in opts.DownloadFiles)
                {
                    //people might be likely to put a proceeding slash...
                    var file_normalized = file.TrimStart('/');

                    String FilePath = Path.GetDirectoryName(file_normalized);
                    String FileName = Path.GetFileName(file_normalized);

                    //file path is blank, its gonna be a shared file!
                    if (FilePath == "")
                    {
                        Dictionary <string, dynamic> Results = interflow.interflow.downloadInterflowSharedFile(opts.apikey, FileName);
                        if (Results["response"] == 200)
                        {
                            try
                            {
                                System.IO.File.WriteAllBytes(Path.Combine(opts.OutputFolder, FileName), Results["data"]);
                            }
                            catch (Exception e)
                            {
                                Console.Error.WriteLine("*** ERROR output directory not found - " + e.Message);
                            }
                        }
                        else
                        {
                            Console.Error.WriteLine("*** ERROR downloading file " + file_normalized + " - returned http response: " + Results["response"]);
                        }
                    }
                    else
                    {
                        Dictionary <string, dynamic> Results = interflow.interflow.downloadInterflowFile(opts.apikey, FilePath, FileName);
                        if (Results["response"] == 200)
                        {
                            try
                            {
                                System.IO.File.WriteAllBytes(Path.Combine(opts.OutputFolder, FileName), Results["data"]);
                            }
                            catch (Exception e)
                            {
                                Console.Error.WriteLine("*** ERROR output directory not found - " + e.Message);
                            }
                        }
                        else
                        {
                            Console.Error.WriteLine("*** ERROR downloading file " + file_normalized + " - returned http response: " + Results["response"]);
                        }
                    }
                }
            }

            //Downloading a directory (mirror)
            else if (!String.IsNullOrEmpty(opts.DownloadDirectory))
            {
                //first get the list of files in the directory supplied
                List <string> FileList = listFiles(opts.apikey, opts.DownloadDirectory, false);

                foreach (String file in FileList)
                {
                    String FilePath = opts.DownloadDirectory;
                    String FileName = Path.GetFileName(file);
                    Dictionary <string, dynamic> Results;
                    if (FilePath == "" || FilePath == "/" || FilePath == "\"/\"" || FilePath == "\\" || FilePath == "\"\\\"")
                    {
                        Results = interflow.interflow.downloadInterflowSharedFile(opts.apikey, FileName);
                    }
                    else
                    {
                        //similar to how we trim in the listFiles function, we also trim here to prevent any / in the beginning from being sent to the api endpoint.
                        FilePath = FilePath.TrimStart('/');
                        FilePath = FilePath.TrimStart('\\');
                        Results  = interflow.interflow.downloadInterflowFile(opts.apikey, FilePath, FileName);
                    }
                    if (Results["response"] == 200)
                    {
                        try
                        {
                            System.IO.File.WriteAllBytes(Path.Combine(opts.OutputFolder, FileName), Results["data"]);
                        }
                        catch (Exception e)
                        {
                            Console.Error.WriteLine("*** ERROR output directory not found - " + e.Message);
                        }
                    }
                    else
                    {
                        Console.Error.WriteLine("*** ERROR downloading file " + file + " - returned http response: " + Results["response"]);
                    }
                }
            }

            return(0);
        }