Beispiel #1
0
 /// <summary>
 /// 新增群組成員
 /// </summary>
 /// <param name="User">使用者</param>
 /// <param name="Database">資料庫</param>
 public void AddMember(User User,SharpDiskDbContext Database)
 {
     if (IsMember(User, Database)) {
         throw new ApiInvalidOperationException("重複成員");
     }
     UserGroup newdata = new Models.UserGroup() {
         User = User,
         Group = this
     };
     Database.UserGroup.Add(newdata);
     Database.SaveChanges();
 }
Beispiel #2
0
 /// <summary>
 /// 檢查使用者是否為成員
 /// </summary>
 /// <param name="User">使用者</param>
 /// <param name="Database">資料庫</param>
 /// <returns></returns>
 public bool IsMember(User User,SharpDiskDbContext Database)
 {
     return User.Id == OwnerId || (from t in Database.UserGroup
                                   where t.UserId == User.Id && t.GroupId == this.Id
                                   select t).Count() > 0;
 }
Beispiel #3
0
 /// <summary>
 /// 取得群組成員
 /// </summary>
 /// <param name="Database">資料庫</param>
 /// <returns></returns>
 public IQueryable<User> GetMembers(SharpDiskDbContext Database)
 {
     return (from t in Database.UserGroup.Include(x=>x.User)
             where t.GroupId == this.Id
             select t.User);
 }
Beispiel #4
0
        /// <summary>
        /// 在指定的<see cref="SharpDiskDbContext"/>中移除此群組
        /// </summary>
        /// <param name="Database">指定資料庫</param>
        public void Delete(SharpDiskDbContext Database)
        {
            //將本群組所有成員退出
            Database.UserGroup.RemoveRange(from t in Database.UserGroup
                                           where t.GroupId == this.Id
                                           select t);

            //清除所有檔案節點有授權本節點存取之權利
            var fileNodes = from t in Database.FileNode
                            where t.GroupId == this.Id
                            select t;
            foreach(var fileNode in fileNodes) {
                fileNode.Group = null;//清除群組
                fileNode.GroupAuthority = FileNodeAuthority.Null;//清除群組權限
            }

            Database.Group.Remove(this);
            Database.SaveChanges();
        }
Beispiel #5
0
 public long GetTotalUsedSpace(SharpDiskDbContext Database)
 {
     return (from t in Database.FileNode
             where t.OwnerId == this.Id && t.IsFile
             select t.Size).Sum();
 }
Beispiel #6
0
 /// <summary>
 /// 在指定的<see cref="SharpDiskDbContext"/>中取得此使用者之根目錄節點
 /// </summary>
 /// <param name="Database">指定資料庫</param>
 public FileNode GetRootFileNode(SharpDiskDbContext Database)
 {
     return (from t in Database.FileNode
             where t.Parent == null && t.OwnerId == this.Id
             select t).First();
 }
Beispiel #7
0
 /// <summary>
 /// 取得檔案節點的父節點鍊
 /// </summary>
 /// <param name="Database">指定資料庫</param>
 /// <returns></returns>
 public List<FileNode> GetParentChain(SharpDiskDbContext Database) {
     List<FileNode> result = new List<FileNode>();
     if (!this.ParentId.HasValue) return result;
     var parent = (from t in Database.FileNode
                   where t.Id == this.ParentId
                   select t).First();
     result.Add(parent);
     result.AddRange(parent.GetParentChain(Database));
     return result;
 }
Beispiel #8
0
 public FileController(
     SharpDiskDbContext Database) : base(Database) {
 }
Beispiel #9
0
        /// <summary>
        /// 在指定的<see cref="SharpDiskDbContext"/>中移除此檔案節點
        /// </summary>
        /// <param name="Database">指定資料庫</param>
        public void Delete(SharpDiskDbContext Database) {
            if (this.ParentId == null) {//Root Node
                throw new ApiInvalidOperationException("使用者根節點不可刪除");
            }

            if (IsFile) {
                //檔案節點為終點,直接刪除與檔案實體
                File.Delete(Startup.FilesDirPath + this.Id);
            } else {
                //目錄節點必須要刪除子節點才可自我清除
                var child = from t in Database.FileNode
                            where t.ParentId == this.Id
                            select t;

                foreach (var node in child) node.Delete(Database);
            }
            Database.FileNode.Remove(this);
            Database.SaveChanges();
        }
Beispiel #10
0
        /// <summary>
        /// 建立目錄節點
        /// </summary>
        /// <param name="Name">目錄名稱</param>
        /// <param name="Database">資料庫</param>
        /// <returns></returns>
        public async Task<FileNode> CreateChildrenAsync(string Name, SharpDiskDbContext Database) {
            FileNode child = new FileNode() {
                Name = Name,
                Datetime = DateTime.Now,
                Parent = this,
                Owner = this.Owner,
                IsFile = false,
                GroupId = this.GroupId,
                GroupAuthority = this.GroupAuthority,
                OtherAuthority = this.OtherAuthority
            };

            Database.FileNode.Add(child);
            await Database.SaveChangesAsync();

            return child;
        }
Beispiel #11
0
        /// <summary>
        /// 建立檔案節點
        /// </summary>
        /// <param name="File">檔案</param>
        /// <param name="Database">資料庫</param>
        /// <returns></returns>
        public async Task<FileNode> CreateChildrenAsync(IFormFile File, SharpDiskDbContext Database) {
            FileNode newFile = new FileNode();

            #region 空間檢查
            if (//使用者空間不足
                Owner.SpaceSize.HasValue &&//有空間限制
                Owner.GetTotalUsedSpace(Database) + File.Length > Owner.SpaceSize) {
                throw new LackOfSpaceException($"目前使用者空間不足");
            }

            //檢查目錄鍊容量是否足夠
            var ParentChain = this.GetParentChain(Database);
            ParentChain.Insert(0, this);
            var TooSmail = ParentChain.Where(x =>
                x.DirMaxSize.HasValue && x.Size + File.Length > x.DirMaxSize
            );
            if (TooSmail.Count() > 0) {
                throw new LackOfSpaceException($"達到父系目錄限制大小:{string.Join(",", TooSmail.Select(x => x.Name))}");
            }
            DateTime now = DateTime.Now;
            foreach (var node in ParentChain) {
                node.Size += File.Length;
                node.Datetime = now;
            }
            #endregion

            #region 儲存檔案
            try {
                using (FileStream fileStream = System.IO.File.Create(Startup.FilesDirPath + newFile.Id)) {
                    await File.OpenReadStream().CopyToAsync(fileStream);
                    await fileStream.FlushAsync();
                }
            } catch (Exception e) {
                throw new UnknowException(e.Message);
            }
            #endregion

            #region FileNode建構
            newFile.IsFile = true;
            newFile.Owner = Owner;

            newFile.GroupId = this.GroupId;//上傳後的檔案權限繼承
            newFile.GroupAuthority = this.GroupAuthority;
            newFile.OtherAuthority = this.OtherAuthority;

            newFile.Parent = this;
            newFile.Size = File.Length;
            newFile.ContentType = File.ContentType;
            newFile.Ext = File.FileName.Split('.').LastOrDefault();
            newFile.Datetime = DateTime.Now;
            newFile.Name = File.FileName.Substring(0, File.FileName.LastIndexOf("."));
            #endregion

            Database.FileNode.Add(newFile);
            Database.SaveChanges();
            return newFile;
        }
Beispiel #12
0
 /// <summary>
 /// 確認指定的使用者擁有寫入權限
 /// </summary>
 /// <param name="User">使用者</param>
 /// <param name="Database">資料庫</param>
 /// <returns></returns>
 public bool HasWriteAuthority(User User, SharpDiskDbContext Database) {
     if (this.OwnerId == User?.Id) return true;//如果為擁有者則可
     if (this.GroupId != null &&//如果非擁有者但為群組成員且可Write則可
        (from t in Database.UserGroup
         where t.GroupId == this.GroupId && t.UserId == User.Id
         select t).Count() > 0 &&
         this.GroupAuthority == FileNodeAuthority.Write ||
         this.GroupAuthority == FileNodeAuthority.ReadAndWrite) {
         return true;
     }
     if (//可匿名Write
         this.GroupAuthority == FileNodeAuthority.Write ||
         this.GroupAuthority == FileNodeAuthority.ReadAndWrite) {
         return true;
     }
     return false;
 }
Beispiel #13
0
        public string GetAndCreateDirZipFileRealPath(User User, SharpDiskDbContext Database) {
            if (this.IsFile) throw new InvalidOperationException();
            
            Guid FileId = Guid.NewGuid();

            using (FileStream fs = new FileStream(Startup.FilesDirPath + "Zip/" + FileId + ".zip", FileMode.Create)) {
                using (ZipArchive arch = new ZipArchive(fs, ZipArchiveMode.Create)) {
                    Func<FileNode, string, Task> func = null;
                    func = async(FileNode dirFileNode , string path) => {
                        var child = dirFileNode.GetChildren(Database).ToList()//篩選可讀取的子系
                                        .Where(x => x.HasReadAuthority(User, Database));
                        foreach (var node in child.Where(x => x.IsFile)) {
                            arch.CreateEntryFromFile(
                                node.GetRealFilePath(),
                                path + dirFileNode.Name + "/" + node.FullName);
                        }
                        foreach (var node in child.Where(x => !x.IsFile)) {
                            await func(node,path + dirFileNode.Name + "/");
                        }
                    };
                    func(this,"");
                }
            }

            return Startup.FilesDirPath + "Zip/" + FileId + ".zip";
        }
Beispiel #14
0
 /// <summary>
 /// 取得目前節點的子節點
 /// </summary>
 /// <param name="Database"></param>
 /// <returns></returns>
 public IQueryable<FileNode> GetChildren(SharpDiskDbContext Database) {
     return (from t in Database.FileNode
             where t.ParentId == this.Id
             select t);
 }
Beispiel #15
0
 /// <summary>
 /// 取得父節點實體
 /// </summary>
 /// <param name="Database">指定資料庫</param>
 /// <returns></returns>
 public FileNode GetParent(SharpDiskDbContext Database) {
     if (!this.ParentId.HasValue) return null;
     return (from t in Database.FileNode
             where t.Id == this.ParentId
             select t).First();
 }
Beispiel #16
0
 /// <summary>
 /// 移除群組成員
 /// </summary>
 /// <param name="User"></param>
 /// <param name="Database"></param>
 public void RemoveMember(User User,SharpDiskDbContext Database)
 {
     if (OwnerId == User.Id) throw new ApiInvalidOperationException("目標不可為擁有者");
     if (!IsMember(User, Database)) throw new ApiInvalidOperationException("目標必須為成員");
     UserGroup data = new UserGroup() {
         User = User,
         Group = this
     };
     Database.UserGroup.Add(data);
     Database.SaveChanges();
 }
Beispiel #17
0
 public UserController(
     SharpDiskDbContext Database) : base(Database) {
 }
Beispiel #18
0
        /// <summary>
        /// 在指定的<see cref="SharpDiskDbContext"/>中移除此使用者
        /// </summary>
        /// <param name="Database">指定資料庫</param>
        public void Delete(SharpDiskDbContext Database)
        {
            #region DeleteGroup
            //退出所有加入的群組
            Database.UserGroup.RemoveRange(from t in Database.UserGroup
                                           where t.UserId == this.Id
                                           select t);
            Database.SaveChanges();

            //刪除所有擁有的群組
            var ownGroup = from t in Database.Group
                           where t.OwnerId == this.Id
                           select t;

            foreach (var group in ownGroup) {
                group.Delete(Database);
            }
            #endregion

            #region 刪除擁有的檔案節點
            GetRootFileNode(Database).Delete(Database);
            #endregion

            Database.User.Remove(this);
            Database.SaveChanges();
        }
 public SharpDiskControllerBase(SharpDiskDbContext Database)
 {
     //DI
     this.Database = Database;
 }
Beispiel #20
0
 public GroupController(
     SharpDiskDbContext Database)
     : base(Database)
 {
 }