Пример #1
0
        public ProjectFileViewModel(ProjectFilePath path, ShqContext db)
        {
            this.Name = System.IO.Path.GetFileName(path.Path);
            this.Path = path.Path;

            FileInfo fi = new FileInfo(path.FullPath);

            this.Size            = fi.Length;
            this.CreatedTime     = fi.CreationTime.ToString();
            this.LastModfiedTime = fi.LastWriteTime.ToString();

            Id = ShqConstants.GetPathId(path.Path);
            if (Id > 0)
            {
                this.Name = System.IO.Path.GetFileNameWithoutExtension(this.Name);

                var f = db.ProjectFiles.FirstOrDefault(item => item.Id == Id);
                if (f == null)
                {
                    this.Name = "";
                }
                else
                {
                    this.Privilege = f.Privilege;

                    CreatedBy   = new ShqUserRequestViewModel(db.ShqUsers.Where(u => u.IdentityUser.Id == db.ProjectFiles.FirstOrDefault(item => item.Id == Id).CreatedById).FirstOrDefault(), db);
                    CreatedTime = f.CreatedTime.ToString();

                    LastModifiedBy  = new ShqUserRequestViewModel(db.ShqUsers.Where(u => u.IdentityUser.Id == db.ProjectFiles.FirstOrDefault(item => item.Id == Id).LastModifiedById).FirstOrDefault(), db);
                    LastModfiedTime = f.LastModfiedTime.ToString();

                    this.Level = f.Level;
                }
            }
        }
        private bool CheckIfParentExistInDb(string parentPath)
        {
            if (parentPath.EndsWith("Root") == true)
            {
                return(true);
            }

            int id = ShqConstants.GetPathId(parentPath);

            if (id > 0)
            {
                return(db.ProjectFiles.FirstOrDefault(item => item.Id == id) != null);
            }

            return(false);
        }
        public async Task <IHttpActionResult> SyncProjectFiles(Guid projectId)
        {
            var pro = db.Projects.FirstOrDefault(item => item.Id == projectId);

            if (pro != null)
            {
                if (ProjectHelper.HasUpdateAccess(pro) == false)
                {
                    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "No Access"));
                }
            }
            else
            {
                return(NotFound());
            }

            string path = ShqConstants.ProjectRootFolder + "\\" + projectId;

            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }

            path = path + "\\Root";
            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }

            var wp          = db.WorkProjects.Include("WorkProjectTemplate").FirstOrDefault(item => item.ProjectId == projectId);
            var sourceFiles = wp.WorkProjectTemplate.ProjectFiles.Where(item => item.Status == (int)ShqConstants.FileStatus.Ready && (item.Level & wp.Level) == wp.Level).ToList();

            wp.FilesToCopyNum = sourceFiles.Count();
            wp.FilesCopiedNum = 0;

            string sourceRootFolder = Path.Combine(ShqConstants.TemplateRootFolder, wp.WorkProjectTemplate.Id.ToString()) + "\\Root";
            int    start            = sourceRootFolder.Split('\\').Length;

            foreach (var file in sourceFiles)
            {
                wp.FilesCopiedNum++;

                string[] folers   = file.Path.Split('\\');
                string   tempPath = path;
                int      to       = file.IsFolder == true ? folers.Length : folers.Length - 1;
                for (int i = start; i < to; i++)
                {
                    int id = ShqConstants.GetPathId(folers[i]);
                    var tf = db.ProjectFiles.FirstOrDefault(item => item.Id == id).FileId;

                    var f = db.ProjectFiles.FirstOrDefault(item => item.FileId == tf && item.WorkProjectTemplateId == wp.WorkProjectTemplateId && item.WorkProjectId == wp.Id);
                    if (f == null)
                    {
                        var projectFile = db.ProjectFiles.Add(new ProjectFile
                        {
                            FileId   = file.FileId,
                            Name     = file.Name,
                            Level    = file.Level,
                            IsFolder = file.IsFolder,
                            Path     = tempPath,
                            Status   = (int)ShqConstants.FileStatus.Copying,
                            WorkProjectTemplateId = wp.WorkProjectTemplateId,
                            WorkProjectId         = wp.Id,
                            CreatedById           = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId,
                            LastModifiedById      = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId
                        });

                        wp.ProjectFiles.Add(projectFile);

                        db.SaveChanges();
                        tempPath = Path.Combine(tempPath, file.Name + "." + projectFile.Id.ToString());
                        if (Directory.Exists(tempPath) == false)
                        {
                            Directory.CreateDirectory(tempPath);
                            projectFile.Path   = tempPath;
                            projectFile.Status = (int)ShqConstants.FileStatus.Ready;
                        }
                    }
                    else
                    {
                        if (f.Status != (int)ShqConstants.FileStatus.Deleted)
                        {
                            tempPath = Path.Combine(tempPath, f.Name + "." + f.Id.ToString());
                            if (Directory.Exists(tempPath) == false)
                            {
                                Directory.CreateDirectory(tempPath);
                                f.Status           = (int)ShqConstants.FileStatus.Ready;
                                f.LastModifiedById = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId;
                                f.LastModfiedTime  = DateTime.Now;
                            }
                        }
                    }
                }

                if (file.IsFolder == false)
                {
                    var f = db.ProjectFiles.FirstOrDefault(item => item.FileId == file.FileId && item.WorkProjectTemplateId == wp.WorkProjectTemplateId && item.WorkProjectId == wp.Id);
                    if (f == null)
                    {
                        var projectFile = db.ProjectFiles.Add(new ProjectFile
                        {
                            FileId   = file.FileId,
                            Name     = file.Name,
                            Level    = file.Level,
                            IsFolder = file.IsFolder,
                            Path     = tempPath,
                            WorkProjectTemplateId = wp.WorkProjectTemplateId,
                            WorkProjectId         = wp.Id,
                            Status           = (int)ShqConstants.FileStatus.Copying,
                            CreatedById      = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId,
                            LastModifiedById = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId
                        });

                        wp.ProjectFiles.Add(projectFile);
                        db.SaveChanges();

                        string targetPath = tempPath + "\\" + projectFile.Name + "." + projectFile.Id;

                        if (File.Exists(targetPath) == false)
                        {
                            File.Copy(file.Path, targetPath);
                            projectFile.Status = (int)ShqConstants.FileStatus.Ready;
                            projectFile.Path   = targetPath;
                        }
                    }
                    else
                    {
                        if (f.Status != (int)ShqConstants.FileStatus.Deleted)
                        {
                            string targetPath = tempPath + "\\" + f.Name + "." + f.Id;

                            if (File.Exists(targetPath) == false)
                            {
                                File.Copy(file.Path, targetPath);
                                f.Status           = (int)ShqConstants.FileStatus.Ready;
                                f.Path             = targetPath;
                                f.LastModifiedById = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId;
                                f.LastModfiedTime  = DateTime.Now;
                            }
                        }
                    }
                }
            }

            await db.SaveChangesAsync();

            return(Ok());
        }
Пример #4
0
        public ProjectFolderViewModel(ProjectFilePath path, SearchOption searchOption, ShqContext db, int folderLevel = 1)
        {
            this.Path = path.Path;
            this.Name = System.IO.Path.GetFileName(path.Path);

            Id = ShqConstants.GetPathId(path.Path);
            if (Id > 0)
            {
                this.Name = System.IO.Path.GetFileNameWithoutExtension(this.Name);
                var f = db.ProjectFiles.FirstOrDefault(item => item.Id == Id);
                if (f == null)
                {
                    this.Name = "";
                }
                else
                {
                    CreatedBy   = new ShqUserRequestViewModel(db.ShqUsers.Where(u => u.IdentityUser.Id == db.ProjectFiles.FirstOrDefault(item => item.Id == Id).CreatedById).FirstOrDefault(), db);
                    CreatedTime = f.CreatedTime.ToString();

                    LastModifiedBy  = new ShqUserRequestViewModel(db.ShqUsers.Where(u => u.IdentityUser.Id == db.ProjectFiles.FirstOrDefault(item => item.Id == Id).LastModifiedById).FirstOrDefault(), db);
                    LastModfiedTime = f.LastModfiedTime.ToString();

                    this.Privilege = f.Privilege;
                    this.Level     = f.Level;
                }
            }

            if (this.Name == "")
            {
                return;
            }

            if ((searchOption == SearchOption.TopDirectoryOnly && folderLevel == 1) || searchOption == SearchOption.AllDirectories)
            {
                string[] directory, files;
                directory = Directory.GetDirectories(path.FullPath);
                files     = Directory.GetFiles(path.FullPath);

                if (directory != null && directory.Length > 0)
                {
                    foreach (var d in directory)
                    {
                        var pf = new ProjectFolderViewModel(new ProjectFilePath {
                            FullPath = d, Path = System.IO.Path.Combine(path.Path, System.IO.Path.GetFileName(d))
                        }, searchOption, db, folderLevel);
                        if (pf.Name != "")
                        {
                            this.SubFolders.Add(pf);
                        }
                    }

                    if (this.SubFolders != null && this.SubFolders.Count > 0)
                    {
                        this.SubFolders.Sort((x, y) => x.Name.CompareTo(y.Name));
                    }
                }

                if (files != null && files.Length > 0)
                {
                    foreach (var f in files)
                    {
                        var ff = new ProjectFileViewModel(new ProjectFilePath {
                            FullPath = f, Path = System.IO.Path.Combine(path.Path, System.IO.Path.GetFileName(f))
                        }, db);
                        if (ff.Name != "")
                        {
                            this.Files.Add(ff);
                        }
                    }

                    if (this.Files != null && this.Files.Count > 0)
                    {
                        this.Files.Sort((x, y) => x.Name.CompareTo(y.Name));
                    }
                }

                folderLevel--;
            }
        }