Exemplo n.º 1
0
        /// <summary>
        /// 附加現有檔案
        /// </summary>
        /// <param name="context">資料庫內容</param>
        /// <param name="fileId">檔案唯一識別號</param>
        /// <param name="stream">檔案串流</param>
        /// <returns>檔案實例</returns>
        public static async Task <File> Append(HHStorageContext context, Guid fileId, Stream stream)
        {
            var file = context.File.SingleOrDefault(x => x.Id == fileId);

            if (file == null)
            {
                throw new NotFoundException("找不到指定檔案");
            }

            try {
                using (FileStream fileStream = System.IO.File.OpenWrite(GetFilePathById(file.Id))) {
                    await stream.CopyToAsync(fileStream);

                    await fileStream.FlushAsync();
                }
            } catch {
                throw new OperationInterruptedException("檔案上傳過程遭到中斷");
            }

            file.Size += stream.Length;

            await context.SaveChangesAsync();

            return(file);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 建立新儲存體
        /// </summary>
        /// <param name="context">資料庫內容</param>
        /// <param name="userId">使用者帳號</param>
        /// <param name="name">名稱</param>
        /// <returns>儲存體實例</returns>
        public static async Task <Repository> Create(HHStorageContext context, string userId, string name)
        {
            if (userId == null)
            {
                throw new NotNullException("使用者帳號不該為null");
            }
            if (!context.User.Any(x => x.Id == userId))
            {
                throw new NotFoundException("找不到指定使用者");
            }
            if (name == null)
            {
                throw new NotNullException("儲存體名稱不該為null");
            }
            var result = new Repository()
            {
                Name   = name,
                UserId = userId
            };

            context.Repository.Add(result);
            await context.SaveChangesAsync();

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 刪除指定使用者帳戶所有資料
        /// </summary>
        /// <param name="context">資料庫內容</param>
        /// <param name="id">使用者帳戶</param>
        /// <returns>非同步操作</returns>
        public static async Task Delete(HHStorageContext context, string id)
        {
            if (id == null)
            {
                throw new NotNullException("使用者帳戶名稱不該為null");
            }
            if (id.ToLower() == "admin")
            {
                throw new OperationException("系統管理者帳戶admin不可刪除");
            }
            var targetUser = context.User.SingleOrDefault(x => x.Id == id);

            if (targetUser == null)
            {
                throw new NotFoundException("找不到指定的使用者");
            }

            // 刪除檔案
            foreach (var file in context.File.Where(x => x.UserId == id))
            {
                await EF.File.Delete(context, file.Id);
            }
            context.RemoveRange(context.WebHook.Where(x => x.UserId == id));
            context.RemoveRange(context.Origin.Where(x => x.UserId == id));
            context.RemoveRange(context.Repository.Where(x => x.UserId == id));

            await context.SaveChangesAsync();
        }
Exemplo n.º 4
0
        /// <summary>
        /// 使用帳號與密碼登入並取得使用者實例
        /// </summary>
        /// <param name="context">資料庫內容</param>
        /// <param name="id">帳號</param>
        /// <param name="password">密碼</param>
        /// <returns>使用者實例</returns>
        public static async Task <User> Login(HHStorageContext context, string id, string password)
        {
            if (id == null || password == null)
            {
                throw new NotNullException("使用者帳戶名稱與密碼不該為null");
            }

            var passwordHash = password.ToHashString <MD5>();

            return(context.User.SingleOrDefault(x => x.Id == id && x.Password == passwordHash));
        }
Exemplo n.º 5
0
        /// <summary>
        /// 建立新檔案
        /// </summary>
        /// <param name="context">資料庫內容</param>
        /// <param name="userId">使用者帳號</param>
        /// <param name="repositoryId">儲存庫唯一識別號</param>
        /// <param name="stream">檔案串流</param>
        /// <param name="contentType">檔案ContentType</param>
        /// <param name="name">名稱</param>
        /// <param name="accessModifier">存取限制</param>
        /// <returns>檔案實例</returns>
        public static async Task <File> Create(
            HHStorageContext context,
            string userId,
            Guid repositoryId,
            Stream stream,
            string contentType,
            string name = null,
            AccessModifierTypes accessModifier = AccessModifierTypes.Private
            )
        {
            if (userId == null)
            {
                throw new NotNullException("使用者帳號不該為null");
            }

            if (!context.Repository.Any(x => x.Id == repositoryId && x.UserId == userId))
            {
                throw new NotFoundException("找不到該使用者指定儲存庫");
            }

            var result = new File()
            {
                UserId       = userId,
                RepositoryId = repositoryId,
                ContentType  = contentType
            };

            result.Name           = name;
            result.AccessModifier = accessModifier;
            result.Size           = stream.Length;

            try {
                using (FileStream fileStream = System.IO.File.Create(GetFilePathById(result.Id))) {
                    await stream.CopyToAsync(fileStream);

                    await fileStream.FlushAsync();
                }
            } catch {
                throw new OperationInterruptedException("檔案上傳過程遭到中斷");
            }

            context.File.Add(result);
            await context.SaveChangesAsync();

            return(result);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 刪除指定檔案
        /// </summary>
        /// <param name="context">資料庫內容</param>
        /// <param name="fileId">檔案唯一識別號</param>
        /// <returns>非同步操作</returns>
        public static async Task Delete(HHStorageContext context, Guid fileId)
        {
            var file = context.File.SingleOrDefault(x => x.Id == fileId);

            if (file == null)
            {
                throw new NotFoundException("找不到指定檔案");
            }

            // 刪除實體檔案
            if (System.IO.File.Exists(GetFilePathById(fileId)))
            {
                System.IO.File.Delete(GetFilePathById(fileId));
            }

            context.File.Remove(file);

            await context.SaveChangesAsync();
        }
Exemplo n.º 7
0
        /// <summary>
        /// 建立新使用者帳戶
        /// </summary>
        /// <param name="context">資料庫內容</param>
        /// <param name="id">帳戶</param>
        /// <param name="password">密碼</param>
        /// <returns>使用者實例</returns>
        public static async Task <User> Create(HHStorageContext context, string id, string password)
        {
            if (id == null || password == null)
            {
                throw new NotNullException("使用者帳戶名稱與密碼不該為null");
            }
            if (context.User.Any(x => x.Id == id))
            {
                throw new DuplicateException("使用者帳戶名稱重複");
            }

            var result = new User()
            {
                Id = id, Password = password.ToHashString <MD5>()
            };
            await context.User.AddAsync(result);

            return(result);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 刪除儲存體
        /// </summary>
        /// <param name="context">資料庫內容</param>
        /// <param name="id">儲存庫唯一識別號</param>
        /// <returns>非同步操作</returns>
        public static async Task Delete(HHStorageContext context, Guid id)
        {
            var repository = context.Repository.SingleOrDefault(x => x.Id == id);

            if (repository == null)
            {
                throw new NotFoundException("找不到指定的儲存庫");
            }

            foreach (var file in context.File.Where(x => x.RepositoryId == id))
            {
                await EF.File.Delete(context, file.Id);
            }

            context.RemoveRange(context.WebHook.Where(x => x.RepositoryId == id));
            context.RemoveRange(context.Origin.Where(x => x.RepositoryId == id));
            context.Repository.Remove(repository);

            await context.SaveChangesAsync();
        }