/// <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); } } }