// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseSwagger(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } using (var scope = app.ApplicationServices.CreateScope()) { var repo = scope.ServiceProvider.GetRequiredService <IAvatarRepository>(); var orepo = scope.ServiceProvider.GetRequiredService <IOwnerRepository>(); var trepo = scope.ServiceProvider.GetRequiredService <ITypeRepository>(); /* repo.Create(new Avatar { Name = "Chili", AvatarType = "Magician", Color = "Pink" }); * repo.Create(new Avatar { Name = "Bunsy", AvatarType = "Healer", Color = "Black" });*/ IAvatarRepository aRepo = new AvatarRepo(); IOwnerRepository owRepo = new OwnerRepo(); ITypeRepository tyrepo = new TypeRepo(); DBInit.InitData(); // Mock data IAvatarService aService = new AvatarService(aRepo); IOwnerService owService = new OwnerService(owRepo); ITypeService tService = new TypeService(tyrepo); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); // c.RoutePrefix = string.Empty; }); app.UseHttpsRedirection(); app.UseRouting(); app.UseCors("CustomerAppAllowSpecificOrigins"); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } //This is new using (var scope = app.ApplicationServices.CreateScope()) { var repo = scope.ServiceProvider.GetRequiredService <IAvatarRepository>(); var atrepo = scope.ServiceProvider.GetRequiredService <IAvatarTypeRepository>(); var owrepo = scope.ServiceProvider.GetRequiredService <IOwnerRepository>(); //repo.Create(new Avatar { Name = "Bunsy", Type ="Bunny", Color ="Blue"}); //repo.Create(new Avatar { Name = "Chili", Type = "Magician", Color = "Pink" }); IAvatarRepository avatarRepository = new AvatarRepo(); IAvatarTypeRepository avatarTypeRepository = new AvatarTypeRepo(); IOwnerRepository ownerRepository = new OwnerRepo(); //new DBinitializer = new DBinitializer(); //d.InitData(); DBinitializer.InitData(); IAvatarService avatarService = new AvatarService(avatarRepository); IAvatarTypeService avatarTypeService = new AvatarTypeService(avatarTypeRepository); IOwnerService ownerService = new OwnerService(ownerRepository); //YOU DID GITHUB WIHT TERMINAL !!! // app.UseWelcomePage(); app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } }
/// <summary> /// 为指定用户创建文件 /// </summary> public async Task <FileStorageInfo> CreateFileAsync(FileOwnerTypeId ownerTypeId, string hash, Stream file, string fileName, int periodMinute = 0) { if (!_clusterSvce.CurrentServer.AllowUpload) { throw new FriendlyException("请从上传服务器上传"); } string tmpFilePath = null; try { var extName = Path.GetExtension(fileName); if (string.IsNullOrEmpty(extName)) { throw new FriendlyException("文件名缺少扩展名"); } extName = extName.Substring(1).ToLower(); var mime = _mimeProvider.GetMimeByExtensionName(extName); //获取hash if (file != null) { tmpFilePath = await ReceiveToTempFileAsync(file); //优先使用文件的hash //hash = FileUtil.GetSha1(tmpFilePath); } if (string.IsNullOrWhiteSpace(hash)) { throw new FriendlyException("hash 必需指定"); } //throw new FriendlyException("file 与 hash 必需至少指定一个"); Task tSync = null; var pseudoId = GeneratePseudoId(hash); var fileInfo = await FileRepo.GetFileByHashAsync(pseudoId, hash); //文件不存在,并且没有传file流 if (fileInfo == null && tmpFilePath == null) { throw new FileNotFoundException("File does not exist"); } //检查所有者剩余配额 var fileSize = fileInfo?.Length ?? new System.IO.FileInfo(tmpFilePath).Length; var remainQuota = await OwnerRepo.GetOwnerRemainQuotaAsync(ownerTypeId.OwnerType, ownerTypeId.OwnerId); if (remainQuota < fileSize) { throw new FriendlyException("您已经没有足够的空间上传该文件"); } Service.Options.Server recServer = null; //检查存在否 if (fileInfo == null) { //插入新文件记录 recServer = _clusterSvce.ElectServer(); fileInfo = new File { ServerId = recServer.Id, Length = new System.IO.FileInfo(tmpFilePath).Length, MimeId = (int)mime.Id, SHA1 = hash, ExtInfo = string.Empty, CreateTime = DateTime.Now }; await FileRepo.AddFileAsync(fileInfo, pseudoId); tSync = _clusterSvce.SyncFileToServerAsync(this, tmpFilePath, fileInfo, pseudoId, recServer); } else { recServer = _clusterSvce.GetServerById(fileInfo.ServerId); var fileExists = await _clusterSvce.RawFileExistsAsync(this, recServer, pseudoId, fileInfo.CreateTime, fileInfo.Id); if (!fileExists) { //通过hash进来的,并且文件不存在 if (string.IsNullOrEmpty(tmpFilePath)) { await FileRepo.DeleteFileAsync(ownerTypeId.OwnerId, fileInfo.Id, pseudoId); throw new FriendlyException("File does not exist(CFA-FE)"); } //文件被删或意外丢失重传 tSync = _clusterSvce.SyncFileToServerAsync(this, tmpFilePath, fileInfo, pseudoId, recServer); } } var fileOwner = await FileRepo.GetFileOwnerByOwnerAsync(pseudoId, fileInfo.Id, ownerTypeId.OwnerType, ownerTypeId.OwnerId); if (fileOwner == null) { fileOwner = new FileOwner { FileId = fileInfo.Id, Name = fileName, OwnerType = ownerTypeId.OwnerType, OwnerId = ownerTypeId.OwnerId, CreateTime = DateTime.Now }; await FileRepo.AddFileOwnerAsync(fileOwner, pseudoId); } if (tSync != null) { await tSync; } //添加配额使用量 await OwnerRepo.AddOwnerUsedQuotaAsync(ownerTypeId.OwnerType, ownerTypeId.OwnerId, fileSize); return(new FileStorageInfo { File = fileInfo, Owner = fileOwner, Server = recServer, PseudoId = pseudoId }); } finally { if (tmpFilePath != null) { FileUtil.TryDelete(tmpFilePath); } } }