Пример #1
0
        public bool TryDo(string command)
        {
            if (!command.StartsWith(_customName))
            {
                return(false);
            }

            var splt = command.Split(" ");

            if (splt.Length < 2)
            {
                return(false);
            }

            if (_container.HasRecords())
            {
                throw new CommandHandleException("уже есть загруженные записи, сначала очистите - 'clear'");
            }

            if (!_fileAction.Exists(splt[1]))
            {
                throw new CommandHandleException("file not found");
            }

            var data = _fileAction.ReadAllLines(splt[1]);

            if (data.Length < 2)
            {
                throw new CommandHandleException("file is empty, please create new");
            }

            var encLoginSplit = data[0].Split(Consts.FileDataStringSeparate);

            if (encLoginSplit.Length < 2)
            {
                throw new CommandHandleException("file is wrong sign");
            }

            var encLogin = encLoginSplit[1];

            string savedLogin = "";

            try
            {
                savedLogin = _coder.DecryptFromString(encLogin, _appSettings.Key);
            }
            catch
            {
            }

            if (!_appSettings.LoginIsGood(savedLogin))
            {
                throw new CommandHandleException(
                          "bad password for this db, please load any db or change password(clear command)");
            }

            foreach (var line in data[1..])
Пример #2
0
        public bool TryDo(string command)
        {
            if (!command.StartsWith(_customName))
            {
                return(false);
            }

            var splitCommand = command.Split(" ");
            var argCount     = 2;

            if (splitCommand.Length < argCount)
            {
                throw new CommandHandleException($"min {argCount} аргумента");
            }

            string loginForSave    = _appSettings.Login;
            string passwordForSave = _appSettings.Key;
            bool   differentCredit = false;

            if (splitCommand.Length > 2)
            {
                if (splitCommand.Length < 4)
                {
                    throw new CommandHandleException($"2 or 4 arg");
                }
                else
                {
                    loginForSave    = splitCommand[2];
                    passwordForSave = splitCommand[3];
                    differentCredit = true;
                }
            }


            string pathForSave = splitCommand[1];//"./"

            if (_fileAction.Exists(pathForSave))
            {
                throw new CommandHandleException($"file is exist");
            }

            List <string> forWrite = new List <string>();

            forWrite.Add("check" + Consts.FileDataStringSeparate + _coder.EncryptWithString(loginForSave, passwordForSave));
            foreach (var item in _container.GetAll())
            {
                string value = item.Value;
                if (!differentCredit)
                {
                    value = item.Value;
                }
                else
                {
                    value = _coder.EncryptWithString(
                        _coder.DecryptFromString(value, _appSettings.Key), passwordForSave);
                }

                forWrite.Add(item.Key + Consts.FileDataStringSeparate + value);
            }

            _fileAction.WriteAllLines(pathForSave, forWrite);

            return(true);
        }