/// <summary>
 /// 同步文件夹内的文件
 /// </summary>
 /// <param name="parentFolder"></param>
 bool SynchronizingFile(AlbumFolder parentFolder)
 {
     bool result = ThreadAction.Instance.RunS(parentFolder).ConfigureAwait(false).GetAwaiter().GetResult();
     return result;
 }
 /// <summary>
 /// 同步子文件夹和子文件夹内的文件
 /// </summary>
 /// <param name="parentFolder"></param> 
 void SynchronizingChildFolderAndFile(AlbumFolder parentFolder)
 {
     QueryModel query = new QueryModel("RecordUser", base.CurrentLoginUser().UserName);
     query.AddAndQuery("ParentId", "=", parentFolder.Id);
     string json = this.superService.FindList<AlbumFolder>(query);
     List<AlbumFolder> subFolders = JsonConvert.DeserializeObject<List<AlbumFolder>>(json);//
     #region 同步文件夹
     DirectoryInfo dir = new DirectoryInfo(base.CurrentLoginUser().UserFolderPath + parentFolder.Path);
     DirectoryInfo[] folders = dir.GetDirectories();
     foreach (DirectoryInfo folder in folders)
     {
         //若数据库没有这个文件夹的记录,则新生成
         AlbumFolder record = subFolders.FirstOrDefault(item => item.DisplayName.Equals(folder.Name));
         if (record == null)
         {
             record = new AlbumFolder()
             {
                 AlbumType = parentFolder.AlbumType,
                 FolderType = 0,
                 DisplayName = folder.Name,
                 ParentId = parentFolder.Id,
                 RecordDate = DateTime.Now,
                 RecordUser = base.CurrentLoginUser().UserName,
                 Path = parentFolder.Path + "\\" + folder.Name
             };
             this.superService.Insert<AlbumFolder>(record);
         }
         this.SynchronizingFile(record);//太多了内存撑不住
     }
     #endregion
 }
        public JsonResult Create(AlbumFolder model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (!base.CheckLogin())
                        Redirect(System.Configuration.ConfigurationManager.AppSettings["baseUrl"].ToString());
                    string uploadPath = string.Empty;
                    if (string.IsNullOrWhiteSpace(model.Path))
                        uploadPath = base.CurrentLoginUser().UserFolderPath + "Album\\" + model.DisplayName;
                    else
                        uploadPath = base.CurrentLoginUser().UserFolderPath + model.Path + "\\" + model.DisplayName;

                    if (Directory.Exists(uploadPath))
                        return Json(new { result = false, text = "Folder is exist!", callbackapi = " " });

                    Directory.CreateDirectory(uploadPath);
                    //model.RecordUser = base.CurrentLoginUser().UserName;
                    model.Path = string.IsNullOrWhiteSpace(model.Path) ? "Album\\" + model.DisplayName : model.Path + "\\" + model.DisplayName;
                    // model.RecordDate = DateTime.Now;
                    bool jsonResult = this.superService.Insert<AlbumFolder>(model); // SuperManager.Instance.Create<Album>(model);
                    BootstrapMvc2015GitHub.Framework.Log.Instance.RecordActionLog(new System.Diagnostics.StackFrame(0).GetMethod().Name, this.ServiceKey, "Media", jsonResult);
                    return Json(new
                    {
                        result = jsonResult,
                        text = jsonResult ? "One record created successfully!" : "something was wrong!",
                        callbackapi = "Media/Album/Index?parentId=" + model.ParentId
                    });
                }
                catch (Exception ex)
                {
                    BootstrapMvc2015GitHub.Framework.Log.Instance.RecordActionLog(new System.Diagnostics.StackFrame(0).GetMethod().Name, this.ServiceKey, "Media", false, ex.Message);
                    throw ex;
                }
            }
            else
                throw new Exception("ModelState.IsValid is false");
        }
        public JsonResult Update(AlbumFolder model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string json = this.superService.FindOneById<AlbumFolder>(model.Id);
                    AlbumFolder record = JsonConvert.DeserializeObject<AlbumFolder>(json);// SuperManager.Instance.FindOneById<AlbumFolder>(model.Id);
                    Dictionary<string, object> updates = new Dictionary<string, object>() { { "AlbumType", model.AlbumType }, { "DisplayName", model.DisplayName }, { "FolderType", model.FolderType } };
                    // if (!record.DisplayName.Equals(model.DisplayName) && Directory.Exists(base.CurrentLoginUser().UserFolderPath + record.Path.Replace(record.DisplayName, model.DisplayName)))
                    //    return Json(new { result = false, text = "Folder is exist!", callbackapi = " " });

                    bool jsonResult = this.superService.UpdateFields<AlbumFolder>(updates, new QueryModel("id", model.Id)); // SuperManager.Instance.UpdateById<AlbumFolder>(update);
                    //if (jsonResult && !record.DisplayName.Equals(model.DisplayName))
                    //{
                    //    string path = base.CurrentLoginUser().UserFolderPath + record.Path;
                    //    RobinCore.Instance.ModifyDirectoryName(path, record.DisplayName, model.DisplayName);
                    //}
                    BootstrapMvc2015GitHub.Framework.Log.Instance.RecordActionLog(new System.Diagnostics.StackFrame(0).GetMethod().Name, this.ServiceKey, "Media", jsonResult);
                    return Json(new
                    {
                        result = jsonResult,
                        text = jsonResult ? "record  updated successfully!" : "something was wrong!",
                        callbackapi = "Media/Album/Index?parentId=" + model.ParentId
                    });
                }
                catch (Exception ex)
                {
                    BootstrapMvc2015GitHub.Framework.Log.Instance.RecordActionLog(new System.Diagnostics.StackFrame(0).GetMethod().Name, this.ServiceKey, "Media", false, ex.Message);
                    throw ex;
                }
            }
            else
                throw new Exception("ModelState.IsValid is false");
        }
 public Task<bool> RunS(AlbumFolder albumFolder)
 {
     return Task.Run(() =>
       {
       #region 同步文件
       string basePath = DataCache.Instance.LoginUser.UserFolderPath + albumFolder.Path;
       //get files in folder
       DirectoryInfo dir = new DirectoryInfo(basePath);
       if (!Directory.Exists(basePath))
           Directory.CreateDirectory(basePath);
       FileInfo[] files = dir.GetFiles();
       //get data in datatable
       QueryModel query = new QueryModel("UploadUserName", DataCache.Instance.LoginUser.UserName);
       query.AddAndQuery(new QueryInfo("FolderId", "=", albumFolder.Id));
       string json = UnityInstance.Instance.GetObject<ISuperService>().FindList<Album>(query);
       List<Album> albums = JsonConvert.DeserializeObject<List<Album>>(json);//
                                                                             //清空cache文件
       if (Directory.Exists(basePath + "\\cache"))
           Directory.Delete(basePath + "\\cache", true);
       Directory.CreateDirectory(basePath + "\\cache");
       //循环每一个图片生成缩略图,如果数据库有了则只生成缩略图,若没 有则插入到数据库
       foreach (FileInfo file in files)
       {
           if (!FileExtention.Instance.CheckImg(file)) continue;
           int width, height;
           FileExtention.Instance.MakeThumbnail(basePath, file.Name, out width, out height);
           Album data = albums.FirstOrDefault(item => item.FileName.Equals(file.Name));
           if (data == null)
           {
               Album record = new Album()
               {
                   FileName = file.Name,
                   FolderId = albumFolder.Id,
                   CreationTime = DateTime.Now,
                   Evaluation = Convert.ToInt32(EvaluationType.一般),
                   FileExtention = file.Extension.ToLower(),
                   FilePath = "User\\" + DataCache.Instance.LoginUser.UserName + "\\" + albumFolder.Path + "\\" + file.Name,
                   FileUrlPath = albumFolder.Path + "\\" + file.Name,
                   ThumbnailUrlPath = albumFolder.Path + "\\cache\\" + file.Name,
                   FileSize = file.Length,
                   UploadDate = DateTime.Now,
                   UploadUserDisplayName = DataCache.Instance.LoginUser.UserDisplayName,
                   UploadUserName = DataCache.Instance.LoginUser.UserName,
                   LastAccessTime = file.LastAccessTime,
                   ThumblWidth = width,
                   ThumblHeight = height
               };
               UnityInstance.Instance.GetObject<ISuperService>().Insert<Album>(record);// SuperManager.Instance.Create<Album>(record);
           }
           else
           {
               albums.Remove(data);
           }
       }
       List<string> toDelete = new List<string>();
       foreach (Album item in albums)
           toDelete.Add(item.Id);
       if (toDelete.Count > 0)
           UnityInstance.Instance.GetObject<ISuperService>().DeleteByIds<Album>(String.Join(",", toDelete));
       #endregion
       return true;
       });
 }