示例#1
0
        //public static async Task<ReturnedSaveFuncInfo> SendChat(long number)
        //{
        //    var res = new ReturnedSaveFuncInfo();
        //    try
        //    {
        //        res.AddReturnedValue(await CheckToken(number, AdvertiseType.DivarChat));
        //        if (res.HasError) return res;

        //        var sim = await SimCardBusiness.GetAsync(number);
        //        var owner = await OwnerBusiness.GetAsync(sim.OwnerGuid);

        //        res.AddReturnedValue(await SendChat_(number, owner.VisitorGuid));
        //        if (res.HasError) return res;

        //        CloseAllChromeWindows();
        //    }
        //    catch (Exception ex)
        //    {
        //        WebErrorLog.ErrorLogInstance.StartLog(ex);
        //        res.AddReturnedValue(ex);
        //    }

        //    return res;
        //}
        public static async Task <ReturnedSaveFuncInfoWithValue <string> > CheckToken(long number, AdvertiseType type)
        {
            var res = new ReturnedSaveFuncInfoWithValue <string>();

            try
            {
                var tk = await AdvTokenBussines.GetTokenAsync(number, type);

                if (tk == null)
                {
                    res.AddReturnedValue(ReturnedState.Error, "Token is Null");
                    return(res);
                }
                if (string.IsNullOrEmpty(tk.Token))
                {
                    res.AddReturnedValue(ReturnedState.Error, "Token is Empty");
                    return(res);
                }

                res.value = tk.Token;
            }
            catch (Exception ex)
            {
                WebErrorLog.ErrorInstence.StartErrorLog(ex);
                res.AddReturnedValue(ex);
            }

            return(res);
        }
示例#2
0
        /// <summary>
        /// ورودی آدرس فایل خروجی آدرس پوشه ای که باید فشرده سازی شود
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static async Task <ReturnedSaveFuncInfoWithValue <string> > Move2Temp(string filePath, Guid guid, EnBackUpType type)
        {
            var ret = new ReturnedSaveFuncInfoWithValue <string>();

            try
            {
                string   tempDIR   = TempDirName();
                FileInfo tempInfor = new FileInfo(filePath);
                if (!File.Exists(filePath))
                {
                    var msg = $"path not exists : {filePath}";
                    await DatabaseAction.BackUpLogAsync(guid, type, EnBackUpStatus.Error, filePath, msg);

                    ret.AddReturnedValue(ReturnedState.Error, msg);
                    return(ret);
                }
                var retFilePath = Path.Combine(tempDIR, tempInfor.Name);
                File.Move(filePath, retFilePath);
                ret.value = tempDIR;
                await DatabaseAction.BackUpLogAsync(guid, type, EnBackUpStatus.Success, filePath, "انتقال فایل به پوشه Temp انجام شد");
            }
            catch (Exception ex)
            {
                await DatabaseAction.BackUpLogAsync(guid, type, EnBackUpStatus.Error, filePath, ex.Message);

                WebErrorLog.ErrorInstence.StartErrorLog(ex);
                ret.AddReturnedValue(ex);
            }
            return(ret);
        }
        private static async Task <ReturnedSaveFuncInfoWithValue <string> > CreateSqlServerBackupFileAsync(string path, string connectionString, Guid guid, EnBackUpType type)
        {
            var    ret         = new ReturnedSaveFuncInfoWithValue <string>();
            string commandText = "";

            try
            {
                var inf = new FileInfo(path);
                if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(inf.Name) || !Directory.Exists(inf.DirectoryName))
                {
                    var msg = $"آدرس {path} موجود یا معتبر نیست.";
                    await BackUpLogAsync(guid, type, EnBackUpStatus.Error, path, msg);

                    ret.AddReturnedValue(ReturnedState.Error, msg);
                    return(ret);
                }
                var cn = new SqlConnection(connectionString);
                ret.value = new SqlConnectionStringBuilder(connectionString).InitialCatalog;
                var cmd = new SqlCommand($"Backup database [{ret.value }] to disk='{path}'", cn)
                {
                    CommandType = CommandType.Text
                };
                commandText = cmd.CommandText;
                //تنها جای مجاز برای افزایش تایم اوت هست
                cmd.CommandTimeout = 5 * 60 * 1000;//5 minutes

                await cn.OpenAsync();

                await cmd.ExecuteNonQueryAsync();

                cn.Close();
                await BackUpLogAsync(guid, type, EnBackUpStatus.Success, path, "پشتیبان اولیه با پسوند .bak تهیه شد");

                commandText = "";
            }
            catch (SqlException ex)
            {
                var msg = $"خطا در تهیه نسخه پشتیبان اطلاعات \r\nحساب کاربری SQL دسترسی به مسیر نصب برنامه ندارد\r\n{ex.Message}//{ex?.InnerException?.Message ?? ""}";
                await BackUpLogAsync(guid, type, EnBackUpStatus.Error, path, msg);

                WebErrorLog.ErrorInstence.StartErrorLog(ex);
                ret.AddReturnedValue(ReturnedState.Error, msg);
            }
            catch (Exception ex)
            {
                await BackUpLogAsync(guid, type, EnBackUpStatus.Error, path, ex.Message);

                WebErrorLog.ErrorInstence.StartErrorLog(ex);
                ret.AddReturnedValue(ex);
            }
            return(ret);
        }
        public async Task <ReturnedSaveFuncInfoWithValue <string> > ExtractTempDIR(string archiveName)
        {
            var ret = new ReturnedSaveFuncInfoWithValue <string>();

            try
            {
                var pathtemp = Zip.TempDirName();
                var fileInfo = new FileInfo(archiveName);
                pathtemp = Path.Combine(pathtemp, fileInfo.Name);
                pathtemp = Path.ChangeExtension(pathtemp, ".bak");
                using (var archive = GZipArchive.Open(archiveName))
                {
                    foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
                    {
                        entry.WriteToFile(pathtemp, new ExtractionOptions()
                        {
                            ExtractFullPath = true,
                            Overwrite       = true
                        });
                    }
                }
                ret.value = pathtemp;
            }
            catch (Exception ex)
            {
                WebErrorLog.ErrorInstence.StartErrorLog(ex);
                ret.AddReturnedValue(ex);
            }
            return(ret);
        }
示例#5
0
        public static ReturnedSaveFuncInfoWithValue <string> GetVersion(string name)
        {
            var ret = new ReturnedSaveFuncInfoWithValue <string>();

            try
            {
                var a = Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\\Software\\Arad\\", name,
                                                          "");
                ret.value = a.ToString();
            }
            catch (Exception ex)
            {
                ret.AddReturnedValue(ex);
            }
            return(ret);
        }
示例#6
0
        public static ReturnedSaveFuncInfoWithValue <string> GetConnectionRegistery(string name)
        {
            var ret = new ReturnedSaveFuncInfoWithValue <string>();

            try
            {
                var a = Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\\Software\\Arad\\", name,
                                                          "");
                ret.value = a.ToString();
            }
            catch (Exception ex)
            {
                //WebErrorLog.ErrorInstence.StartErrorLog(ex);
                ret.AddReturnedValue(ex);
            }
            return(ret);
        }
        public static async Task <ReturnedSaveFuncInfoWithValue <string> > BackupDbAsync(string connectionString, ENSource source, EnBackUpType type, string path = "")
        {
            var    line         = 0;
            var    res          = new ReturnedSaveFuncInfoWithValue <string>();
            bool   IsAutomatic  = string.IsNullOrEmpty(path);
            string DatabaseName = "";
            var    guid         = Guid.NewGuid();

            try
            {
                OnCreateBackup?.Invoke(null, new CreateBackupArgs()
                {
                    Message = "", State = CreateBackupState.Start
                });

                if (IsAutomatic)
                {
                    path = CreateFileName(connectionString);
                }
                if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
                {
                    //در نرم افزار حسابداری آدرس پوشه پشتیبان گیری اتوماتیک داده شده است
                    //باید فایل پشتیبان در این پوشه ایجاد گردد
                    path        = CreateFileName(connectionString, path);
                    IsAutomatic = true;
                }

                await BackUpLogAsync(guid, type, EnBackUpStatus.Pending, path,
                                     "آغاز فرآیند تهیه فایل پشتیبان");

                //تهیه اولین نسخه بکاپ توسط سرویس اس کیو ال
                var CreateSqlBackuResult = await CreateSqlServerBackupFileAsync(path, connectionString, guid, type);

                DatabaseName = CreateSqlBackuResult.value;
                res.AddReturnedValue(CreateSqlBackuResult);

                var PathForZipDirectory = await Zip.Move2Temp(path, guid, type);

                res.AddReturnedValue(PathForZipDirectory);

                try
                {
                    //please dont remove this try
                    //اگر به خطا خورد باید تابع ادامه پیدا کند و پشتیبان با پسوند .بک تهیه شود
                    res.AddReturnedValue(
                        await CompressFile.CompressFileInstance.CompressFileAsync(PathForZipDirectory.value, path, guid,
                                                                                  type));
                }
                catch (Exception ex)
                {
                    WebErrorLog.ErrorInstence.StartErrorLog(ex);
                    res.AddReturnedValue(ex);
                }
                res.value = path.ToLower().Replace(".npz2", ".np").Replace(".npz", ".np").Replace(".np", ".npz2");

                //بنا به هر دلیلی امکان فشرده کردن فایل وجود نداشته است
                //باید همان بکاپ فشرده نشده به آدرس مقصد درخواستی مشتری متقل گردد
                if (res.HasError)
                {
                    var file = Directory.GetFiles(PathForZipDirectory.value).FirstOrDefault();
                    File.Move(file, path.ToLower().Replace(".npz2", ".np").Replace(".npz", ".np"));
                }

                OnCreateBackup?.Invoke(null,
                                       new CreateBackupArgs()
                {
                    Message = "", State = CreateBackupState.End
                });

                await BackUpLogAsync(guid, type, EnBackUpStatus.Success, path.ToLower(),
                                     "عملیات پشتیبان گیری با موفقیت انجام شد");
            }
            catch (OperationCanceledException ex)
            {
                res.AddReturnedValue(ex);
            }
            catch (Exception ex)
            {
                await BackUpLogAsync(guid, type, EnBackUpStatus.Error, path.ToLower().Replace(".npz2", ".np").Replace(".npz", ".np"),
                                     ex.Message);

                WebErrorLog.ErrorInstence.StartErrorLog(ex, $"Error in Line {line}");
                res.AddReturnedValue(ex);
            }
            finally
            {
                Task.Run(DeleteTempsAsync);
            }
            return(res);
        }