예제 #1
0
        public (ErrStruct, tgtdata) GetTargetTypes(string TargetFormat)
        {
            // get target types available from the source format
            if (TargetFormat.StartsWith("."))
            {
                TargetFormat = TargetFormat.Substring(1);
            }
            RestClient client = new RestClient(ZServer + "formats/");

            client.Authenticator = new HttpBasicAuthenticator(apiKey, "");
            RestRequest      request      = new RestRequest(TargetFormat, DataFormat.Json);
            IRestResponse    response     = client.Get(request);
            JsonDeserializer deserializer = new JsonDeserializer();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                tgtdata td = deserializer.Deserialize <tgtdata>(response);
                return(new ErrStruct(), td);
            }
            else
            {
                ErrStruct err = deserializer.Deserialize <ErrStruct>(response);
                return(err, new tgtdata());
            }
        }
예제 #2
0
        private void Start()
        {
            // this will do all communication with the server
            ServerWorker Server = new ServerWorker(ApiKeyTBox, SandboxCkBox);

            //
            Log("Clear");

            // have user select input file
            Log("Selecting Input file");
            UserSelectInputFile();
            if (FullInputFileName == string.Empty)
            {
                Log("Clear");
                Log("No input file selected");
                ControlsEnabled(true);
                return;
            }
            Log("Input file: " + Path.GetFileName(FullInputFileName));

            // get possible target formats
            (ErrStruct, tgtdata)ErrAndTgt = Server.GetTargetTypes(Path.GetExtension(FullInputFileName));
            ErrStruct err  = ErrAndTgt.Item1;
            tgtdata   tdat = ErrAndTgt.Item2;

            if (err.errors != null)
            {
                Log("Error from Server:");
                foreach (OneError oneE in err.errors)
                {
                    string msg = oneE.message;
                    if (msg == "resource does not exist")
                    {
                        msg = "Can not convert a file of this type.";
                    }
                    Log("  " + msg + "  (code: " + oneE.code + ")");
                }
                Util.ErrorBeep();
                ControlsEnabled(true);
                return;
            }

            // have user select destination format
            UserSelectTargetFileType(tdat.targets);
            if (SelectedTypeExtension == string.Empty)
            {
                Log("No conversion Format selected.");
                ControlsEnabled(true);
                return;
            }
            Log("Convert file to ." + SelectedTypeExtension);

            // determine where output file(s) should be placed
            string DestFolder = Path.GetDirectoryName(FullInputFileName);

            if (!SameDirectoryCkBox.Checked)
            {
                // ask user where to put the files
                bool UseWindowsDefaultUglyBrowser = false;
                if (UseWindowsDefaultUglyBrowser)
                {
                    FolderBrowserDialog folderDlg = new FolderBrowserDialog();
                    folderDlg.ShowNewFolderButton = true;
                    folderDlg.SelectedPath        = DestFolder;
                    folderDlg.Description         = "Where should output files be placed?";
                    DialogResult result = folderDlg.ShowDialog(this);
                    if (result != DialogResult.OK)
                    {
                        Log("No Destination folder was selected.");
                        ControlsEnabled(true);
                        return;
                    }
                    DestFolder = folderDlg.SelectedPath;
                }
                else
                {
                    WKLib.BetterFolderBrowser bfb = new WKLib.BetterFolderBrowser();
                    bfb.RootFolder = DestFolder.Substring(0, DestFolder.LastIndexOf('\\'));
                    bfb.Title      = "Where should output files be placed?";
                    DialogResult result = bfb.ShowDialog();
                    if (result != DialogResult.OK)
                    {
                        Log("No Destination folder was selected.");
                        ControlsEnabled(true);
                        return;
                    }
                    DestFolder = bfb.SelectedFolder;
                }
            }

            // insure the output directory exists (it always should, in theory)
            bool okay = Util.DirectoryExistsOrWasCreated(DestFolder);

            if (!okay)
            {
                ControlsEnabled(true);
                Log("ERROR");
                Log("  Can not create output directory: ");
                Log("  " + DestFolder);
                return;
            }

            // insure we can write to the selected output directory
            okay = Util.DirectoryIsWriteable(DestFolder);
            if (!okay)
            {
                ControlsEnabled(true);
                Log("ERROR:");
                Log("  Can not write to destination directory:");
                Log("  " + DestFolder);
                return;
            }

            // send input file to server and start conversion job
            (ErrStruct, jobstatus)ErrAndJob = Server.UploadFileStartConversion(FullInputFileName, SelectedTypeExtension);
            err = ErrAndJob.Item1;
            jobstatus jobstat = ErrAndJob.Item2;

            if (err.errors != null)
            {
                Log("Error from Server:");
                foreach (OneError oneE in err.errors)
                {
                    string msg = oneE.message;
                    Log("  " + msg + "  (code: " + oneE.code + ")");
                }
                Util.ErrorBeep();
                ControlsEnabled(true);
                return;
            }
            string msg2 = string.Format("job id = {0}, status = {1}", jobstat.id, jobstat.status);

            Log(msg2);

            // wait for conversion completion by the server
            (ErrStruct, jobstatus)StatusUpdate = Server.AwaitCompletion(jobstat.id, this);
            jobstat = StatusUpdate.Item2;
            Log(string.Format("Completion status: {0}", jobstat.status));
            Log(string.Format("Conversion cost:   {0} credit", jobstat.credit_cost));

            // download and save the completed file/files
            bool overwrite = OverwriteCkBox.Checked;

            Server.DownloadFiles(DestFolder, jobstat.target_files, overwrite);

            Log("Done.");
            //
            SaveState.Save(ApiKeyTBox.Text, DestFolder, OverwriteCkBox.Checked, SameDirectoryCkBox.Checked, SandboxCkBox.Checked);

            // done
            ControlsEnabled(true);
        }