예제 #1
0
        //Разблокировка файла
        public static string UnlockFile(string sessionId, string path, string filename)
        {
            string            user      = FileServiceSession.GetUser(sessionId);
            string            localPath = path;
            FileSystemContext ctx       = new FileSystemContext();

            if (path == "")
            {
                localPath = GetPath(sessionId);
            }
            string      fullFilename = $"{localPath}{filename}";
            MdDirectory localDir     = PathExist(localPath);

            if (ctx.Files.Where(f => f.IdDirectory == localDir.Id && f.FileName == filename).Count() > 0)
            {
                MdFile localFile = ctx.Files.Where(f => f.IdDirectory == localDir.Id && f.FileName == filename).FirstOrDefault();
                if (ctx.Locks.Where(l => l.IdFile == localFile.Id && l.User == user).Count() > 0)
                {
                    ctx.Locks.Remove(ctx.Locks.Where(l => l.IdFile == localFile.Id && l.User == user).FirstOrDefault());
                    ctx.SaveChanges();
                }
                return($"{localPath}{filename}");
            }
            else
            {
                throw new FileServiceCommandExeption($"{filename} not exist in {localPath}");
            }
        }
예제 #2
0
        private void ExportMdFiles(MdFile mdFile, string targetFilePath)
        {
            if (mdFile.IsProtectIntroduction)
            {
                string introductionContent;
                if (!fileSystem.Exists(targetFilePath))
                {
                    fileSystem.Write(targetFilePath, string.Empty);

                    //TODO Test edilemedi
                    var caption = Path.GetFileNameWithoutExtension(targetFilePath);
                    introductionContent = string.Format(new TagConverters.Type(documentProcessor).Pattern, caption, string.Empty);
                }
                else
                {
                    var content = fileSystem.Read(targetFilePath);
                    introductionContent = content.Contains("#### Method:")
                                                ? content.Split(new[] { "#### Method:" }, StringSplitOptions.None)[0]
                                                : content;
                }

                fileSystem.Write(targetFilePath, string.Format("{0}{1}", introductionContent, mdFile.Content));
            }
            else
            {
                fileSystem.Write(targetFilePath, mdFile.Content);
            }
        }
예제 #3
0
        private static Mock <IXml2MdConverter> MockXml2MdConverter(MdFile mdFile)
        {
            var mockXml2MdConverter = new Mock <IXml2MdConverter>();

            mockXml2MdConverter.Setup(x2m => x2m.Convert(It.IsAny <string>())).Returns(() => new List <MdFile>()
            {
                mdFile
            });
            return(mockXml2MdConverter);
        }
예제 #4
0
        //Копирование файла
        public static string CopyFile(string sessionId, string filename, string source, string dest)
        {
            FileSystemContext ctx         = new FileSystemContext();
            string            localSource = "";

            if (source == "")
            {
                localSource = GetPath(sessionId);
            }
            else
            {
                localSource = formatPath(source.ToLower(), sessionId);
            }
            if (ctx.Directories.Where(d => d.Path == localSource).Count() == 0)
            {
                throw new FileServiceCommandExeption($" source {localSource} not exist");
            }
            string localDest = formatPath(dest.ToLower(), sessionId);

            if (ctx.Directories.Where(d => d.Path == localDest).Count() == 0)
            {
                throw new FileServiceCommandExeption($" destinationw {localDest} not exist");
            }
            MdDirectory copyDir     = ctx.Directories.Where(d => d.Path == localSource).FirstOrDefault();
            MdDirectory newDir      = ctx.Directories.Where(d => d.Path == localDest).FirstOrDefault();
            MdFile      currentFile = ctx.Files.Where(f => f.IdDirectory == copyDir.Id && f.FileName == filename).FirstOrDefault();

            if (currentFile == null)
            {
                throw new FileServiceCommandExeption($"{filename} not found");
            }
            else
            {
                MdFile dubFile = ctx.Files.Where(f => f.IdDirectory == newDir.Id && f.FileName == filename).FirstOrDefault();
                if (dubFile != null)
                {
                    CheckLock(sessionId, ctx, dubFile);
                    dubFile.Content = currentFile.Content.ToArray();
                }
                else
                {
                    ctx.Files.Add(new MdFile()
                    {
                        FileName = currentFile.FileName, IdDirectory = newDir.Id, Content = currentFile.Content.ToArray()
                    });
                }
                ctx.SaveChanges();
                return($"{filename} copied from {localSource} to {localDest}");
            }
        }
예제 #5
0
 //Проверка файла на наличие блокировки
 private static void CheckLock(string sessionId, FileSystemContext ctx, MdFile filename)
 {
     if (ctx.Locks.Where(l => l.IdFile == filename.Id).Count() > 0)
     {
         string      user  = FileServiceSession.GetUser(sessionId);
         MdDirectory dir   = ctx.Directories.Where(d => d.Id == filename.IdDirectory).FirstOrDefault();
         string      error = $"{dir.Path}{filename.FileName} locked by ";
         ctx.Locks.Where(l => l.IdFile == filename.Id
                         ).ToList().ForEach(l => {
             if (user == l.User)
             {
                 error += "Me,";
             }
             else
             {
                 error += $"{l.User},";
             }
         });
         throw new FileServiceCommandExeption(error.Trim(','));
     }
 }
예제 #6
0
        //Копирование директории
        private static void CopyMatches(string sessionId, FileSystemContext ctx, string copyPath, string newPath)
        {
            List <MdDirectory> copyList = ctx.Directories.Where(d => d.Path.Contains(copyPath)).ToList();

            copyList.ForEach(d => {
                List <string> fileSet  = new List <string>();
                List <MdFile> fileList = ctx.Files.Where(f => f.IdDirectory == d.Id).ToList();
                fileList.ForEach(f => {
                    fileSet.Add(f.FileName);
                });
                string path        = d.Path.Replace(copyPath, newPath);
                MdDirectory newDir = ctx.Directories.Where(nd => nd.Path == path).FirstOrDefault();
                if (newDir != null)
                {
                    List <MdFile> newDirFiles = ctx.Files.Where(f => f.IdDirectory == newDir.Id).ToList();
                    newDirFiles.ForEach(f => {
                        if (fileSet.IndexOf(f.FileName) >= 0)
                        {
                            CheckLock(sessionId, ctx, f);
                        }
                    });
                }
            });
            copyList.ForEach(d => {
                List <string> fileSet  = new List <string>();
                List <MdFile> fileList = ctx.Files.Where(f => f.IdDirectory == d.Id).ToList();
                fileList.ForEach(f => {
                    fileSet.Add(f.FileName);
                });
                string path        = d.Path.Replace(copyPath, newPath);
                MdDirectory newDir = ctx.Directories.Where(nd => nd.Path == path).FirstOrDefault();
                if (newDir != null)
                {
                    List <MdFile> newDirFiles = ctx.Files.Where(f => f.IdDirectory == newDir.Id).ToList();
                    newDirFiles.ForEach(f => {
                        if (fileSet.IndexOf(f.FileName) >= 0)
                        {
                            MdFile copyFile = fileList.Where(cf => cf.FileName == f.FileName).FirstOrDefault();
                            f.Content       = copyFile.Content.ToArray();
                            fileSet.Remove(f.FileName);
                        }
                    });
                    fileList.ForEach(f =>
                    {
                        if (fileSet.IndexOf(f.FileName) >= 0)
                        {
                            ctx.Files.Add(new MdFile()
                            {
                                FileName = f.FileName, IdDirectory = newDir.Id, Content = f.Content.ToArray()
                            });
                        }
                    });
                }
                else
                {
                    MdDirectory copyDir = new MdDirectory()
                    {
                        Path = path
                    };
                    ctx.Directories.Add(copyDir);
                    ctx.SaveChanges();
                    fileList.ForEach(f =>
                    {
                        ctx.Files.Add(new MdFile()
                        {
                            FileName = f.FileName, IdDirectory = copyDir.Id, Content = f.Content.ToArray()
                        });
                    });
                }
            });
            ctx.SaveChanges();
        }