Пример #1
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);
        }
Пример #2
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);
        }
Пример #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();
        }
Пример #4
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);
        }
Пример #5
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();
        }
Пример #6
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();
        }