Пример #1
0
        //public FileRecordManager(SlimeDbContext tdb)
        //{
        //    db = tdb;
        //    postManager = new PostManager(db);
        //}
        public async Task <string> Create(int?BlogId, int?postid, Files filemodel, IFormFile filedata, string user)
        {
            try
            { string ap = null;
              if ((filedata != null) && (filemodel != null) && (!CommonTools.isEmpty(user)))
              {
                  var blog = await blogmngr.GetBlogByIdAsync(BlogId);

                  var post = await postManager.Details(postid);

                  ApplicationUser usr = (ApplicationUser)db.Users.First(m => m.UserName == user);
                  if ((blog != null))
                  {
                      var    blogpath = FileSystemManager.GetBlogRootDataFolderAbsolutePath(blog.Name);
                      string abspath  = await FileSystemManager.CreateFile(blogpath, filedata);

                      if (!CommonTools.isEmpty(abspath))
                      {
                          ap = FileSystemManager.GetBlogRootDataFolderRelativePath(blog.Name) + "/" + Path.GetFileName(abspath);
                          filemodel.FileName     = Path.GetFileName(abspath);
                          filemodel.Path         = abspath;
                          filemodel.RelativePath = ap;

                          filemodel.Owner = usr.UserName;
                          //filemodel.PostId =(int) postid;

                          db.Files.Add(filemodel);
                          await db.SaveChangesAsync();

                          FilesPostBlog filesPost = new FilesPostBlog();
                          filesPost.BlogId = blog.Id;
                          filesPost.FileId = filemodel.Id;
                          filesPost.PostId = (int)postid;
                          db.FilesPostsBlog.Add(filesPost);
                          await db.SaveChangesAsync();
                      }
                  }
              }
              return(ap); }
            catch (Exception ex)
            {
                CommonTools.ErrorReporting(ex);
                return(null);
            }
        }
Пример #2
0
        public async Task DeleteBlogAsync(string Blogname)
        {
            try
            {
                Blog ap = null;
                if (!CommonTools.isEmpty(Blogname))
                {
                    string       path    = FileSystemManager.GetBlogRootDataFolderRelativePath(Blogname);
                    List <Files> blfiles = (await this.GetBlogAsync(Blogname)).Files;
                    //if (blfiles != null)
                    //{
                    //    foreach (Files f in blfiles)
                    //    {
                    //        FileSystemManager.DeleteFile(f.RelativePath);
                    //    }
                    //    FileSystemManager.DeleteDirectory(path);
                    //}
                    //if (blfiles != null)
                    //{
                    //    db.Files.RemoveRange(blfiles);
                    //}

                    Blog blog = await this.GetBlogAsync(Blogname);

                    this.fileRecordManager.DeleteByBlog(Blogname);
                    PostManager postManager = new PostManager();
                    await postManager.DeleteByBlogId(blog.Id);

                    FileSystemManager.DeleteDirectory(path);
                    db.Blogs.Remove(blog);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                CommonTools.ErrorReporting(ex);
            }
        }