示例#1
0
        /// <summary>
        ///     Преобразовывает прикладной объект в объект xml-модели, используя указанный маппер.
        /// </summary>
        /// <param name="appObject"></param>
        /// <returns></returns>
        private IEnumerable <ISync> GetXMLObjectByMapper <TItem>(DataObject appObject)
            where TItem : IChangedItem
        {
            IEnumerable <ISync> xmlObjects;
            SyncSetting         setting = null;
            IPropertyMapper     mapper  = null;

            try
            {
                // Преобразуем вычитанный изменнённый объект в его xml-представление.
                //TODO Проверить обработку мастеровых и детейловых объектов при маппинге. В маппинге используется дочитка, возможно сброситься состояние полученное при откате изменений.
                var field = GetXMLDestinationTypeByChangedItem <TItem>(appObject);
                // Этот объект так же должен поддерживать ISync.
                var syncObj = appObject as ISync;
                setting    = SettingService.Current.GetSettings(syncObj).First(s => s.Destination.Name == field.FullName);
                mapper     = setting.ExtractMapper <IPropertyMapper>();
                xmlObjects = mapper.Map(appObject).Cast <ISync>();
            }
            catch (ArgumentException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new MappingException(appObject, setting, mapper, ex.Message);
            }

            return(xmlObjects);
        }
 private void SaveSettingsToConfig(string currentIndex)
 {
     SyncSetting.settings.Set("ConfigStateCurrentIndex", currentIndex);
     SyncSetting.SaveSettingToConfig(chk_configActive.Name, chk_configActive.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig("LocalPath", directoryPath?.Text, ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig("PlaylistUrl", url?.Text, ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(nameof(ConcurrentDownloads), nudConcurrency.Value.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(favoritesRadio.Name, favoritesRadio.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig("playlistRadio", playlistRadio.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(userPlaylists.Name, userPlaylists.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(artistRadio.Name, artistRadio.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(trackRadio.Name, trackRadio.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(chk_convertToMp3.Name, chk_convertToMp3.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(chk_excl_m4a.Name, chk_excl_m4a.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(chk_exl_aac.Name, chk_exl_aac.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(chk_folderByArtist.Name, chk_folderByArtist.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(chk_highquality.Name, chk_highquality.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(chk_replaceIllegalCharacters.Name, chk_replaceIllegalCharacters.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(rbttn_oneWay.Name, rbttn_oneWay.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(rbttn_twoWay.Name, rbttn_twoWay.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(chk_MergePlaylists.Name, chk_MergePlaylists.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(chk_CreatePlaylists.Name, chk_CreatePlaylists.Checked.ToString(), ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(nameof(FormatForName), FormatForName, ConfigStateCurrentIndex);
     SyncSetting.SaveSettingToConfig(nameof(FormatForTag), FormatForTag, ConfigStateCurrentIndex);
 }
        private void LoadSettingsFromCurrentConfig(string currentIndex)
        {
            SyncSetting.settings.Set("ConfigStateCurrentIndex", currentIndex);
            string accessString = SyncSetting.GetAccessString(currentIndex, ConfigStateCurrentIndex);

            lbl_currentConfig.Text = SyncSetting.settings.Get("ConfigStateCurrentIndex");

            chk_configActive.Checked = bool.Parse(SyncSetting.LoadSettingFromConfig(chk_configActive.Name, accessString));
            url.Text                             = SyncSetting.LoadSettingFromConfig("PlaylistUrl", accessString);
            directoryPath.Text                   = SyncSetting.LoadSettingFromConfig("LocalPath", accessString);
            nudConcurrency.Value                 = int.Parse(SyncSetting.LoadSettingFromConfig(nameof(ConcurrentDownloads), accessString));
            favoritesRadio.Checked               = bool.Parse(SyncSetting.LoadSettingFromConfig(favoritesRadio.Name, accessString));
            userPlaylists.Checked                = bool.Parse(SyncSetting.LoadSettingFromConfig(userPlaylists.Name, accessString));
            playlistRadio.Checked                = bool.Parse(SyncSetting.LoadSettingFromConfig("playlistRadio", accessString));
            artistRadio.Checked                  = bool.Parse(SyncSetting.LoadSettingFromConfig(artistRadio.Name, accessString));
            trackRadio.Checked                   = bool.Parse(SyncSetting.LoadSettingFromConfig(trackRadio.Name, accessString));
            chk_convertToMp3.Checked             = bool.Parse(SyncSetting.LoadSettingFromConfig(chk_convertToMp3.Name, accessString));
            chk_excl_m4a.Checked                 = bool.Parse(SyncSetting.LoadSettingFromConfig(chk_excl_m4a.Name, accessString));
            chk_exl_aac.Checked                  = bool.Parse(SyncSetting.LoadSettingFromConfig(chk_exl_aac.Name, accessString));
            chk_folderByArtist.Checked           = bool.Parse(SyncSetting.LoadSettingFromConfig(chk_folderByArtist.Name, accessString));
            chk_highquality.Checked              = bool.Parse(SyncSetting.LoadSettingFromConfig(chk_highquality.Name, accessString));
            chk_replaceIllegalCharacters.Checked = bool.Parse(SyncSetting.LoadSettingFromConfig(chk_replaceIllegalCharacters.Name, accessString));
            chk_CreatePlaylists.Checked          = bool.Parse(SyncSetting.LoadSettingFromConfig(chk_CreatePlaylists.Name, accessString));
            chk_MergePlaylists.Checked           = bool.Parse(SyncSetting.LoadSettingFromConfig(chk_MergePlaylists.Name, accessString));
            rbttn_oneWay.Checked                 = bool.Parse(SyncSetting.LoadSettingFromConfig(rbttn_oneWay.Name, accessString));
            rbttn_twoWay.Checked                 = bool.Parse(SyncSetting.LoadSettingFromConfig(rbttn_twoWay.Name, accessString));
            FormatForName                        = SyncSetting.LoadSettingFromConfig(nameof(FormatForName), accessString);
            FormatForTag                         = SyncSetting.LoadSettingFromConfig(nameof(FormatForTag), accessString);
        }
示例#4
0
        //this is called before page loaded
        internal void LoadSyncSetting(SyncSetting syncSetting)
        {
            this.syncSetting = syncSetting;

            string jobName = string.Join("\t", new string[] { syncSetting.LocalDirectory, syncSetting.TargetBucket });

            this.jobId = Tools.md5Hash(jobName);

            string myDocPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            this.jobsDbPath = System.IO.Path.Combine(myDocPath, "qsunsync", "sync_jobs.db");
            this.jobLogDir  = System.IO.Path.Combine(myDocPath, "qsunsync", "logs", jobId);

            string localHashDBDir = System.IO.Path.Combine(myDocPath, "qsunsync", "hashdb");

            if (!Directory.Exists(localHashDBDir))
            {
                Directory.CreateDirectory(localHashDBDir);
            }
            string bucketId = Tools.md5Hash(syncSetting.TargetBucket);

            this.localHashDBPath = System.IO.Path.Combine(localHashDBDir, bucketId + ".db");
            this.cacheDir        = System.IO.Path.Combine(myDocPath, "qsunsync", "dircache");
            this.syncLogDir      = System.IO.Path.Combine(myDocPath, "qsunsync", "synclog");
            this.syncLogDBPath   = System.IO.Path.Combine(this.syncLogDir, jobId + ".log.db");

            string cacheId = jobId;

            this.cacheFilePathDone = System.IO.Path.Combine(cacheDir, cacheId + ".done");
            this.cacheFilePathTemp = System.IO.Path.Combine(cacheDir, cacheId + ".temp");
        }
示例#5
0
        void exportJobLogMenuItem_Click(object sender, RoutedEventArgs e)
        {
            object selectedItem = this.SyncHistoryListBox.SelectedItem;

            if (selectedItem != null)
            {
                string      jobId       = this.syncRecordDict[(ListBoxItem)selectedItem];
                SyncSetting syncSetting = SyncRecord.LoadSyncSettingByJobId(jobId);
                if (syncSetting != null)
                {
                    System.Windows.Forms.SaveFileDialog dlg = new System.Windows.Forms.SaveFileDialog();
                    dlg.Title  = "选择保存文件";
                    dlg.Filter = "Log (*.log)|*.log";

                    System.Windows.Forms.DialogResult dr = dlg.ShowDialog();
                    if (dr.Equals(System.Windows.Forms.DialogResult.OK))
                    {
                        string logSaveFilePath = dlg.FileName;
                        LogExporter.exportLog(
                            Path.Combine(this.myAppPath, "logs", jobId, "success.log"),
                            Path.Combine(this.myAppPath, "logs", jobId, "error.log"),
                            Path.Combine(this.myAppPath, "logs", jobId, "skipped.log"),
                            Path.Combine(this.myAppPath, "logs", jobId, "exists.log"),
                            Path.Combine(this.myAppPath, "logs", jobId, "not_overwrite.log"),
                            Path.Combine(this.myAppPath, "logs", jobId, "overwrite.log"),
                            logSaveFilePath);
                    }
                }
            }
        }
示例#6
0
        /// <summary>
        ///     Получить настройки из SyncDOEntity.
        /// </summary>
        /// <param name="syncEntity">SyncDOEntity из которой будем получать настройки</param>
        /// <param name="type">Тип объекта, который был изменён.</param>
        /// <param name="mapper">Маппер для объекта, который был изменён.</param>
        /// <param name="appObjPrimaryKey">Первичный ключ прикладного объекта, который был изменён.</param>
        private void GetSyncSettings(SyncDOEntity syncEntity, out Type type, out IPropertyMapper mapper, out Guid appObjPrimaryKey)
        {
            SyncSetting setting = null;

            type   = null;
            mapper = null;
            if (syncEntity.ObjectPrimaryKey == null ||
                !syncEntity.Date.HasValue ||
                !syncEntity.AuditChangePK.HasValue)
            {
                throw new NullParamInSyncEntityException(syncEntity);
            }

            appObjPrimaryKey = syncEntity.ObjectPrimaryKey.Value;

            try
            {
                setting = SettingService.Current.GetSetting(syncEntity);
                type    = setting.Source.ExtractType();
                mapper  = setting.ExtractMapper <IPropertyMapper>();
            }
            catch (Exception ex)
            {
                throw new LoadSyncSettingException(syncEntity.ObjectPrimaryKey, setting, type, mapper, ex.Message);
            }
        }
示例#7
0
        private static string GetErrorText(DataObject appObject, SyncSetting setting, IPropertyMapper mapper, string exMessage)
        {
            var text = $"Ошибка: не удалось преобразовать объект (Type = {appObject.GetType().AssemblyQualifiedName}, PK = {appObject.__PrimaryKey})!";

            if (!(appObject is ISync suncObj))
            {
                text = $"{text} Объект указанного типа не реализует {nameof(ISync)} интерфейс.";
            }
示例#8
0
        /// <summary>
        /// load sync settings, this method is called before the page loaded method
        /// </summary>
        /// <param name="syncSetting"></param>
        public void LoadSyncSetting(SyncSetting syncSetting)
        {
            this.syncSetting   = syncSetting;
            this.bucketManager = null;

            // [2016-09-01 11:40] 更新 @fengyh
            // 如果syncSetting已被设置,就说明是从历史记录载入
            // 如果syncSetting没有设置,就说明是新建任务
            this.isLoadFromRecord = (this.syncSetting != null);
        }
示例#9
0
        void Init()
        {
            base.Text      = LanguageManager.Language["STR_FORMAT_FILE_TITLE"];
            button3.Text   = LanguageManager.Language["STR_FORMAT_FILE_SAMTR"] + " ▼";
            button1.Text   = LanguageManager.Language["STR_FORMAT_FILE_FOROP"] + " ▼";
            checkBox1.Text = LanguageManager.Language["STR_FORMAT_FILE_FROMID3"];
            groupBox1.Text = LanguageManager.Language["STR_FORMAT_FILE_PREV"];
            button2.Text   = LanguageManager.Language["STR_FORMAT_FILE_SAVE"];

            track_sample = JsonConvert.DeserializeObject <Track>(SyncSetting.LoadSettingFromConfig("SampleTrack1"));
            RefreshTaglist();
        }
示例#10
0
        /// <summary>
        /// load sync settings, this method is called before the page loaded method
        /// </summary>
        /// <param name="syncSetting"></param>
        public void LoadSyncSetting(SyncSetting syncSetting)
        {
            if (syncSetting == null)
            {
                isLoadedFromRecord = false;
                this.syncSetting   = new SyncSetting();
            }
            else
            {
                isLoadedFromRecord = true;
                this.syncSetting   = syncSetting;
            }

            this.bucketManager = null;
        }
        private static string GetErrorText(Guid?syncEntityPK, SyncSetting setting, Type type, string exMessage)
        {
            var text = "Ошибка: не удалось";

            if (setting == null)
            {
                text = $"{text} загрузить настройки SyncSettings для SyncDOEntity.PK = {syncEntityPK}.";
            }
            else if (type == null)
            {
                text = $"{text} получить тип из настройки SyncSettings.PK - {setting.__PrimaryKey}.";
            }

            return($"{text}{Environment.NewLine}{exMessage}");
        }
        protected override object[] MapDetails(DetailArray source, SyncSetting setting, List <ISync> detailSeparatedMasters)
        {
            Document[] result  = null;
            var        details = base.MapDetails(source, setting, detailSeparatedMasters);

            if (details != null)
            {
                result = new Document[details.Length];
                for (int i = 0; i < details.Length; i++)
                {
                    result[i] = (Document)details[i];
                }
            }

            return(result);
        }
示例#13
0
 private void listBoxItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
 {
     object selectedItem = this.SyncHistoryListBox.SelectedItem;
     if (selectedItem != null)
     {
         string jobId = this.syncRecordDict[(ListBoxItem)selectedItem];
         SyncSetting syncSetting = SyncSetting.LoadSyncSettingByJobId(jobId);
         if (syncSetting != null)
         {
             this.mainWindow.GotoSyncSettingPage(syncSetting);
         }
         else
         {
             Log.Error("load sync setting by id failed, " + jobId);
         }
     }
 }
示例#14
0
        /// <summary>
        /// load sync settings, this method is called before the page loaded method
        /// </summary>
        /// <param name="syncSetting"></param>
        public void LoadSyncSetting(SyncSetting syncSetting)
        {
            if (syncSetting == null)
            {
                isLoadedFromRecord = false;
                this.syncSetting   = new SyncSetting();
            }
            else
            {
                isLoadedFromRecord = true;
                this.syncSetting   = syncSetting;
            }

            // 初始化bucketManager
            var mac = new Qiniu.Util.Mac(SystemConfig.ACCESS_KEY, SystemConfig.SECRET_KEY);

            this.bucketManager = new BucketManager(mac);
        }
        private static string GetErrorText(string syncType, SyncSetting setting, Type type, IPropertyMapper mapper, string exMessage)
        {
            var text = "Ошибка: не удалось";

            if (setting == null)
            {
                text = $"{text} загрузить настройки SyncSettings для входящего типа = {syncType}.";
            }
            else if (type == null)
            {
                text = $"{text} получить исходящий тип из настройки SyncSettings.PK - {setting.__PrimaryKey}.";
            }
            else if (mapper == null)
            {
                text = $"{text} получить маппер из настройки SyncSettings.PK - {setting.__PrimaryKey}.";
            }

            return($"{text}{Environment.NewLine}{exMessage}");
        }
示例#16
0
        private static bool SyncExportersToPayslipDb(EpbPayslipDbEntities payslipDb, List <Exporter> payslipExporters,
                                                     SyncSetting syncSettings)
        {
            foreach (var payslipExporter in payslipExporters)
            {
                var exporter = payslipDb.Exporters.Find(payslipExporter.Id);

                Console.WriteLine(payslipExporter.CompanyOrFactoryName);

                if (exporter == null)
                {
                    payslipDb.Exporters.Add(payslipExporter);
                    syncSettings.NewSyncEntry += 1;
                }
                else
                {
                    var modifiedExporterData = ConvertionService.ModifiedExporterData(exporter, payslipExporter);
                    payslipDb.Entry(modifiedExporterData).State = EntityState.Modified;
                    syncSettings.ModifiedSyncEntry += 1;
                }

                try
                {
                    syncSettings.Skip += 1;
                    payslipDb.SaveChanges();
                }
                catch (Exception exception)
                {
                    WriteExceptionLog(exception);
                    Console.WriteLine(exception.Message);
                }
            }
            try
            {
                return(payslipDb.SaveChanges() > 0);
            }
            catch (Exception exception)
            {
                WriteExceptionLog(exception);
                return(false);
            }
        }
示例#17
0
        //this is called before page loaded
        internal void LoadSyncSetting(SyncSetting syncSetting)
        {
            this.syncSetting = syncSetting;

            string jobName = string.Join("\t", new string[] { syncSetting.SyncLocalDir, syncSetting.SyncTargetBucket });

            this.jobId = Tools.md5Hash(jobName);

            string myDocPath = AppDomain.CurrentDomain.BaseDirectory;

            this.jobsDbPath      = System.IO.Path.Combine(myDocPath, "qsunsync", "jobs.db");
            this.jobLogDir       = System.IO.Path.Combine(myDocPath, "qsunsync", "logs", jobId);
            this.localHashDBPath = System.IO.Path.Combine(myDocPath, "qsunsync", "hash.db");
            this.cacheDir        = System.IO.Path.Combine(myDocPath, "qsunsync", "dircache");
            this.syncLogDir      = System.IO.Path.Combine(myDocPath, "qsunsync", "synclog");
            this.syncLogDBPath   = System.IO.Path.Combine(this.syncLogDir, jobId + ".log.db");

            string cacheId = jobId;

            this.cacheFilePathDone = System.IO.Path.Combine(cacheDir, cacheId + ".done");
            this.cacheFilePathTemp = System.IO.Path.Combine(cacheDir, cacheId + ".temp");
        }
        public static void SetLanguage(int indexFromDropDown)
        {
            SyncSetting.settings.Set("Language", indexFromDropDown.ToString());

            switch (indexFromDropDown)
            {
            case 1:
                var resourceName = $"SoundCloud_Playlist_Sync_4.Language.{SyncSetting.LoadSettingFromConfig("Language_Korean")}.txt";
                var stream       = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
                var list         = new List <string>();
                using (var sr = new StreamReader(stream))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        list.Add(line);
                    }
                }
                SetLanguageDictionary(list); break;

            default: SetLanguageDictionaryForDefaultLanguage(); break;
            }
        }
示例#19
0
        /// <summary>
        ///     Получить настройки по входящему типу.
        /// </summary>
        /// <param name="source">входящий объект</param>
        /// <param name="destType">Тип объекта, который был изменён.</param>
        /// <param name="mapper">Маппер для объекта, который был изменён.</param>
        private void GetSyncSettings(ISync source, out Type destType, out IPropertyMapperWithChangedAttrs mapper)
        {
            SyncSetting setting = null;

            destType = null;
            mapper   = null;
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            try
            {
                setting  = SettingService.Current.GetSettings(source).First();
                destType = setting.Destination.ExtractType();
                mapper   = setting.ExtractMapper <IPropertyMapperWithChangedAttrs>();
            }
            catch (Exception ex)
            {
                var text = "Ошибка: не удалось";
                if (setting == null)
                {
                    text = $"{text} загрузить настройки SyncSettings для входящего типа = {source.GetType().FullName}.";
                }
                else if (destType == null)
                {
                    text = $"{text} получить исходящий тип из настройки SyncSettings.PK - {setting.__PrimaryKey}.";
                }
                else if (mapper == null)
                {
                    text = $"{text} получить маппер из настройки SyncSettings.PK - {setting.__PrimaryKey}.";
                }

                throw new Exception($"{text}{Environment.NewLine}{ex.Message}");
            }
        }
 public LoadSyncSettingException(Guid?syncEntityPK, SyncSetting setting, Type type, IPropertyMapper mapper, string exMessage)
     : base(GetErrorText(syncEntityPK, setting, type, mapper, exMessage))
 {
 }
示例#21
0
        /// <summary>
        /// 1.根据设定参数进行必要的配置
        /// 2.检查过滤之后,生成待上传的文件列表并展示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonCheckFilesToUpload_Click(object sender, RoutedEventArgs e)
        {
            #region CHECK_SYNC_SETTINS

            // 检查并设置AK&SK
            if (string.IsNullOrEmpty(this.account.AccessKey) || string.IsNullOrEmpty(this.account.SecretKey))
            {
                this.SettingsErrorTextBlock.Text = "请设置AK&SK";
                return;
            }

            SystemConfig.ACCESS_KEY = this.account.AccessKey;
            SystemConfig.SECRET_KEY = this.account.SecretKey;

            // 检查本地同步目录与远程同步空间
            string syncDirectory = this.SyncLocalFolderTextBox.Text.Trim();
            if (string.IsNullOrEmpty(syncDirectory) || !Directory.Exists(syncDirectory))
            {
                this.SettingsErrorTextBlock.Text = "请设置本地待同步的目录";
                return;
            }
            if (this.SyncTargetBucketsComboBox.SelectedIndex < 0)
            {
                this.SettingsErrorTextBlock.Text = "请设置目标空间";
                return;
            }

            string targetBucket = this.SyncTargetBucketsComboBox.SelectedItem.ToString();

            if (this.syncSetting == null)
            {
                this.syncSetting = new SyncSetting();
            }

            this.syncSetting.LocalDirectory       = syncDirectory;
            this.syncSetting.TargetBucket         = targetBucket;
            this.syncSetting.SyncPrefix           = this.PrefixTextBox.Text.Trim();
            this.syncSetting.SkipPrefixes         = this.SkipPrefixesTextBox.Text.Trim();
            this.syncSetting.SkipSuffixes         = this.SkipSuffixesTextBox.Text.Trim();
            this.syncSetting.CheckNewFiles        = this.CheckNewFilesCheckBox.IsChecked.Value;
            this.syncSetting.UseShortFilename     = this.CheckBoxUseShortFilename.IsChecked.Value;
            this.syncSetting.OverwriteDuplicate   = this.RadioButtonOverwriteDuplicate.IsChecked.Value;
            this.syncSetting.SyncThreadCount      = (int)this.ThreadCountSlider.Value;
            this.syncSetting.ChunkUploadThreshold = (int)this.ChunkUploadThresholdSlider.Value * 1024 * 1024;
            this.syncSetting.DefaultChunkSize     = this.defaultChunkSize;
            this.syncSetting.UploadFromCDN        = this.RadioButtonFromCDN.IsChecked.Value;

            #endregion CHECK_SYNC_SETTINS

            #region SIMULATION

            StatResult statResult = this.bucketManager.stat(this.syncSetting.TargetBucket, "NONE_EXIST_KEY");

            if (statResult.ResponseInfo.isNetworkBroken())
            {
                this.SettingsErrorTextBlock.Text = "网络故障";
                return;
            }

            if (statResult.ResponseInfo.StatusCode == 401)
            {
                //ak & sk not right
                this.SettingsErrorTextBlock.Text = "AK 或 SK 不正确";
                return;
            }
            else if (statResult.ResponseInfo.StatusCode == 631)
            {
                //bucket not exist
                this.SettingsErrorTextBlock.Text = "指定空间不存在";
                return;
            }
            else if (statResult.ResponseInfo.StatusCode == 612 ||
                     statResult.ResponseInfo.StatusCode == 200)
            {
                //file exists or not
                //ignore
            }
            else if (statResult.ResponseInfo.StatusCode == 400)
            {
                if (string.IsNullOrEmpty(statResult.ResponseInfo.Error))
                {
                    this.SettingsErrorTextBlock.Text = "未知错误(状态代码400)";
                }
                else
                {
                    if (statResult.ResponseInfo.Error.Equals("incorrect zone"))
                    {
                        this.SettingsErrorTextBlock.Text = "上传入口机房设置错误";
                    }
                    else
                    {
                        this.SettingsErrorTextBlock.Text = statResult.ResponseInfo.Error;
                    }
                }
                return;
            }
            else
            {
                this.SettingsErrorTextBlock.Text = "未知错误,请联系七牛";
                Log.Error(string.Format("get buckets unknown error, {0}:{1}:{2}:{3}", statResult.ResponseInfo.StatusCode,
                                        statResult.ResponseInfo.Error, statResult.ResponseInfo.ReqId, statResult.Response));
                return;
            }

            #endregion SIMULATION

            int numFiles  = 0; // 总文件数
            int numUpload = 0; // 待上传文件数
            uploadItems.Clear();
            this.FilesToUploadDataGrid.DataContext = null;

            List <string> localFiles   = new List <string>(); // 本地待上传的文件
            List <string> saveKeys     = new List <string>(); // 保存到空间文件名
            List <string> fileEtags    = new List <string>(); // 待上传文件的ETAG
            List <long>   lastModified = new List <long>();   // 文件最后修改时间
            List <bool>   fileSkip     = new List <bool>();   // 是否跳过该文件(不上传)

            long T0 = (TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0))).Ticks;

            #region TRAVERSE_LOCAL_DIRECTORY

            DirectoryInfo di  = new DirectoryInfo(syncDirectory);
            FileInfo[]    ffi = di.GetFiles("*.*", SearchOption.AllDirectories);
            numFiles = ffi.Length;

            string savePrefix = this.PrefixTextBox.Text.Trim();

            if (this.syncSetting.UseShortFilename)
            {
                foreach (var fi in ffi)
                {
                    localFiles.Add(fi.FullName);
                    saveKeys.Add(savePrefix + fi.Name);
                    fileEtags.Add("_");
                    lastModified.Add((fi.LastWriteTime.Ticks - T0) / 10000);
                    fileSkip.Add(false);
                }
            }
            else
            {
                foreach (var fi in ffi)
                {
                    localFiles.Add(fi.FullName);
                    saveKeys.Add(savePrefix + fi.FullName);
                    fileEtags.Add("_");
                    lastModified.Add((fi.LastWriteTime.Ticks - T0) / 10000);
                    fileSkip.Add(false);
                }
            }

            #endregion TRAVERSE_LOCAL_DIRECTORY

            #region CHECK_PREFIX_SUFFX

            string skipPrefixes = this.SkipPrefixesTextBox.Text.Trim();
            string skipSuffixes = this.SkipSuffixesTextBox.Text.Trim();

            for (int i = 0; i < numFiles; ++i)
            {
                string saveKey = saveKeys[i];

                string[] ssPrfx = skipPrefixes.Split(',');
                foreach (string prefix in ssPrfx)
                {
                    if (!string.IsNullOrWhiteSpace(prefix))
                    {
                        if (saveKey.StartsWith(prefix.Trim()))
                        {
                            fileSkip[i] = true;
                            break;
                        }
                    }
                }

                string[] ssSufx = skipSuffixes.Split(',');
                foreach (string suffix in ssSufx)
                {
                    if (!string.IsNullOrWhiteSpace(suffix))
                    {
                        if (saveKey.EndsWith(suffix.Trim()))
                        {
                            fileSkip[i] = true;
                            break;
                        }
                    }
                }
            }
            #endregion CHECK_PREFIX_SUFFX

            string hashDBFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "qsunsync", "local_hash.db");
            if (!File.Exists(hashDBFile))
            {
                CachedHash.CreateCachedHashDB(hashDBFile);
            }
            SQLiteConnection localHashDBConn = new SQLiteConnection(string.Format("data source = {0}", hashDBFile));
            localHashDBConn.Open();
            Dictionary <string, HashDBItem> localHashDict = CachedHash.GetAllItems(localHashDBConn);
            localHashDBConn.Close();

            #region CHECK_LOCAL_DUPLICATE

            if (this.syncSetting.CheckNewFiles)
            {
                for (int i = 0; i < numFiles; ++i)
                {
                    if (fileSkip[i])
                    {
                        continue;
                    }

                    string fileName = localFiles[i];

                    if (localHashDict.ContainsKey(fileName))
                    {
                        string oldEtag = localHashDict[fileName].FileHash;
                        string newEtag = QETag.hash(fileName);

                        if (string.Equals(oldEtag, newEtag))
                        {
                            fileSkip[i] = true;
                        }
                        else
                        {
                            fileEtags[i] = newEtag;
                        }
                    }
                    else
                    {
                        fileEtags[i] = QETag.hash(fileName);
                    }
                }
            }
            else
            {
                for (int i = 0; i < numFiles; ++i)
                {
                    if (fileSkip[i])
                    {
                        continue;
                    }
                    fileEtags[i] = QETag.hash(localFiles[i]);
                }
            }

            #endregion CHECK_LOCAL_DUPLICATE

            #region CHECK_REMOTE_DUPLICATES

            // 如果选择了“强制覆盖”,那么无需进行远程检查
            if (!this.syncSetting.OverwriteDuplicate)
            {
                List <string> remoteHash   = new List <string>();
                List <long>   remoteUpdate = new List <long>();

                try
                {
                    Mac mac = new Mac(this.account.AccessKey, this.account.SecretKey);
                    BucketFileHash.BatchStat(mac, targetBucket, saveKeys, fileSkip, ref remoteHash, ref remoteUpdate);

                    for (int i = 0, k = 0; i < numFiles; ++i)
                    {
                        if (fileSkip[i])
                        {
                            continue;
                        }

                        if (string.Equals(fileEtags[i], remoteHash[k]))
                        {
                            // 云端已存在相同文件,跳过
                            fileSkip[i] = true;
                        }

                        ++k;
                    }
                }
                catch (Exception ex)
                {
                    this.SettingsErrorTextBlock.Text = ex.Message;
                    Log.Error(ex.Message);
                }
            }

            #endregion CHECK_REMOTE_DUPLICATES

            #region SHOW_UPLOAD_DETAILS

            this.SyncSettingTabControl.SelectedItem = this.TabItemFilesToUploadDetail;

            numUpload = numFiles;

            foreach (var b in fileSkip)
            {
                if (b)
                {
                    --numUpload;
                }
            }

            if (numUpload < 1)
            {
                TextBlockFilesToUploadSummery.Text = "没有待上传的文件";
                return;
            }


            double N = 0;

            for (int i = 0; i < numFiles; ++i)
            {
                if (fileSkip[i])
                {
                    continue;
                }

                string fsize = "0";

                long   n = ffi[i].Length;
                double K = 1.0 * n / 1024.0;
                double M = 0.0;
                if (K > 1024.0)
                {
                    M     = K / 1024.0;
                    fsize = string.Format("{0:0.00}MB", M);
                }
                else if (K > 1.0)
                {
                    fsize = string.Format("{0:0.00}KB", K);
                }
                else
                {
                    fsize = string.Format("{0}B", n);
                }

                N += n;

                uploadItems.Add(new UploadItem()
                {
                    LocalFile  = localFiles[i],
                    SaveKey    = saveKeys[i],
                    FileSize   = fsize,
                    FileHash   = fileEtags[i],
                    LastUpdate = lastModified[i].ToString()
                });
            }

            string vol  = "";
            double mega = 1024.0 * 1024;
            double kilo = 1024.0;
            if (N > mega)
            {
                vol = string.Format("{0:0.00}MB", N / mega);
            }
            else if (N > kilo)
            {
                vol = string.Format("{0:0.00}KB", N / kilo);
            }
            else
            {
                vol = string.Format("{0}B", N);
            }

            TextBlockFilesToUploadSummery.Text = string.Format("待上传的文件总数:{0}, 总大小:{1}", numUpload, vol);

            Dispatcher.Invoke(new Action(delegate
            {
                ObservableCollection <UploadItem> dataSource = new ObservableCollection <UploadItem>();
                foreach (var d in uploadItems)
                {
                    dataSource.Add(d);
                }
                this.FilesToUploadDataGrid.DataContext = dataSource;
            }));

            ButtonStartSync.IsEnabled = true;

            #endregion SHOW_UPLOAD_DETAILS
        }
示例#22
0
        void deleteJobMenuItem_Click(object sender, RoutedEventArgs e)
        {
            object selectedItem = this.SyncHistoryListBox.SelectedItem;

            if (selectedItem != null)
            {
                string      jobId       = this.syncRecordDict[(ListBoxItem)selectedItem];
                SyncSetting syncSetting = SyncRecord.LoadSyncSettingByJobId(jobId);
                if (syncSetting != null)
                {
                    MessageBoxResult mbr = MessageBox.Show(
                        string.Format("确认删除同步任务 {0} -> {1} 么?", syncSetting.LocalDirectory, syncSetting.TargetBucket), "删除任务",
                        MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (mbr.Equals(MessageBoxResult.Yes))
                    {
                        //delete job related files
                        string[] filesToDelete =
                        {
                            Path.Combine(this.myAppPath, "logs",     jobId,              "error.log"),
                            Path.Combine(this.myAppPath, "logs",     jobId,              "exists.log"),
                            Path.Combine(this.myAppPath, "logs",     jobId,              "not_overwrite.log"),
                            Path.Combine(this.myAppPath, "logs",     jobId,              "overwrite.log"),
                            Path.Combine(this.myAppPath, "logs",     jobId,              "skipped.log"),
                            Path.Combine(this.myAppPath, "logs",     jobId,              "success.log"),
                            Path.Combine(this.myAppPath, "synclog",  jobId + ".log.db"),
                            Path.Combine(this.myAppPath, "dircache", jobId + ".done")
                        };

                        foreach (string path in filesToDelete)
                        {
                            try
                            {
                                File.Delete(path);
                            }
                            catch (Exception ex)
                            {
                                Log.Error(string.Format("delete file {0} failed due to {1}", path, ex.Message));
                            }
                        }

                        string[] foldersToDelete =
                        {
                            Path.Combine(this.myAppPath, "logs", jobId)
                        };

                        foreach (string path in foldersToDelete)
                        {
                            try
                            {
                                Directory.Delete(path);
                            }
                            catch (Exception ex)
                            {
                                Log.Error(string.Format("delete folder {0} failed due to {1}", path, ex.Message));
                            }
                        }

                        try
                        {
                            SyncRecord.DeleteSyncJobById(jobId, this.jobsDbPath);
                        }
                        catch (Exception ex)
                        {
                            Log.Error("delete sync job by id error, " + ex.Message);
                        }

                        this.SyncHistoryListBox.Items.Remove(selectedItem);
                        this.syncRecordDict.Remove((ListBoxItem)selectedItem);
                    }
                }
                else
                {
                    Log.Error("load sync setting by id failed, " + jobId);
                }
            }
        }
        public static void SetLanguage(int indexFromDropDown)
        {
            SyncSetting.settings.Set("Language", indexFromDropDown.ToString());
            switch (indexFromDropDown)
            {
            case 1: SetLanguageDictionary(File.ReadAllLines(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "Language", SyncSetting.LoadSettingFromConfig("Language_Korean") + ".txt"))); break;

            default: SetLanguageDictionaryForDefaultLanguage(); break;
            }
        }
示例#24
0
 public MappingException(DataObject appObject, SyncSetting setting, IPropertyMapper mapper, string exMessage)
     : base(GetErrorText(appObject, setting, mapper, exMessage))
 {
 }
示例#25
0
        public static void Receive(SiteDb SiteDb, Kooboo.Sites.Sync.SyncObject SyncObject, SyncSetting setting = null, Guid UserId = default(Guid))
        {
            if (SyncObject == null)
            {
                return;
            }

            if (SyncObject.IsTable)
            {
                ReceiveTableData(SiteDb, SyncObject, setting, UserId);
            }
            else
            {
                ReceiveSiteObject(SiteDb, SyncObject, setting, UserId);
            }
        }
示例#26
0
        private static void ReceiveSiteObject(SiteDb SiteDb, SyncObject SyncObject, SyncSetting setting, Guid UserId)
        {
            var repo = GetRepository(SiteDb, SyncObject);

            if (repo != null)
            {
                if (SyncObject.IsDelete)
                {
                    var obj = repo.Get(SyncObject.ObjectId);
                    if (obj != null)
                    {
                        repo.Delete(SyncObject.ObjectId, UserId);

                        if (setting != null && Attributes.AttributeHelper.IsCoreObject(repo.ModelType))
                        {
                            var logid = GetJustDeletedVersion(SiteDb, repo, SyncObject.ObjectId);
                            if (logid > -1)
                            {
                                SiteDb.Synchronization.AddOrUpdate(new Synchronization {
                                    SyncSettingId = setting.Id, StoreName = repo.StoreName, ObjectId = SyncObject.ObjectId, In = true, Version = logid, RemoteVersion = SyncObject.SenderVersion
                                });
                            }
                        }
                    }
                }
                else
                {
                    var siteobject = Kooboo.Sites.Sync.SyncObjectConvertor.FromSyncObject(SyncObject);
                    if (siteobject is ICoreObject)
                    {
                        var core = siteobject as ICoreObject;
                        core.Version = -1;

                        bool ok = repo.AddOrUpdate(core, UserId);

                        if (ok && setting != null)
                        {
                            var localversion = core.Version;
                            if (localversion == -1)
                            {
                                var currentsiteobject = core as SiteObject;
                                if (currentsiteobject != null)
                                {
                                    var dbobject = repo.Get(currentsiteobject.Id);
                                    if (dbobject != null)
                                    {
                                        var dbcoreobject = dbobject as CoreObject;
                                        localversion = dbcoreobject.Version;
                                    }
                                }
                            }
                            SiteDb.Synchronization.AddOrUpdate(new Synchronization {
                                SyncSettingId = setting.Id, StoreName = repo.StoreName, ObjectId = siteobject.Id, Version = localversion, RemoteVersion = SyncObject.SenderVersion, In = true
                            });
                        }
                    }
                    else
                    {
                        repo.AddOrUpdate(siteobject);
                    }
                }
            }
        }
        private void StartSyncButton_EventHandler(object sender, RoutedEventArgs e)
        {
            this.SyncSettingTabControl.SelectedIndex = 0;
            //check ak & sk
            if (string.IsNullOrEmpty(this.account.AccessKey) ||
                string.IsNullOrEmpty(this.account.SecretKey))
            {
                this.SettingsErrorTextBlock.Text = "请返回设置 AK & SK";
                return;
            }

            //save config to job record
            if (this.SyncLocalFolderTextBox.Text.Trim().Length == 0)
            {
                this.SettingsErrorTextBlock.Text = "请选择本地待同步目录";
                return;
            }

            if (this.SyncTargetBucketsComboBox.SelectedIndex == -1)
            {
                this.SettingsErrorTextBlock.Text = "请选择同步的目标空间";
                return;
            }

            string syncLocalDir = this.SyncLocalFolderTextBox.Text.Trim();

            if (!Directory.Exists(syncLocalDir))
            {
                //directory not found
                this.SyncSettingTabControl.SelectedIndex = 0;
                this.SettingsErrorTextBlock.Text         = "本地待同步目录不存在";
                return;
            }

            string     syncTargetBucket = this.SyncTargetBucketsComboBox.SelectedItem.ToString();
            StatResult statResult       = this.bucketManager.Stat(syncTargetBucket, "NONE_EXIST_KEY");

            if (statResult.Code == 401)
            {
                //ak & sk not right
                this.SettingsErrorTextBlock.Text = "AK 或 SK 不正确";
                return;
            }
            else if (statResult.Code == 631)
            {
                //bucket not exist
                this.SettingsErrorTextBlock.Text = "指定空间不存在";
                return;
            }
            else if (statResult.Code == 612 ||
                     statResult.Code == 200)
            {
                //file exists or not
                //ignore
            }
            else
            {
                this.SettingsErrorTextBlock.Text = "网络故障";
                Log.Error(string.Format("get buckets unknown error, {0}:{1}:{2}:{3}", statResult.Code,
                                        statResult.Text, statResult.RefInfo["X-Reqid"], System.Text.Encoding.UTF8.GetString(statResult.Data)));
                return;
            }

            //set progress ak & sk
            SystemConfig.ACCESS_KEY = this.account.AccessKey;
            SystemConfig.SECRET_KEY = this.account.SecretKey;

            //optional settings
            SyncSetting syncSetting = new SyncSetting();

            syncSetting.SyncLocalDir         = syncLocalDir;
            syncSetting.SyncTargetBucket     = syncTargetBucket;
            syncSetting.CheckRemoteDuplicate = this.CheckRemoteDuplicateCheckBox.IsChecked.Value;
            syncSetting.SyncPrefix           = this.PrefixTextBox.Text.Trim();
            syncSetting.CheckNewFiles        = this.CheckNewFilesCheckBox.IsChecked.Value;
            syncSetting.IgnoreDir            = this.IgnoreDirCheckBox.IsChecked.Value;
            syncSetting.SkipPrefixes         = this.SkipPrefixesTextBox.Text.Trim();
            syncSetting.SkipSuffixes         = this.SkipSuffixesTextBox.Text.Trim();
            syncSetting.OverwriteFile        = this.OverwriteFileCheckBox.IsChecked.Value;
            syncSetting.SyncThreadCount      = (int)this.ThreadCountSlider.Value;
            syncSetting.ChunkUploadThreshold = (int)this.ChunkUploadThresholdSlider.Value * 1024 * 1024;
            syncSetting.DefaultChunkSize     = this.defaultChunkSizeIndex;
            syncSetting.UploadEntryDomain    = this.uploadEntryDomain;

            this.mainWindow.GotoSyncProgress(syncSetting);
        }
示例#28
0
 //go to sync setting page
 internal void GotoSyncSettingPage(SyncSetting syncSetting)
 {
     this.MainHostFrame.Content = this.syncSettingPage;
     this.syncSettingPage.LoadSyncSetting(syncSetting);
 }
 /// <summary>
 /// load sync settings, this method is called before the page loaded method
 /// </summary>
 /// <param name="syncSetting"></param>
 public void LoadSyncSetting(SyncSetting syncSetting)
 {
     this.syncSetting   = syncSetting;
     this.bucketManager = null;
 }
示例#30
0
        private static void ReceiveTableData(SiteDb SiteDb, SyncObject SyncObject, SyncSetting setting, Guid UserId)
        {
            var table = Data.DB.GetOrCreateTable(SiteDb.WebSite, SyncObject.TableName);

            if (table != null)
            {
                if (SyncObject.IsDelete)
                {
                    bool deleteOk = table.Delete(SyncObject.ObjectId);

                    if (deleteOk && setting != null)
                    {
                        var logid = GetJustDeletedVersion(SiteDb, table.Name, SyncObject.ObjectId);
                        if (logid > -1)
                        {
                            SiteDb.Synchronization.AddOrUpdate(new Synchronization {
                                SyncSettingId = setting.Id, TableName = table.Name, ObjectId = SyncObject.ObjectId, In = true, Version = logid, RemoteVersion = SyncObject.SenderVersion
                            });
                        }
                    }
                }
                else
                {
                    var data = Kooboo.Sites.Sync.SyncObjectConvertor.FromTableSyncObject(SyncObject);

                    if (data == null || (data.Count == 1 && data.ContainsKey("_id")))
                    {
                        Kooboo.Data.Log.Instance.Exception.Write("null pull table data/r/n" + Lib.Helper.JsonHelper.Serialize(setting) + Lib.Helper.JsonHelper.Serialize(SyncObject));
                        return;
                    }

                    Guid TableItemKey = SyncObject.ObjectId;
                    var  item         = table.Get(TableItemKey);

                    bool updateok = false;
                    if (item != null)
                    {
                        if (!string.IsNullOrWhiteSpace(SyncObject.TableColName))
                        {
                            object value = null;
                            if (data.ContainsKey(SyncObject.TableColName))
                            {
                                value = data[SyncObject.TableColName];
                            }
                            updateok = table.UpdateColumn(SyncObject.ObjectId, SyncObject.TableColName, value);
                        }
                        else
                        {
                            updateok = table.Update(SyncObject.ObjectId, data);
                        }
                    }
                    else
                    {
                        TableItemKey = table.Add(data, true);
                        updateok     = TableItemKey != default(Guid);
                    }

                    if (updateok && setting != null)
                    {
                        var AddLog = table.OwnerDatabase.Log.GetLastLogByTableNameAndKey(table.Name, TableItemKey);

                        if (AddLog != null)
                        {
                            SiteDb.Synchronization.AddOrUpdate(new Synchronization {
                                SyncSettingId = setting.Id, TableName = table.Name, ObjectId = TableItemKey, Version = AddLog.Id, RemoteVersion = SyncObject.SenderVersion, In = true
                            });
                        }
                    }
                }
            }
        }