Example #1
0
        public static string EnsureDataFile(string name, string extension, string subDir, string appendName = null)
        {
            if (String.IsNullOrEmpty(exePath))
            {
                SetAppInfo();
            }

            subDir = MyFile.SafeDirectory(subDir);
            string dataDirectory = exeDirectory + @"\" + subDir;

            if (!Directory.Exists(dataDirectory))
            {
                try
                {
                    Directory.CreateDirectory(dataDirectory);
                }
                catch (Exception e)
                {
                    if (subDir.IndexOf("log") != -1)
                    {
                        MyLog.Add(e.ToString());
                    }
                    else if (Application.OpenForms.Count > 0)
                    {
                        MessageBox.Show("Unable to create the data directory [" + dataDirectory + "]");
                    }
                    return(null);
                }
            }

            if (appendName != null)
            {
                appendName = "_" + appendName;

                appendName = MyFile.SafeFileName(appendName);
            }


            string file = dataDirectory + @"\" + name + appendName + "." + extension;

            return(file);
        }
        private void buttonSourceBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

            if (textBoxDirectory.Text != "" && textBoxDirectory.Text.Length > 2)
            {
                folderBrowserDialog.SelectedPath = textBoxDirectory.Text;
            }

            if (folderBrowserDialog.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            else
            {
                textBoxDirectory.Text = folderBrowserDialog.SelectedPath;
                if (textBoxAlias.Text == "")
                {
                    string alias = folderBrowserDialog.SelectedPath.Substring(folderBrowserDialog.SelectedPath.LastIndexOf(@"\") + 1);
                    MyFormField.SetTextBoxText(textBoxAlias, MyFile.SafeFileName(alias));
                }
            }
        }
        public bool SyncUpVideoSource(FileInfo sourceFileInfo)
        {
            // clean up old uploads
            IEnumerable <string> files = MyFile.EnumerateFiles(@"sync", "*.gz");

            foreach (string file in files)
            {
                MyFile.DeleteFile(file);
            }

            // compress file, video source
            string compressedFile = @"sync\" + MyFile.SafeFileName(sourceFileInfo.Name);

            if (!MyFile.Compress(sourceFileInfo.FullName, compressedFile))
            {
                return(false);
            }

            // rename file so has 'rand' key/iv
            Random random     = new Random();
            int    rand       = random.Next(0, MyEncrypt.sharedKeys.Length - 1);
            string uploadFile = compressedFile.Replace("." + Config.settings.exportExt, "-" + rand + "." + Config.settings.exportExt);

            File.Move(compressedFile + ".gz", uploadFile + ".gz");



            string key       = MyEncrypt.sharedKeys[rand];
            string iv        = MyEncrypt.GenerateIV();
            string delimiter = "#";

            // now encrypt compressed file contents
            string fileContents = MyFile.ReadAllBinaryToString(uploadFile + ".gz");

            string contentsHeader = "{";

            contentsHeader += "\"apiKey\":\"" + this.apiKey + "\", ";
            contentsHeader += "\"iv\":\"" + iv + "\", ";
            contentsHeader += "\"sync\":" + MySerialize.ToJSON(Sync.syncSettings);
            contentsHeader += "}";

            contentsHeader = Convert.ToBase64String(Encoding.UTF8.GetBytes(contentsHeader));



            fileContents = MyEncrypt.EncryptRJ256(key, iv, fileContents);
            if (fileContents == null)
            {
                return(false);
            }

            contentsHeader = MyEncrypt.EncryptRJ256(key, iv, contentsHeader);

            string contentsToEncode = contentsHeader + delimiter + fileContents;

            // write base64 encoded file
            File.WriteAllText(uploadFile + ".enc", contentsToEncode);


            // log it
            FileInfo encodedFileInfo = MyFile.FileInfo(uploadFile + ".enc");

            if (encodedFileInfo == null)
            {
                return(false);
            }
            MyLog.Add(String.Format("Encrypted {0} to {1}", encodedFileInfo.Name, MyFile.FormatSize(encodedFileInfo.Length)));


            // test decrypt

            fileContents = MyFile.ReadAllText(uploadFile + ".enc");

            string[] fileParts = fileContents.Split(new string[] { delimiter }, StringSplitOptions.None);

            fileContents = MyEncrypt.DecryptRJ256(key, iv, fileParts[1]);



            MyFile.DeleteFile(uploadFile + ".gz");
            MyFile.WriteAllBinaryFromString(uploadFile + ".gz", fileContents);



            // post encoded file to website

            string url = apiURL;
            List <KeyValuePair <string, string> > headers = new List <KeyValuePair <string, string> > {
            };

            headers.Add(new KeyValuePair <string, string>("api-key", apiKey));
            headers.Add(new KeyValuePair <string, string>("access-token", iv));
            Upload(url, uploadFile + ".enc", headers);



            return(true);
        }
        private void buttonSaveSource_Click(object sender, EventArgs e)
        {
            string alias     = textBoxAlias.Text.Trim();
            string type      = VideoFileEnums.sourceType.GetTextByValue(comboBoxType.SelectedValue);
            string directory = textBoxDirectory.Text.Trim();

            string message = "";

            if (alias == "")
            {
                message += "Enter an Alias for this Video Source." + Environment.NewLine;
            }
            if (directory.Length < 3)
            {
                message += "Select a Directory for this Video Source" + Environment.NewLine;
            }
            foreach (ListViewItem listViewItemCheck in listViewSource.Items)
            {
                string aliasCheck     = listViewItemCheck.SubItems[0].Text;
                string directoryCheck = listViewItemCheck.SubItems[1].Text;

                if (alias == aliasCheck)
                {
                    message += "Alias [" + alias + "] already exists." + Environment.NewLine;
                    message += "Enter an unique Alias for this Video Source." + Environment.NewLine;
                }
                if (directory == directoryCheck)
                {
                    message += "Directory [" + directory + "] already exists." + Environment.NewLine;
                    message += "Select an unique Directory for this Video Source." + Environment.NewLine;
                }
            }
            if (message != "")
            {
                MessageBox.Show(message);
                return;
            }

            // add to source list
            alias     = MyFile.SafeFileName(alias);
            directory = MyFile.SafeDirectory(directory);
            ListViewItem listViewItem = new ListViewItem(alias);

            listViewItem.SubItems.Add(type);
            listViewItem.SubItems.Add("");
            listViewSource.Items.Add(listViewItem);

            // add to settings
            ConfigSettings.Source source = new ConfigSettings.Source();
            source.alias     = alias;
            source.directory = directory;
            source.type      = type;
            Config.settings.sources.Add(source);

            // clear form
            ClearSourceForm();

            buttonSaveSource.Enabled = false; // no edits

            // select added item and infer scan
            listViewItem = listViewSource.FindItemWithText(source.alias);
            if (listViewItem != null)
            {
                listViewItem.Selected = true;
            }
            buttonScanSource.Focus();

            // video list ui will be update when user rescans
            // video list xml will be updated on main form close
            // settings xml will be updated on main form close, and options form closes
        }
Example #5
0
        public bool SaveFilter(string filterName)
        {
            if (filterName.Length == 0)
            {
                return(false);
            }
            string dataFile;

            if (filterName == "<_reset_>")
            {
                MessageBox.Show("Cannot Save filter");
                subFormFilterForm.ResetForm();
                return(false);
            }
            else if (filterName == "<filter when closed>")
            {
                dataFile = MyFile.EnsureDataFile("Filter", Config.settings.exportExt, "filters", "filter when closed");
                if (dataFile == null)
                {
                    return(false);
                }
            }
            else if (filterName == "<last saved filter>")
            {
                MessageBox.Show("Cannot Save filter.\nThis Filter will be automaticlly be saved when you Save another Filter.");
                return(false);
            }
            else
            {
                filterName = MyFile.SafeFileName(filterName);
                dataFile   = MyFile.EnsureDataFile("Filter", Config.settings.exportExt, "filters", filterName);
                if (dataFile == null)
                {
                    return(false);
                }
                allow_comboBoxPreset_SelectedIndexChanged = false;
                comboBoxFilters.Text = filterName;
                allow_comboBoxPreset_SelectedIndexChanged = true;
            }

            FilterInfo filterInfo = subFormFilterForm.GetFilterForm();

            MyLog.RotateFiles(dataFile);

            filterInfo.about = Config.GetConfigSettingsAbout();
            filterInfo.name  = filterName;
            if (filterInfo.description == null)
            {
                filterInfo.description = "";
            }

            MySerialize.ToFile(Config.settings.exportFormat, dataFile, filterInfo);

            MyLog.Add("Saved Filter Preset " + dataFile.Replace(MyFile.exeDirectory, ""));

            // make a copy of last saved filter
            if (filterName != "<last saved filter>" && filterName != "<filter when closed>")
            {
                dataFile = MyFile.EnsureDataFile("Filter", Config.settings.exportExt, "filters", "last saved filter");
                if (dataFile == null)
                {
                    return(false);
                }
                MyLog.RotateFiles(dataFile);


                MySerialize.ToFile(Config.settings.exportFormat, dataFile, filterInfo);
                MyLog.Add("Saved Filter Preset " + dataFile.Replace(MyFile.exeDirectory, ""));
            }


            return(true);
        }