public async Task <bool> RawFileExistsAsync(IStorageService storageSvce, Service.Options.Server server, uint pseudoId, DateTime fileCreateTime, int fileId) { //如果目标服务器就是当前服务器,则直接移动文件 if (CurrentServer.Id == server.Id) { return(storageSvce.RawFileExists(pseudoId, fileCreateTime, fileId)); } else { var fct = Uri.EscapeDataString(fileCreateTime.ToString(CultureInfo.CurrentCulture)); var url = $"{GetCurrentScheme()}://{server.Host}/api/cluster/files/{fileId}/exists?pseudoId={pseudoId}&fileCreateTime={fct}"; var rspStr = await _httpClient.GetStringAsync(url); var json = JObject.Parse(rspStr); var errCode = json.Value <int>("errCode"); if (errCode != 0 && errCode != 100) { throw new InvalidProgramException($"获取文件存在状态:{json.Value<string>("errMsg")}"); } return(errCode == 0); } }
public async Task SyncFileToServerAsync(IStorageService storageSvce, string filePath, File file, uint pseudoId, Service.Options.Server server) { //如果目标服务器就是当前服务器,则直接移动文件 if (CurrentServer.Id == server.Id) { var realFilePath = storageSvce.GetRawFilePath(pseudoId, file.CreateTime, file.Id); await storageSvce.MoveToPathAsync(filePath, realFilePath, true); } else { //var fTokenStr = _fileTokenCodec.Encode(fToken); //var url = $"{server.Host}/api/cluster/files"; //var mfdCont = new MultipartFormDataContent(); //mfdCont.AddByString("fileToken", fTokenStr); //mfdCont.AddByFile("file", filePath); //var rspMsg = await _httpClient.PostAsync(url, mfdCont); //rspMsg.EnsureSuccessStatusCode(); //var rspStr = await rspMsg.Content.ReadAsStringAsync(); //var json = JObject.Parse(rspStr); //if (json.Value<int>("errCode") != 0) // throw new InvalidProgramException($"同步失败:{json.Value<string>("errMsg")}"); throw new NotImplementedException(); } }
public async Task DeleteFileAsync(IStorageService storageSvce, uint pseudoId, DateTime fileCreateTime, int fileId, Service.Options.Server server) { if (CurrentServer.Id == server.Id) { await storageSvce.DeleteFileAsync(pseudoId, fileCreateTime, fileId); } else { throw new NotImplementedException("暂未实现"); } }
/// <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); } } }